示例#1
0
        private void ListenerOnPeerDisconnectedEvent(NetPeer peer, DisconnectInfo disconnectInfo)
        {
            if (!ConnectedPlayers.TryGetValue(peer.Id, out Player player))
            {
                return;
            }

            _logger.Info($"Player {player.Username} lost connection! Reason: {disconnectInfo.Reason}");

            switch (disconnectInfo.Reason)
            {
            case DisconnectReason.RemoteConnectionClose:
                ChatLogPanel.PrintGameMessage($"Player {player.Username} disconnected!");
                break;

            case DisconnectReason.Timeout:
                ChatLogPanel.PrintGameMessage($"Player {player.Username} timed out!");
                break;

            default:
                ChatLogPanel.PrintGameMessage($"Player {player.Username} lost connection!");
                break;
            }

            HandlePlayerDisconnect(player);
        }
示例#2
0
        private void ListenerOnNetworkLatencyUpdateEvent(NetPeer peer, int latency)
        {
            if (!ConnectedPlayers.TryGetValue(peer.Id, out Player player))
            {
                return;
            }

            player.Latency = latency;
        }
示例#3
0
文件: Server.cs 项目: nzgamer41/Tango
        /// <summary>
        ///     When we get a message from a client, we handle the message here
        ///     and perform any necessary tasks.
        /// </summary>
        private void ListenerOnNetworkReceiveEvent(NetPeer peer, NetPacketReader reader, DeliveryMethod deliveryMethod)
        {
            try
            {
                // Handle ConnectionRequest as special case
                if (reader.PeekByte() == 0)
                {
                    Command.Parse(reader, out CommandHandler handler, out byte[] message);
                    ConnectionRequestHandler requestHandler = (ConnectionRequestHandler)handler;
                    requestHandler.HandleOnServer(message, peer);
                    return;
                }

                // Parse this message
                ConnectedPlayers.TryGetValue(peer.Id, out Player player);
                bool relayOnServer = Command.ParseOnServer(reader, player);

                if (relayOnServer)
                {
                    // Copy relevant message part (exclude protocol headers)
                    byte[] data = new byte[reader.UserDataSize];
                    Array.Copy(reader.RawData, reader.UserDataOffset, data, 0, reader.UserDataSize);

                    // Send this message to all other clients
                    var peers = _netServer.ConnectedPeerList;
                    foreach (var client in peers)
                    {
                        // Don't send the message back to the client that sent it.
                        if (client.Id == peer.Id)
                        {
                            continue;
                        }

                        // Send the message so the other client can stay in sync
                        client.Send(data, DeliveryMethod.ReliableOrdered);
                    }
                }
            }
            catch (Exception ex)
            {
                ChatLogPanel.PrintGameMessage(ChatLogPanel.MessageType.Error, "Error while parsing command. See log.");
                _logger.Error(ex, $"Encountered an error while reading command from {peer.EndPoint.Address}:{peer.EndPoint.Port}:");
            }
        }
示例#4
0
        /// <summary>
        ///     When we get a message from a client, we handle the message here
        ///     and perform any necessary tasks.
        /// </summary>
        private void ListenerOnNetworkReceiveEvent(NetPeer peer, NetDataReader reader)
        {
            try
            {
                // Handle ConnectionRequest as special case
                if (reader.Data[0] == 0)
                {
                    Command.Parse(reader.Data, out CommandHandler handler, out byte[] message);
                    ConnectionRequestHandler requestHandler = (ConnectionRequestHandler)handler;
                    requestHandler.HandleOnServer(message, peer);
                    return;
                }

                // Parse this message
                ConnectedPlayers.TryGetValue(peer.ConnectId, out Player player);
                Command.ParseOnServer(reader.Data, player);

                // Send this message to all other clients
                var peers = _netServer.GetPeers();
                foreach (var client in peers)
                {
                    // Don't send the message back to the client that sent it.
                    if (client.ConnectId == peer.ConnectId)
                    {
                        continue;
                    }

                    // Send the message so the other client can stay in sync
                    client.Send(reader.Data, SendOptions.ReliableOrdered);
                }
            }
            catch (Exception ex)
            {
                CSM.Log($"Encountered an error while reading command from {peer.EndPoint.Host}:{peer.EndPoint.Port}:");
                CSM.Log(ex.ToString());
            }
        }
示例#5
0
        private void ListenerOnPeerDisconnectedEvent(NetPeer peer, DisconnectInfo disconnectInfo)
        {
            if (!ConnectedPlayers.TryGetValue(peer.ConnectId, out Player player))
            {
                return;
            }

            switch (disconnectInfo.Reason)
            {
            case DisconnectReason.RemoteConnectionClose:
                CSM.Log($"Player {player.Username} disconnected!");
                break;

            case DisconnectReason.Timeout:
                CSM.Log($"Player {player.Username} timed out!");
                break;

            default:
                CSM.Log($"Player {player.Username} lost connection!");
                break;
            }

            HandlePlayerDisconnect(player);
        }