mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 22:30:50 +08:00 
			
		
		
		
	small refactor of RandomPortFinder
This commit is contained in:
		@@ -1,55 +1,58 @@
 | 
				
			|||||||
using System;
 | 
					namespace Ocelot.AcceptanceTests
 | 
				
			||||||
using System.Collections.Concurrent;
 | 
					 | 
				
			||||||
using System.Collections.Generic;
 | 
					 | 
				
			||||||
using System.Linq;
 | 
					 | 
				
			||||||
using System.Net;
 | 
					 | 
				
			||||||
using System.Net.Http;
 | 
					 | 
				
			||||||
using System.Net.Sockets;
 | 
					 | 
				
			||||||
using System.Text;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
namespace Ocelot.AcceptanceTests
 | 
					 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
					    using System;
 | 
				
			||||||
 | 
					    using System.Collections.Concurrent;
 | 
				
			||||||
 | 
					    using System.Linq;
 | 
				
			||||||
 | 
					    using System.Net;
 | 
				
			||||||
 | 
					    using System.Net.Sockets;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public static class RandomPortFinder
 | 
					    public static class RandomPortFinder
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        private static readonly int TrialNumber = 100;
 | 
					        private const int TrialNumber = 100;
 | 
				
			||||||
        private static readonly int BeginPortRange = 20000;
 | 
					        private const int BeginPortRange = 20000;
 | 
				
			||||||
        private static readonly int EndPortRange = 45000;
 | 
					        private const int EndPortRange = 45000;
 | 
				
			||||||
 | 
					        private static readonly Random Random = new Random();
 | 
				
			||||||
        private static Random random = new Random();
 | 
					        private static readonly ConcurrentBag<int> UsedPorts = new ConcurrentBag<int>();
 | 
				
			||||||
        private static ConcurrentBag<int> usedPorts = new ConcurrentBag<int>();
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public static int GetRandomPort()
 | 
					        public static int GetRandomPort()
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            int randomPort = 0;
 | 
					            for (var i = 0; i < TrialNumber; i++)
 | 
				
			||||||
            for (int i = 0; i < TrialNumber; i++)
 | 
					 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                randomPort = random.Next(BeginPortRange, EndPortRange);
 | 
					                var randomPort = Random.Next(BeginPortRange, EndPortRange);
 | 
				
			||||||
                if (usedPorts.Any(p => p == randomPort))
 | 
					 | 
				
			||||||
                {
 | 
					 | 
				
			||||||
                    continue;
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
                else
 | 
					 | 
				
			||||||
                {
 | 
					 | 
				
			||||||
                    usedPorts.Add(randomPort);
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
                try
 | 
					                if (!PortInUse(randomPort))
 | 
				
			||||||
                {
 | 
					                {
 | 
				
			||||||
                    IPEndPoint ipe = new IPEndPoint(IPAddress.Loopback, randomPort);
 | 
					                    try
 | 
				
			||||||
                    using (var socket = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
 | 
					 | 
				
			||||||
                    {
 | 
					                    {
 | 
				
			||||||
                        socket.Bind(ipe);
 | 
					                        return UsePort(randomPort);
 | 
				
			||||||
                        socket.Close();
 | 
					                    }
 | 
				
			||||||
                        return randomPort;
 | 
					                    catch (Exception)
 | 
				
			||||||
 | 
					                    {
 | 
				
			||||||
 | 
					                        // ignored
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
                catch (Exception)
 | 
					 | 
				
			||||||
                {
 | 
					 | 
				
			||||||
                    continue;
 | 
					 | 
				
			||||||
                }                
 | 
					 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            throw new Exception("Cannot find available port to bind to.");
 | 
					            throw new Exception("Cannot find available port to bind to.");
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        private static int UsePort(int randomPort)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            UsedPorts.Add(randomPort);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            var ipe = new IPEndPoint(IPAddress.Loopback, randomPort);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            using (var socket = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                socket.Bind(ipe);
 | 
				
			||||||
 | 
					                socket.Close();
 | 
				
			||||||
 | 
					                return randomPort;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        private static bool PortInUse(int randomPort)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            return UsedPorts.Any(p => p == randomPort);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user