Beispiel #1
0
 public MobileTcpListener(MobileNetworkSettings settings, UsersService userService)
 {
     _settings = settings;
     _listener = new TcpSocketListener ();
     _userService = userService;
     _listener.ConnectionReceived = OnConnectionReceived;
 }
        public TcpSocketListenerPage()
        {
            _listener = new TcpSocketListener();

            _messagesSub = new Subject<Message>();
            _messagesObs = _messagesSub.AsObservable();

            _listener.ConnectionReceived += (sender, args) =>
            {
                var client = args.SocketClient;
                Device.BeginInvokeOnMainThread(() => _clients.Add(client));

                Task.Factory.StartNew(() =>
                {
                    foreach (var msg in client.ReadStrings(_canceller.Token))
                    {
                        _messagesSub.OnNext(msg);
                    }
                    
                    Device.BeginInvokeOnMainThread(()=> _clients.Remove(client));
                }, TaskCreationOptions.LongRunning);

            };

            InitView();
        }
        public async Task TcpSocketClient_ShouldSendReceiveData()
        {
            var bytesToSend = new byte[] {0x1, 0x2, 0x3, 0x4, 0x5};
            var len = bytesToSend.Length;
            var port = PortGranter.GrantPort();

            var listener = new TcpSocketListener();
            await listener.StartListeningAsync(port);

            var tcs = new TaskCompletionSource<bool>();
            listener.ConnectionReceived += async (sender, args) =>
            {
                var bytesReceived = new byte[len];
                await args.SocketClient.ReadStream.ReadAsync(bytesReceived, 0, len);

                var allSame =
                    Enumerable
                        .Zip(bytesToSend,
                            bytesReceived,
                            (s, r) => s == r)
                        .All(b => b);

                tcs.SetResult(allSame);
            };

            var client = new TcpSocketClient();
            await client.ConnectAsync("127.0.0.1", port);
            await client.WriteStream.WriteAsync(bytesToSend, 0, len);

            var ok = await tcs.Task;

            await listener.StopListeningAsync();

            Assert.True(ok);
        }
Beispiel #4
0
        public async Task StartServer(int port, bool singleConnection)
        {
            // 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();
                }
            };

            // Start listening
            await listener.StartListeningAsync(port);
        }
        /// <inheritdoc/>
        public override async Task<FtpResponse> Process(FtpCommand command, CancellationToken cancellationToken)
        {
            if (Data.PassiveSocketClient != null)
            {
                await Data.PassiveSocketClient.DisconnectAsync();
                Data.PassiveSocketClient.Dispose();
                Data.PassiveSocketClient = null;
            }

            if (Data.TransferTypeCommandUsed != null && !string.Equals(command.Name, Data.TransferTypeCommandUsed, StringComparison.OrdinalIgnoreCase))
                return new FtpResponse(500, $"Cannot use {command.Name} when {Data.TransferTypeCommandUsed} was used before.");

            int port;
            var isEpsv = string.Equals(command.Name, "EPSV", StringComparison.OrdinalIgnoreCase);
            if (isEpsv)
            {
                if (string.IsNullOrEmpty(command.Argument) || string.Equals(command.Argument, "ALL", StringComparison.OrdinalIgnoreCase))
                {
                    port = 0;
                }
                else
                {
                    port = Convert.ToInt32(command.Argument, 10);
                }
            }
            else
            {
                port = 0;
            }

            Data.TransferTypeCommandUsed = command.Name;

            var sem = new SemaphoreSlim(0, 1);
            var listener = new TcpSocketListener();
            listener.ConnectionReceived += (sender, args) =>
            {
                Data.PassiveSocketClient = args.SocketClient;
                sem.Release();
            };
            await listener.StartListeningAsync(port);
            var localPort = listener.LocalPort;

            if (isEpsv || Server.ServerAddress.Contains(":"))
            {
                var listenerAddress = new Address(localPort);
                await Connection.WriteAsync(new FtpResponse(229, $"Entering Extended Passive Mode ({listenerAddress})."), cancellationToken);
            }
            else
            {
                var listenerAddress = new Address(Server.ServerAddress, localPort);
                await Connection.WriteAsync(new FtpResponse(227, $"Entering Passive Mode ({listenerAddress})."), cancellationToken);
            }

            await sem.WaitAsync(TimeSpan.FromSeconds(5), cancellationToken);
            await listener.StopListeningAsync();
            listener.Dispose();

            return null;
        }
        public async Task TcpSocketClient_ShouldBeAbleToConnect()
        {
            var port = PortGranter.GrantPort();
            var listener = new TcpSocketListener();
            await listener.StartListeningAsync(port);

            var sut = new TcpSocketClient();
            await sut.ConnectAsync("127.0.0.1", port);

            await listener.StopListeningAsync();

            // getting here means nothing went boom
            Assert.True(true);
        }
