/// <summary>
        /// Starts to listen to the given port and ipAddress.
        /// </summary>
        public async void Start()
        {
            if (IsOnline)
            {
                return;
            }

            listener = new TcpListener(System.Net.IPAddress.Parse(IPAddress), Port);
            IsOnline = !IsOnline;
            listener.Start();

            while (IsOnline)
            {
                TcpClient tcpClient = await listener.AcceptTcpClientAsync();

                TcpConnection tcpConnection = ConnectionFactory.CreateTcpConnection(tcpClient);
                tcpConnection.ConnectionClosed      += tcpOrUdpConnectionClosed;
                tcpConnection.ConnectionEstablished += udpConnectionReceived;
                connections.Add(tcpConnection, new List <UdpConnection>());

                //Inform all subscribers.
                if (connectionEstablished != null &&
                    connectionEstablished.GetInvocationList().Length > 0)
                {
                    connectionEstablished(tcpConnection, ConnectionType.TCP);
                }
            }
        }
        /// <summary>
        /// Starts to listen to the given port and ipAddress.
        /// </summary>
        public async void StartTCPListener()
        {
            if (IsTCPOnline)
            {
                return;
            }

            tcpListener = new TcpListener(System.Net.IPAddress.Parse(IPAddress), Port);
            IsTCPOnline = !IsTCPOnline;
            tcpListener.Start();

            while (IsTCPOnline)
            {
                TcpClient tcpClient = await tcpListener.AcceptTcpClientAsync();

                TcpConnection tcpConnection = ConnectionFactory.CreateTcpConnection(tcpClient);
                tcpConnection.ConnectionClosed      += connectionClosed;
                tcpConnection.ConnectionEstablished += udpConnectionReceived;
                connections.GetOrAdd(tcpConnection, new List <UdpConnection>());

                //Inform all subscribers.
                if (connectionEstablished != null &&
                    connectionEstablished.GetInvocationList().Length > 0)
                {
                    connectionEstablished(tcpConnection, ConnectionType.TCP);
                }

                KnownTypes.ForEach(tcpConnection.AddExternalPackets);
                //Now that the server registered all the methods, unlock the client.
                tcpConnection.UnlockRemoteConnection();
            }
        }
Exemple #3
0
 /// <summary>
 /// Creates a new <see cref="TcpConnection"/> instance from the given <see cref="TcpClient"/>.
 /// </summary>
 /// <param name="tcpClient">The <see cref="TcpClient"/> to use for the <see cref="TcpConnection"/>.</param>
 /// <returns>A <see cref="TcpConnection"/> that uses the given <see cref="TcpClient"/> to send data to and from the client.</returns>
 protected virtual TcpConnection CreateTcpConnection(TcpClient tcpClient) => ConnectionFactory.CreateTcpConnection(tcpClient);