mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-05 00:10:49 +08:00 
			
		
		
		
	small refactor of RandomPortFinder
This commit is contained in:
		@@ -1,55 +1,58 @@
 | 
			
		||||
using System;
 | 
			
		||||
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
 | 
			
		||||
namespace Ocelot.AcceptanceTests
 | 
			
		||||
{
 | 
			
		||||
    using System;
 | 
			
		||||
    using System.Collections.Concurrent;
 | 
			
		||||
    using System.Linq;
 | 
			
		||||
    using System.Net;
 | 
			
		||||
    using System.Net.Sockets;
 | 
			
		||||
 | 
			
		||||
    public static class RandomPortFinder
 | 
			
		||||
    {
 | 
			
		||||
        private static readonly int TrialNumber = 100;
 | 
			
		||||
        private static readonly int BeginPortRange = 20000;
 | 
			
		||||
        private static readonly int EndPortRange = 45000;
 | 
			
		||||
 | 
			
		||||
        private static Random random = new Random();
 | 
			
		||||
        private static ConcurrentBag<int> usedPorts = new ConcurrentBag<int>();
 | 
			
		||||
        private const int TrialNumber = 100;
 | 
			
		||||
        private const int BeginPortRange = 20000;
 | 
			
		||||
        private const int EndPortRange = 45000;
 | 
			
		||||
        private static readonly Random Random = new Random();
 | 
			
		||||
        private static readonly ConcurrentBag<int> UsedPorts = new ConcurrentBag<int>();
 | 
			
		||||
 | 
			
		||||
        public static int GetRandomPort()
 | 
			
		||||
        {
 | 
			
		||||
            int randomPort = 0;
 | 
			
		||||
            for (int i = 0; i < TrialNumber; i++)
 | 
			
		||||
            for (var i = 0; i < TrialNumber; i++)
 | 
			
		||||
            {
 | 
			
		||||
                randomPort = random.Next(BeginPortRange, EndPortRange);
 | 
			
		||||
                if (usedPorts.Any(p => p == randomPort))
 | 
			
		||||
                {
 | 
			
		||||
                    continue;
 | 
			
		||||
                }
 | 
			
		||||
                else
 | 
			
		||||
                {
 | 
			
		||||
                    usedPorts.Add(randomPort);
 | 
			
		||||
                }
 | 
			
		||||
                var randomPort = Random.Next(BeginPortRange, EndPortRange);
 | 
			
		||||
 | 
			
		||||
                try
 | 
			
		||||
                if (!PortInUse(randomPort))
 | 
			
		||||
                {
 | 
			
		||||
                    IPEndPoint ipe = new IPEndPoint(IPAddress.Loopback, randomPort);
 | 
			
		||||
                    using (var socket = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
 | 
			
		||||
                    try
 | 
			
		||||
                    {
 | 
			
		||||
                        socket.Bind(ipe);
 | 
			
		||||
                        socket.Close();
 | 
			
		||||
                        return randomPort;
 | 
			
		||||
                        return UsePort(randomPort);
 | 
			
		||||
                    }
 | 
			
		||||
                    catch (Exception)
 | 
			
		||||
                    {
 | 
			
		||||
                        // ignored
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
                catch (Exception)
 | 
			
		||||
                {
 | 
			
		||||
                    continue;
 | 
			
		||||
                }                
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            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