Beispiel #7
0
        public GWServerHttp(int serverPort = DefaultPort, IGWClient[] clients = null)
        {

            _listener = new TcpSocketListener();
            _port = serverPort;

            if (clients != null)
            {
                foreach (IGWClient client in clients)
                    _clients.Add(client.Name.ToLower(), client);
            }

            _listener.ConnectionReceived += (s, e) => ProcessRequestAsync(e.SocketClient);
        }
Beispiel #8
0
        public Server()
        {
            this.IsMulticastConnected = false;
            this._receiver = new UdpSocketMulticastClient();
            this._receiver.MessageReceived += MessageReceived;
            this._listener = new TcpSocketListener();
            this._listener.ConnectionReceived += ListenerConnectionReceived;
            IsMulticastConnected = JoinMultiUdp();

            if (!IsMulticastConnected)
            {
                var msg = new Message(MessageTypes.NetworkWarning, "Cannot connect to multicast UDP.");
                MessageManager.SendMessage(msg);
            }
        }
        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);

                    Connected?.Invoke(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;
                }
            }
        }
Beispiel #10
0
        public string revc_text(int length)
        {
            string result = "";
            var listener = new TcpSocketListener();
            listener.ConnectionReceived += async (sender, args) =>
              {
                  var _client = args.SocketClient;
                  List<byte> all_byte = new List<byte>();
                  for (int i = 0; i < length; i++)
                  {
                      var nextByte = await Task.Run(() => client.ReadStream.ReadByte());
                      all_byte.Add((byte)nextByte);

                  }
                  result = (new System.Text.UTF8Encoding()).GetString(all_byte.ToArray(), 0, all_byte.Count);
              };
            listener.StartListeningAsync(port[0]);
            return result;
        }
