PeekByte() public method

Reads a Byte without advancing the read pointer
public PeekByte ( ) : byte
return byte
        public static Packet HandleMessage(NetIncomingMessage msg)
        {
            byte header = msg.PeekByte();
            Console.WriteLine(header);
            Packet p = GetPacket(msg.PeekByte());

            p.Decode(msg);

            return p;
        }
Example #2
0
        private void ReadPacket(NetIncomingMessage msg)
        {
            switch (msg.PeekByte ()) {
            case Packet.CLIENTINFO:
                ClientInfo ci = ClientInfo.decode (ref msg);
                if(clientId == - 1){
                    clientId = ci.ClientId;
                }
                else if (ci.ClientId != clientId ){

                    if(otherClients.ContainsKey(ci.ClientId)){
                        NetworkClient c = otherClients[ci.ClientId];
                        c.Pseudo = ci.Pseudo;
                        c.Player.Color = ci.Color;
                    }
                    else
                    {
                        NetworkClient newClient = new NetworkClient(ci.ClientId, null);
                        newClient.Player = new Player(map, imageManager);
                        newClient.Player.Color = ci.Color;
                        newClient.Pseudo = ci.Pseudo;
                        map.AddPlayer(newClient.Player);
                        otherClients.Add(newClient.ClientId, newClient);
                    }
                }
                break;

            case Packet.USERMESSAGE:
                UserMessage uMsg = UserMessage.decode(ref msg);
                if(uMsg.ClientId == clientId){
                    uMsgBuffer.clientCorrection(player, uMsg);
                }
                else
                {
                    if(otherClients.ContainsKey(uMsg.ClientId)){
                        NetworkClient c = otherClients[uMsg.ClientId];
                        c.Player.LookAt(uMsg.EyePosition);
                        c.Player.Layer = uMsg.Layer;
                        if(VectorUtils.Distance(c.Player.Position, uMsg.Position) > 1){
                            c.Player.Position = uMsg.Position;
                        }
                        else
                            c.Player.Position += (c.Player.Position - uMsg.Position) * 0.1F;
                        return;
                    }
                }
                break;

            case Packet.BLOCKUPDATE:
                BlockUpdate bu = BlockUpdate.decode(ref msg);
                if(bu.Added)
                    map.ForceAddCube(bu.Position, bu.BlockType, bu.Layer);
                else
                    map.DeleteCube(bu.Position, bu.Layer);
                break;

            case Packet.CLIENTDISCONNECT:
                ClientDisconnect cd = ClientDisconnect.decode(ref msg);

                if(otherClients.ContainsKey(cd.ClientId)){
                    NetworkClient c = otherClients[cd.ClientId];
                    otherClients.Remove(c.ClientId);
                    map.DeletePlayer(c.Player);
                }
                break;

            case Packet.CHATMESSAGE:
                ChatMessage cm = ChatMessage.decode(ref msg);
                chat.addMsg(cm.Pseudo +": " + cm.Message);
                break;

            default:
                break;
            }
        }
Example #3
0
 /// <summary>
 /// Handles a single incoming data packet from a client.
 /// </summary>
 /// <param name="msg">The packet to handle.</param>
 private void HandleDataPacket( NetIncomingMessage msg )
 {
     MsgType type = (MsgType)msg.PeekByte();
     switch ( type )
     {
         case MsgType.Debug:
             msg.ReadByte();
             string output = "[Dbg Msg From " + NetUtility.ToHexString( msg.SenderConnection.RemoteUniqueIdentifier ) + "]: "
                 + msg.ReadString();
             WriteConsoleMessage( output );
             break;
         case MsgType.ReqLobbyList:
             SendLobbyList( msg.SenderConnection );
             break;
         case MsgType.ReqJoinLobby:
             msg.ReadByte();
             int lobbyID = msg.ReadByte();
             string playerName = msg.ReadString();
             MsgErrorCode err;
             if ( ! AddPlayerToLobby( msg.SenderConnection, lobbyID, playerName, out err ) )
             {
                 NetOutgoingMessage errMsg = server.CreateMessage();
                 errMsg.Write( (byte)MsgType.Error );
                 errMsg.Write( (byte)err );
                 server.SendMessage( errMsg, msg.SenderConnection, NetDeliveryMethod.Unreliable );
             }
             else
             {
                 playerLobbies[msg.SenderConnection.RemoteUniqueIdentifier] = lobbyID;
             }
             break;
         case MsgType.ReqLeaveLobby:
             if ( playerLobbies.ContainsKey( msg.SenderConnection.RemoteUniqueIdentifier ) )
             {
                 int playerLobby = playerLobbies[msg.SenderConnection.RemoteUniqueIdentifier];
                 if ( playerLobby != NO_LOBBY )
                 {
                     lock ( lobbyLock )
                     {
                         lobbies[playerLobby].RemovePlayer( msg.SenderConnection );
                     }
                 }
             }
             break;
         case MsgType.ReqCreateLobby:
             msg.ReadByte();
             string lName = msg.ReadString();
             int maxCap = msg.ReadInt32();
             double matchDur = msg.ReadDouble();
             string pName = msg.ReadString();
             ServiceLobbyCreateRequest( msg.SenderConnection, lName, pName, maxCap, matchDur );
             break;
         default:
             int pLobby = playerLobbies[msg.SenderConnection.RemoteUniqueIdentifier];
             if ( pLobby != NO_LOBBY )
             {
                 lobbies[pLobby].messageQueue.Enqueue( msg );
             }
             break;
     }
 }
