/// <summary> /// Binds the <code>TcpSocketListener</code> to the specified port on all endpoints and listens for TCP connections. /// </summary> /// <param name="port">The port to listen on. If '0', selection is delegated to the operating system.</param> /// <param name="listenOn">The <code>CommsInterface</code> to listen on. If unspecified, all interfaces will be bound.</param> /// <returns></returns> public Task StartListeningAsync(int port, ICommsInterface listenOn = null) { if (listenOn != null && !listenOn.IsUsable) throw new InvalidOperationException("Cannot listen on an unusable interface. Check the IsUsable property before attemping to bind."); _listenCanceller = new CancellationTokenSource(); _backingStreamSocketListener = new StreamSocketListener(); _backingStreamSocketListener.ConnectionReceived += (sender, args) => { var nativeSocket = args.Socket; var wrappedSocket = new TcpSocketClient(nativeSocket, _bufferSize); var eventArgs = new TcpSocketListenerConnectEventArgs(wrappedSocket); if (ConnectionReceived != null) ConnectionReceived(this, eventArgs); }; var sn = port == 0 ? "" : port.ToString(); #if !WP80 if (listenOn != null) { var adapter = ((CommsInterface)listenOn).NativeNetworkAdapter; return _backingStreamSocketListener .BindServiceNameAsync(sn, SocketProtectionLevel.PlainSocket, adapter) .AsTask(); } else #endif return _backingStreamSocketListener .BindServiceNameAsync(sn) .AsTask(); }
private void OnConnectionReceived(object sender, TcpSocketListenerConnectEventArgs e) { if (ConnectionReceived != null) { var remotePoint = new RemotePoint(e.SocketClient.RemotePort, e.SocketClient.RemoteAddress); ConnectionReceived(this, new ListenerConnectEventArgs(remotePoint.Address, remotePoint.Port, new MobileTcpRemoteClient(remotePoint, e.SocketClient))); } }
async void DidConnect(object sender, TcpSocketListenerConnectEventArgs args) { ITcpSocketClient client = args.SocketClient; StreamReader reader = new StreamReader(client.ReadStream); int numLines = 10; int linesRead = 0; try { while (linesRead < numLines ) { string line = reader.ReadLine(); await HandleHttpAuthCode(line, linesRead); //Debug.WriteLine(line); linesRead++; } } catch (IOException) { Debug.WriteLine("-----------------REACHED END YO. read " + linesRead + " lines -------------"); } }
private void ListenerConnectionReceived(object sender, TcpSocketListenerConnectEventArgs e) { var client = (TcpSocketClient)e.SocketClient; var bytes = new List<byte>(); while (true) { int nextByte = ReadByte(client); if (nextByte == -1) break; bytes.Add((byte)nextByte); } if (ReceivedBytes != null) { var eventArgs = new ReceivedBytesEventArgs { ConnectionType = ConnectionTypes.Tcp }; ReceivedBytes(bytes.ToArray(), eventArgs); bytes.Clear(); } }
private void ConnectionReceived(object sender, TcpSocketListenerConnectEventArgs args) { var connection = new FtpConnection(this, args.SocketClient, DefaultEncoding); Statistics.ActiveConnections += 1; Statistics.TotalConnections += 1; connection.Closed += ConnectionOnClosed; _connections.Add(connection); connection.Log = LogManager?.CreateLog(connection); OnConfigureConnection(connection); connection.Start(); }
#pragma warning disable 4014 private void WaitForConnections(CancellationToken cancelToken) { Task.Factory.StartNew(async () => { while (!cancelToken.IsCancellationRequested) { var nativeClient = await Task.Run(() => _backingTcpListener.AcceptTcpClient(), cancelToken); var wrappedClient = new TcpSocketClient(nativeClient, _bufferSize); var eventArgs = new TcpSocketListenerConnectEventArgs(wrappedClient); if (ConnectionReceived != null) ConnectionReceived(this, eventArgs); } }, cancelToken, TaskCreationOptions.LongRunning, TaskScheduler.Default); }