Esempio n. 1
0
        private static void TCPConnectAsyncCallback(IAsyncResult asyncResult)
        {
            TcpClient client = TCPListener.EndAcceptTcpClient(asyncResult);

            TCPBeginAcceptClient();
            Console.WriteLine($"\n\tServer: {ServerName}, user {client.Client.RemoteEndPoint} is trying to connect...");

            for (int count = 1; count < MaxNumPlayers + 1; count++)
            {
                if (ClientDictionary[count].tCP.Socket == null)
                {
                    ClientDictionary[count].tCP.Connect(client);
                    Console.WriteLine($"\tServer: {ServerName}, sent welcome packet to: {count}");
                    ServerSend.Welcome(count, $"Welcome to {ServerName} server client: {count}", MapName);
                    return;
                }
            }
            for (int count = 1; count < MaxNumPlayers + 1; count++)
            {
                if (NotConnectedClients[count].tCP.Socket == null)
                {
                    NotConnectedClients[count].tCP.Connect(client);
                    ServerSend.ServerIsFullPacket(count);
                    Console.WriteLine($"\n\tThe server is full... {client.Client.RemoteEndPoint} couldn't connect...");
                }
            }
        }
        //Method is used to accept incoming connection
        public void AcceptCallback(IAsyncResult result)
        {
            Connection connection = new Connection();

            lock (locker) // establish only one connection at time
            {
                try
                {
                    //we try to make a connection
                    connection           = new Connection();
                    connection.tcpClient = TCPListener.EndAcceptTcpClient(result); // Ending accepting the connection
                    // Adding client to connections that are already established
                    Connections.Add(connection);

                    //Handshake based on WebSocket protocol
                    confirmConnectionByHandshake(connection);
                }
                catch (ObjectDisposedException e)
                {
                    Console.WriteLine(e.ToString()); // if anything went wrong
                }
            }
            try
            {
                //print that client has connected
                Console.WriteLine("Client connected: " +
                                  ((IPEndPoint)connection.tcpClient.Client.RemoteEndPoint).Address +
                                  ":" + ((IPEndPoint)connection.tcpClient.Client.RemoteEndPoint).Port);
            }
            catch (Exception)
            {
                ///...
            }

            try
            {
                // start of the async data reading that are being send via TCP protocol
                connection.tcpClient.GetStream().BeginRead(connection.buffer, 0, connection.buffer.Length,
                                                           new AsyncCallback(ReadCallBack), connection);
            }
            catch (Exception)
            {
                try
                {
                    // If connection is lost print information
                    Console.WriteLine("Connection lost: " + ((IPEndPoint)connection.tcpClient.
                                                             Client.RemoteEndPoint).Address + ":" +
                                      ((IPEndPoint)connection.tcpClient.Client.RemoteEndPoint).Port);

                    //Delete information about the connection
                    disconnect(((IPEndPoint)connection.tcpClient.Client.RemoteEndPoint));
                }
                catch (Exception)
                {
                    ///...
                }
            }
            // Start async method that waits for accepting another connection
            TCPListener.BeginAcceptTcpClient(new AsyncCallback(AcceptCallback), null);
        }