Beispiel #1
0
        /// <summary>
        /// Starts the transport protocol.
        /// </summary>
        public void Start()
        {
            int port = Port;

            for (int attempt = 0; attempt < 5; attempt++)
            {
                try
                {
                    listener = new TcpListener(LocalBindAddress, port + attempt);
                    listener.Start();
                }
                catch (SocketException ex)
                    when(ex.SocketErrorCode == SocketError.AddressAlreadyInUse && bindToNextAvailablePort)
                    {
                        // Try next available port
                        continue;
                    }

                break;
            }

            // If port=0 was supplied, set the actual port we are listening on.
            Port           = ((IPEndPoint)listener.LocalEndpoint).Port;
            LocalConection = new LocalTcpConnectionDetails(Port, null, LocalBindAddress);
            ListenForIncomingConnections();
        }
Beispiel #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TcpTransportProtocol"/> class that will listen on the specified port.
 /// </summary>
 /// <param name="port">Port to listen on for incoming connections.</param>
 /// <param name="bindToNextAvailablePort">If the specified port is in use, attempts to bind to the next available port.</param>
 /// <param name="localBindAddress">The local address to use for connections.</param>
 /// <param name="acceptConnectionHandler">Invoked to accept the connection.</param>
 public TcpTransportProtocol(
     int port,
     bool bindToNextAvailablePort,
     IPAddress localBindAddress,
     Action <AcceptConnectionEventArgs> acceptConnectionHandler)
 {
     streams = new ConcurrentBag <TcpTransportStream>();
     AcceptConnectionHandler      = acceptConnectionHandler;
     this.bindToNextAvailablePort = bindToNextAvailablePort;
     Port             = port;
     LocalBindAddress = localBindAddress;
     LocalConection   = new LocalTcpConnectionDetails(port, null, localBindAddress);
     RateLimiter      = new RateLimiter();
 }