Example #1
0
        /// <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.</param>
        /// <param name="listenOn">The <code>CommsInterface</code> to listen on. If unspecified, all interfaces will be bound.</param>
        /// <returns></returns>
        public async 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);

                var eventArgs = new TcpSocketListenerConnectEventArgs(wrappedSocket);
                if (ConnectionReceived != null)
                {
                    ConnectionReceived(this, eventArgs);
                }
            };

#if !WP80
            if (listenOn != null)
            {
                var adapter = ((CommsInterface)listenOn).NativeNetworkAdapter;

                await _backingStreamSocketListener.BindServiceNameAsync(port.ToString(), SocketProtectionLevel.PlainSocket, adapter);
            }
            else
#endif
            await _backingStreamSocketListener.BindServiceNameAsync(port.ToString());
        }
Example #2
0
        //This method runs in a seperate thread.
        //This is undesirable because windows form elements will complain about shared resources not being avaliable
        //solution is to use a semaphore that is picked up in the other thread
        private async void ListenerTCP_ConnectionReceived(object sender, TcpSocketListenerConnectEventArgs e)
        {
            //wait until previous connection has been handled
            await tcpConnProccessed.WaitAsync();

            //update the shared memory
            curTcpConnection = e;

            //tell main thread of new message (using signal)
            tcpConnection.Release();
        }
Example #3
0
        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();
        }
Example #4
0
        private async void ListenTcpLoop()
        {
            bool tcpListenerActive = true;

            while (tcpListenerActive)
            {
                //wait until signal is recieved
                TcpSocketListenerConnectEventArgs tcpConnection = await ReceivedIncomingTCP();

                PeerConnectTCPRequest?.Invoke(this, tcpConnection);
            }
        }
        private void connectionReceived(object sender, TcpSocketListenerConnectEventArgs e)
        {
            var citpClient = new RemoteCitpTcpClient(_log, e.SocketClient, _cancellationTokenSource.Token);

            citpClient.Disconnected    += clientDisconnected;
            citpClient.MessageReceived += clientMessageReceived;
            Clients.TryAdd(citpClient.RemoteEndPoint, citpClient);

            ClientConnected?.Invoke(this, citpClient);

            citpClient.OpenStream();
        }
Example #6
0
#pragma warning disable 4014
        private void WaitForConnections(CancellationToken cancelToken)
        {
            Task.Factory.StartNew(async() =>
            {
                while (!cancelToken.IsCancellationRequested)
                {
                    var nativeClient  = await _backingTcpListener.AcceptTcpClientAsync();
                    var wrappedClient = new TcpSocketClient(nativeClient, _bufferSize);

                    var eventArgs = new TcpSocketListenerConnectEventArgs(wrappedClient);
                    ConnectionReceived?.Invoke(this, eventArgs);
                }
            },
                                  cancelToken,
                                  TaskCreationOptions.LongRunning,
                                  TaskScheduler.Default);
        }
        private async void Listener_ConnectionReceived(object sender, TcpSocketListenerConnectEventArgs e)
        {
            Debug.WriteLine("Client connected");

            var msg       = new StringBuilder();
            int bytesRead = -1;

            byte[] buf = new byte[5];

            while (bytesRead != 0)
            {
                bytesRead = await e.SocketClient.ReadStream.ReadAsync(buf, 0, 5);

                msg.Append(Encoding.UTF8.GetString(buf, 0, bytesRead));
            }
            RaiseSoundboxEvent(msg.ToString());
        }
        /// <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.</param>
        /// <returns></returns>
        public async Task StartListeningAsync(int port)
        {
            _listenCanceller             = new CancellationTokenSource();
            _backingStreamSocketListener = new StreamSocketListener();

            _backingStreamSocketListener.ConnectionReceived += (sender, args) =>
            {
                var nativeSocket  = args.Socket;
                var wrappedSocket = new TcpSocketClient(nativeSocket);

                var eventArgs = new TcpSocketListenerConnectEventArgs(wrappedSocket);
                if (ConnectionReceived != null)
                {
                    ConnectionReceived(this, eventArgs);
                }
            };

            await _backingStreamSocketListener.BindServiceNameAsync(port.ToString());
        }
Example #9
0
        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 -------------");
            }
        }
Example #10
0
 public void NewTCPConnection(object sender, TcpSocketListenerConnectEventArgs e)
 {
     StoreConnectedPeerTCP(e.SocketClient);
 }