示例#1
0
        /// <summary>
        /// Called when a client connects to the tcp listener
        /// Will send the client their id via the welcome message if they successfully connect
        /// </summary>
        private static void TCPConnectCallback(IAsyncResult _result)
        {
            // Begins connection with the client
            TcpClient _client = tcpListener.EndAcceptTcpClient(_result);

            tcpListener.BeginAcceptTcpClient(TCPConnectCallback, null);
            Debug.Log($"Incoming connection from {_client.Client.RemoteEndPoint}...");

            // Will search for a slot and assign the connectee to an available slot
            for (int i = 1; i <= MaxPlayers; i++)
            {
                if (clients[i].tcp.socket == null)
                {
                    clients[i].tcp.Connect(_client);
                    ServerSender.SendWelcome(i);
                    return;
                }
            }
            // Only reaches this point if the server is full
            for (int i = MaxPlayers + 1; i <= MaxPlayers + 10; i++)
            {
                if (clients[i].tcp.socket == null)
                {
                    clients[i].tcp.Connect(_client);
                    ServerSender.SendError(i, "Failed to connect, server is full");
                }
            }

            Debug.Log($"{_client.Client.RemoteEndPoint} failed to connect: Server full!");
        }
示例#2
0
        /// <summary>
        /// Disconnect the client from the server
        /// </summary>
        public void Disconnect()
        {
            Debug.Log($"{id} has disconnected");

            tcp.Disconnect();
            udp.Disconnect();

            ServerSender.SendDisconnect(id);
        }
示例#3
0
        /// <summary>
        /// Disconnect the client from the server
        /// </summary>
        public void Disconnect()
        {
            Console.WriteLine($"{id} has disconnected");

            tcp.Disconnect();
            udp.Disconnect();

            ServerSender.SendDisconnect(id);
        }