Beispiel #11
0
 private async Task ExecuteServerListener(CancellationToken cancellationToken)
 {
     using (var listener = new TcpSocketListener(0))
     {
         listener.ConnectionReceived = ConnectionReceived;
         await listener.StartListeningAsync(Port);
         try
         {
             for (; ;)
             {
                 cancellationToken.ThrowIfCancellationRequested();
                 await Task.Delay(100, cancellationToken);
             }
         }
         finally
         {
             await listener.StopListeningAsync();
             foreach (var connection in _connections.ToArray())
                 connection.Close();
         }
     }
 }
		async void loginButton_Clicked(object sender, EventArgs e)
		{
			StringBuilder sb = new StringBuilder("https://accounts.google.com/o/oauth2/auth");
			// append query strings
			sb.Append("?");
			sb.Append("scope=email%20profile&");
			sb.Append("redirect_uri=http://localhost:" + LISTEN_PORT + "&");
			sb.Append("response_type=code&");
			sb.Append("client_id=" + APP_ID);

			Debug.WriteLine("url: " + sb.ToString());

			webView.Source = sb.ToString();
			webView.IsVisible = true;


			TcpSocketListener listener = new TcpSocketListener();

			listener.ConnectionReceived += DidConnect;

			await listener.StartListeningAsync(LISTEN_PORT);


			//HttpClient mClient = new HttpClient
			//{
			//	BaseAddress = new Uri("https://accounts.google.com/o/oauth2/auth")
			//};

			//http://stackoverflow.com/questions/17096201/build-query-string-for-system-net-httpclient-get
		}
        [InlineData(1000)] // yes buffered stream
        public async Task TcpSocketClient_ShouldBeAbleToDisconnectThenReconnect(int bufferSize)
        {
            TcpSocketClient sut = null;

            var port = PortGranter.GrantPort();
            var listener = new TcpSocketListener();
            await listener.StartListeningAsync(port);

            if (bufferSize != -1)
                sut = new TcpSocketClient(bufferSize);
            else
                sut = new TcpSocketClient();

            await sut.ConnectAsync("127.0.0.1", port);
            await sut.DisconnectAsync();

            await sut.ConnectAsync("127.0.0.1", port); 
            await sut.DisconnectAsync();

            await listener.StopListeningAsync();
        }
        public async Task TcpSocketClient_ShouldSendReceiveDataSimultaneously(int bufferSize)
        {
            var port = PortGranter.GrantPort();

            TcpSocketListener listener = null;
            TcpSocketClient socket1 = null;

            // get both ends of a connected socket, with or without buffer
            if (bufferSize != -1)
            {
                listener = new TcpSocketListener(bufferSize);
                socket1 = new TcpSocketClient(bufferSize);
            }
            else
            {
                listener = new TcpSocketListener();
                socket1 = new TcpSocketClient();
            }

            var tcs = new TaskCompletionSource<ITcpSocketClient>();
            
            await listener.StartListeningAsync(port);
            listener.ConnectionReceived += (sender, args) => tcs.SetResult(args.SocketClient);

            await socket1.ConnectAsync("127.0.0.1", port);

            var socket2 = await tcs.Task;
            await listener.StopListeningAsync();

            // socket1 is the socket from the client end
            // socket2 is the socket from the server end

            // for five seconds, send and receive the data 
            var sentToSocket1 = new List<byte>();
            var sentToSocket2 = new List<byte>();
            var recvdBySocket1 = new List<byte>();
            var recvdBySocket2 = new List<byte>();

            // send random data and keep track of it
            // also keep track of what is received
            Func<ITcpSocketClient, List<byte>, List<byte>, CancellationToken, Task> sendAndReceive =
                (socket, sent, recvd, token) =>
                {
                    var r = new Random(socket.GetHashCode());
                    var send = Task.Run(async () =>
                    {
                        var buf = new byte[1000];
                        while (!token.IsCancellationRequested)
                        {
                            r.NextBytes(buf);
                            sent.AddRange(buf);
                            await socket.WriteStream.WriteAsync(buf, 0, buf.Length, token);
                            await socket.WriteStream.FlushAsync();
                        }
                    });

                    var recv = Task.Run(async () =>
                    {
                        var buf = new byte[1000];
                        while (!token.IsCancellationRequested)
                        {
                            await socket.ReadStream.ReadAsync(buf, 0, buf.Length, token);
                            recvd.AddRange(buf);
                        }
                    });

                    return Task.WhenAll(send, recv);
                };

            // let the sockets run for 2.5 seconds
            var cts = new CancellationTokenSource();
            var timer = Task.Run(() => Task.Delay(TimeSpan.FromSeconds(2.5)).ContinueWith(t => cts.Cancel()));

            var socketRunners =
                Task.WhenAll(
                    Task.Run(() => sendAndReceive(socket1, sentToSocket2, recvdBySocket1, cts.Token)),
                    Task.Run(() => sendAndReceive(socket2, sentToSocket1, recvdBySocket2, cts.Token))
                    );

            await timer;
            await socketRunners;

            Debug.WriteLine("Sent to S1:{0}, Recvd by S1:{1}", sentToSocket1.Count, recvdBySocket1.Count);
            Debug.WriteLine("Sent to S2:{0}, Recvd by S2:{1}", sentToSocket2.Count, recvdBySocket2.Count);

            // zip will join up to the lowest index of both lists (must be recvd)
            // it's ok if recvd is shorter than sent because we cancel abruptly,
            // but we want to be sure that everything that was received matches 
            // everything that sent, and that we did both send and receive.

            var socket1OK =
                Enumerable.Zip(sentToSocket1,
                            recvdBySocket1,
                            (s, r) => s == r)
                        .All(b => b);

            var socket2OK =
                Enumerable.Zip(sentToSocket1,
                            recvdBySocket1,
                            (s, r) => s == r)
                        .All(b => b);

            Assert.True(socket1OK, "Socket1 received data did not match what was sent to it");
            Assert.True(socket2OK, "Socket2 received data did not match what was sent to it");

            // eww, but does the job we need
            Assert.True(recvdBySocket1.Count > 5000, String.Format("Socket 1 received data count was less than 5000 : {0}", recvdBySocket1.Count));
            Assert.True(recvdBySocket2.Count > 5000, String.Format("Socket 2 received data count was less than 5000 : {0}", recvdBySocket2.Count));

            await socket1.DisconnectAsync();
            await socket2.DisconnectAsync();
        }
        public async Task TcpSocketClient_ShouldSendReceiveDataSimultaneously(int bufferSize)
        {
            var port = PortGranter.GrantPort();

            TcpSocketListener listener = null;
            TcpSocketClient socket1 = null;

            // get both ends of a connected socket, with or without buffer
            if (bufferSize != -1)
            {
                listener = new TcpSocketListener(bufferSize);
                socket1 = new TcpSocketClient(bufferSize);
            }
            else
            {
                listener = new TcpSocketListener();
                socket1 = new TcpSocketClient();
            }

            var tcs = new TaskCompletionSource<ITcpSocketClient>();
            
            await listener.StartListeningAsync(port);
            listener.ConnectionReceived += (sender, args) => tcs.SetResult(args.SocketClient);

            await socket1.ConnectAsync("127.0.0.1", port);

            var socket2 = await tcs.Task;
            await listener.StopListeningAsync();

            // socket1 is the socket from the client end
            // socket2 is the socket from the server end

            // for five seconds, send and receive the data 
            var sentToSocket1 = new List<byte>();
            var sentToSocket2 = new List<byte>();
            var recvdBySocket1 = new List<byte>();
            var recvdBySocket2 = new List<byte>();

            // send random data and keep track of it
            // also keep track of what is received
            Func<ITcpSocketClient, List<byte>, List<byte>, CancellationToken, Task> sendAndReceive =
                (socket, sent, recvd, token) =>
                {
                    var r = new Random(socket.GetHashCode());
                    var send = Task.Run(async () =>
                    {
                        var buf = new byte[1000];
                        while (!token.IsCancellationRequested)
                        {
                            r.NextBytes(buf);
                            sent.AddRange(buf);
                            await socket.WriteStream.WriteAsync(buf, 0, buf.Length, token);
                            await socket.WriteStream.FlushAsync(token);
                        }
                    });

                    var recv = Task.Run(async () =>
                    {
                        var buf = new byte[1000];
                        while (!token.IsCancellationRequested)
                        {
                            var len = await socket.ReadStream.ReadAsync(buf, 0, buf.Length, token);
                            recvd.AddRange(buf.Take(len));
                        }
                    });

                    var innerTcs = new TaskCompletionSource<bool>();
                    token.Register(() => innerTcs.SetResult(true));

                    return innerTcs.Task;
                };

            // let the sockets run for 2.5 seconds
            var socketRunners =
                Task.WhenAll(
                    Task.Run(() => sendAndReceive(socket1, sentToSocket2, recvdBySocket1, new CancellationTokenSource(TimeSpan.FromSeconds(2.5)).Token).ContinueWith(t=> Debug.WriteLine($"Socket 1 task completed: {t}"))),
                    Task.Run(() => sendAndReceive(socket2, sentToSocket1, recvdBySocket2, new CancellationTokenSource(TimeSpan.FromSeconds(2.5)).Token).ContinueWith(t => Debug.WriteLine($"Socket 2 task completed: {t}")))
                    );

            await socketRunners;

            Debug.WriteLine("Sent to S1:{0}, Recvd by S1:{1}", sentToSocket1.Count, recvdBySocket1.Count);
            Debug.WriteLine("Sent to S2:{0}, Recvd by S2:{1}", sentToSocket2.Count, recvdBySocket2.Count);

            // zip will join up to the lowest index of both lists (must be recvd)
            // it's ok if recvd is shorter than sent because we cancel abruptly,
            // but we want to be sure that everything that was received matches 
            // everything that sent, and that we did both send and receive.

            var socket1Errors =
                Enumerable.Zip(recvdBySocket1, sentToSocket1,
                            (r, s) => s == r)
                        .Count(b => !b);

            var socket2Errors =
                Enumerable.Zip(recvdBySocket2, 
                                sentToSocket2,
                            (r, s) => s == r)
                        .Count(b => !b);
            
            var max = new[] {recvdBySocket1.Count, recvdBySocket2.Count, sentToSocket1.Count, sentToSocket2.Count}.Max();
            Func<List<byte>, int, string> getValueOrStars =
                (bytes, i) => bytes.Count > i ? ((int) bytes[i]).ToString().PadLeft(3, '0') : "***";

            var rows =
                Enumerable.Range(0, max - 1)
                    .Select(i =>
                    {
                        var sTo1 = getValueOrStars(sentToSocket1, i);
                        var rBy1 = getValueOrStars(recvdBySocket1, i);
                        var sTo2 = getValueOrStars(sentToSocket2, i);
                        var rBy2 = getValueOrStars(recvdBySocket2, i);

                        return new { Index = i, SentTo1 = sTo1, ReceivedBy1 = rBy1, SentTo2 = sTo2, ReceivedBy2 = rBy2};
                    })
                    .Where(r => (r.SentTo1 != r.ReceivedBy1 && r.ReceivedBy1 != "***") || (r.SentTo2 != r.ReceivedBy2 && r.ReceivedBy2 != "***"))
                    .Select(r=> $"{r.Index}: \t{r.SentTo1}\t{r.ReceivedBy1}\t{r.SentTo2}\t{r.ReceivedBy2}\t")
                    .Take(1000);
            
            Func<IEnumerable<byte>, string> show = bs => $"[ {String.Join(", ", bs.Take(5).Select(b => (int) b))} ]";

            if (socket1Errors > 0 || socket2Errors > 0)
            {
                Assert.True(socket1Errors == 0,
                    $"Socket1 received {socket1Errors} byte/s that did not match what was sent to it{Environment.NewLine}{String.Join(Environment.NewLine, rows)}");
                Assert.True(socket2Errors == 0,
                    $"Socket2 received {socket2Errors} byte/s that did not match what was sent to it{Environment.NewLine}{String.Join(Environment.NewLine, rows)}");
            }
            // eww, but does the job we need
            Assert.True(recvdBySocket1.Count > 5000, String.Format("Socket 1 received data count was less than 5000 : {0}", recvdBySocket1.Count));
            Assert.True(recvdBySocket2.Count > 5000, String.Format("Socket 2 received data count was less than 5000 : {0}", recvdBySocket2.Count));

            await socket1.DisconnectAsync();
            await socket2.DisconnectAsync();
        }
Beispiel #16
0
 public TcpListener(NetworkSettings settings)
 {
     _settings = settings;
     _listener = new TcpSocketListener();
 }
Beispiel #17
0
        private Task ExecuteServerListener(AutoResetEvent e)
        {
            return Task.Run(() =>
            {
                _log = LogManager?.CreateLog(typeof(FtpServer));
                using (var listener = new TcpSocketListener(0))
                {
                    listener.ConnectionReceived = ConnectionReceived;
                    try
                    {
                        e.Reset();
                        listener.StartListeningAsync(Port).Wait();
                        _log?.Debug("Server listening on port {0}", Port);

                        try
                        {
                            e.WaitOne();
                        }
                        finally
                        {
                            listener.StopListeningAsync().Wait();
                            foreach (var connection in _connections.ToArray())
                                connection.Close();
                        }
                    }
                    catch (Exception ex)
                    {
                        _log?.Fatal(ex, "{0}", ex.Message);
                    }
                }
            }
            );
        }