Ejemplo n.º 1
0
        public async Task StartServer(int port, bool singleConnection, int retryCount = 1)
        {
            // Create TCP listener
            var listener = new TcpSocketListener(2048);

            listener.ConnectionReceived = async(sender, args) =>
            {
                var clientSocketContext = new SimpleSocket();

                try
                {
                    // Stop listening if we accept only a single connection
                    if (singleConnection)
                    {
                        await listener.StopListeningAsync();
                    }

                    clientSocketContext.SetSocket((TcpSocketClient)args.SocketClient);

                    // Do an ack with magic packet (necessary so that we know it's not a dead connection,
                    // it sometimes happen when doing port forwarding because service don't refuse connection right away but only fails when sending data)
                    await SendAndReceiveAck(clientSocketContext.socket, MagicAck, MagicAck);

                    if (Connected != null)
                    {
                        Connected(clientSocketContext);
                    }

                    clientSocketContext.isConnected = true;
                }
                catch (Exception)
                {
                    clientSocketContext.DisposeSocket();
                }
            };

            for (int i = 0; i < retryCount; ++i)
            {
                try
                {
                    // Start listening
                    await listener.StartListeningAsync(port);

                    break; // Break if no exception, otherwise retry
                }
                catch (Exception)
                {
                    // If there was an exception last try, propragate exception
                    if (i == retryCount - 1)
                    {
                        throw;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public async Task StartServer(int port, bool singleConnection, int retryCount = 1)
        {
            // Create TCP listener
            var listener = new TcpSocketListener(2048);

            listener.ConnectionReceived = async (sender, args) =>
            {
                var clientSocketContext = new SimpleSocket();

                try
                {
                    // Stop listening if we accept only a single connection
                    if (singleConnection)
                        await listener.StopListeningAsync();

                    clientSocketContext.SetSocket((TcpSocketClient)args.SocketClient);

                    // Do an ack with magic packet (necessary so that we know it's not a dead connection,
                    // it sometimes happen when doing port forwarding because service don't refuse connection right away but only fails when sending data)
                    await SendAndReceiveAck(clientSocketContext.socket, MagicAck, MagicAck);

                    if (Connected != null)
                        Connected(clientSocketContext);

                    clientSocketContext.isConnected = true;
                }
                catch (Exception)
                {
                    clientSocketContext.DisposeSocket();
                }
            };

            for (int i = 0; i < retryCount; ++i)
            {
                try
                {
                    // Start listening
                    await listener.StartListeningAsync(port);
                    break; // Break if no exception, otherwise retry
                }
                catch (Exception)
                {
                    // If there was an exception last try, propragate exception
                    if (i == retryCount - 1)
                        throw;
                }
            }
        }