Example #4
0
 private void HandleDataPacket( NetIncomingMessage msg )
 {
     MsgType type = (MsgType)msg.PeekByte();
     switch ( type )
     {
         case MsgType.Debug:
             msg.ReadByte();
             string output = "[Dbg Msg From Server]: " + msg.ReadString();
             System.Console.WriteLine( output );
             break;
         default:
             int numReceivers = packetReceivers.Count;
             for ( int i = 0; i < numReceivers; ++i )
             {
                 packetReceivers[i].ReceivePacket( msg );
                 msg.Position = 0;	// Reset the message for the next guy
             }
             break;
     }
 }
Example #5
0
        public void Act()
        {
            double time = sw.ElapsedMilliseconds / timeDivider;
            sw.Reset(); // reset the timer (change current time to 0)
            sw.Start();
            OutStrCmd = String.Empty;
            if (TimerWait.ElapsedMilliseconds / 1000 > 10)
            {
                AnalyzeMsg();
                TimerWait.Reset();
                DG = false;
            }
            while ((inMsg = server.ReadMessage()) != null)
            {
                switch (inMsg.MessageType)
                {
                    case NetIncomingMessageType.Data:

                        AnalyzeMsg();
                        break;

                    case NetIncomingMessageType.StatusChanged:
                          if (((NetConnectionStatus)(inMsg.PeekByte())).ToString() != "RespondedConnect" &&
                               ((NetConnectionStatus)(inMsg.PeekByte())).ToString() != "Connected")
                          {
                              Console.WriteLine(((NetConnectionStatus)(inMsg.ReadByte())).ToString());
                              Console.ReadLine();
                          }

                        break;

                }
                server.Recycle(inMsg);
            }
            UpdateUnits(time);
        }
Example #6
0
        public void ReadPacket(NetIncomingMessage msg)
        {
            switch (msg.PeekByte()) {
                case Packet.CLIENTINFO:
                    if (clientList.ContainsKey(msg.SenderConnection)) {
                        NetworkClient c = clientList[msg.SenderConnection];
                        ClientInfo ci = ClientInfo.decode(ref msg);
                        Console.WriteLine("Client " + c.Pseudo + " changed pseudo for " + ci.Pseudo);
                        c.Pseudo = ci.Pseudo;
                        c.Player.Color = ci.Color;
                        SendPktToAll(ci);
                    }
                    break;

                case Packet.USERMESSAGE:
                    UserMessage uMsg = UserMessage.decode(ref msg);
                    if (clientList.ContainsKey(msg.SenderConnection)) {
                        NetworkClient c = clientList[msg.SenderConnection];
                        c.Player.Layer = uMsg.Layer;
                        c.Player.Noclip = uMsg.Nolcip;
                        c.Player.Update((float)(uMsg.Time - c.LastUpdate).TotalSeconds, uMsg.Input);
                        c.LastUpdate = uMsg.Time;
                        uMsg.Position = c.Player.Position;
                        SendPktToAll(uMsg, true);
                    }
                    break;

                case Packet.BLOCKUPDATE:
                    BlockUpdate bu = BlockUpdate.decode(ref msg);

                    if ((bu.Added && map.AddCube(bu.Position, bu.BlockType, bu.Layer)) || !bu.Added) {
                        SendPktToAll(bu);
                    }

                    if (!bu.Added)
                        map.DeleteCube(bu.Position, bu.Layer);

                    break;

                case Packet.CLIENTRESET:
                    if (clientList.ContainsKey(msg.SenderConnection)) {
                        NetworkClient c = clientList[msg.SenderConnection];
                        c.Player.Reset();
                    }
                    break;
                case Packet.CHATMESSAGE:
                    ChatMessage cm = ChatMessage.decode(ref msg);
                    SendPktToAll(cm, true);
                    Console.WriteLine(cm.Pseudo + ": " + cm.Message);
                    break;

                default:
                    Console.WriteLine("Unsupported packet recieved");
                    break;
            }
        }