Exemplo n.º 1
0
        public void SendPlayerDisconnectPacket(List <NetConnection> all, PlayerDisconnectsPacket packet)
        {
            Logger.Info("Player disconnected: " + packet.player);
            playerPositions.Remove(packet.player);
            players.Remove(packet.player);

            NetOutgoingMessage message = server.CreateMessage();

            packet.PacketToNetOutGoingMessage(message);
            server.SendMessage(message, all, NetDeliveryMethod.ReliableOrdered, 0);
        }
Exemplo n.º 2
0
        public void Listen()
        {
            Logger.Info("Listening for clients...");

            while (!Console.KeyAvailable || Console.ReadKey().Key != ConsoleKey.Escape)
            {
                NetIncomingMessage message;

                while ((message = server.ReadMessage()) != null)
                {
                    Logger.Info("Message received");

                    //Get list of users
                    List <NetConnection> all = server.Connections;

                    switch (message.MessageType)
                    {
                    case NetIncomingMessageType.StatusChanged:
                        NetConnectionStatus status = (NetConnectionStatus)message.ReadByte();

                        string reason = message.ReadString();

                        if (status == NetConnectionStatus.Connected)
                        {
                            var player = NetUtility.ToHexString(message.SenderConnection.RemoteUniqueIdentifier);
                            // add player to dictionary
                            players.Add(player);

                            // send player local ID
                            SendLocalPlayerPacket(message.SenderConnection, player);

                            // send spawn info
                            spawnPlayers(all, message.SenderConnection, player);
                        }
                        break;

                    case NetIncomingMessageType.Data:
                        //Get packet type
                        byte type = message.ReadByte();

                        //Create packet
                        Packet packet;

                        switch (type)
                        {
                        case (byte)PacketTypes.PositionPacket:
                            packet = new PositionPacket();
                            packet.NetIncomingMessageToPacket(message);
                            SendPositionPacket(all, (PositionPacket)packet);
                            break;

                        case (byte)PacketTypes.DicePacket:
                            packet = new DicePacket();
                            packet.NetIncomingMessageToPacket(message);
                            SendDicePacket(all, (DicePacket)packet);
                            break;

                        case (byte)PacketTypes.PlayerDisconnectsPacket:
                            packet = new PlayerDisconnectsPacket();
                            packet.NetIncomingMessageToPacket(message);
                            SendPlayerDisconnectPacket(all, (PlayerDisconnectsPacket)packet);
                            break;

                        default:
                            Logger.Error("Unhandled data / Packet type");
                            break;
                        }
                        break;

                    case NetIncomingMessageType.DebugMessage:
                    case NetIncomingMessageType.ErrorMessage:
                    case NetIncomingMessageType.WarningMessage:
                    case NetIncomingMessageType.VerboseDebugMessage:
                        string text = message.ReadString();
                        Logger.Debug(text);
                        break;

                    default:
                        Logger.Error("Unhandled type: " + message.MessageType + " " + message.LengthBytes + " bytes " + message.DeliveryMethod);
                        break;
                    }

                    server.Recycle(message);
                }
            }
        }