示例#1
0
        public void Main()
        {
            while (ShouldReceive)
            {
                try
                {
                    if (Stream == null)
                    {
                        return;
                    }

                    //the main function that receives packet data
                    byte[] buffer    = new byte[2];
                    int    readTotal = 0;
                    //reading the length of the packet
                    Stream.Read(buffer, 0, 2);
                    if (buffer[0] == 0)
                    {
                        continue;
                    }

                    byte length = buffer[0];
                    if (length < 2 && PACKET_DEBUG)
                    {
                        //we're not interested in any packets less than 2.
                        //Could happen if a client other than a DansWorld client tries to connect.
                        Logger.Error("Packet of length less than 2 received");
                        continue;
                    }

                    //initiating a new byte array of the packet length
                    byte[] data = new byte[length];

                    //while there's still data to read
                    while (readTotal < length)
                    {
                        //filling the byte array with the data read from the stream
                        int read = Stream.Read(data, 0, length);
                        if (read == 0 && PACKET_DEBUG)
                        {
                            Logger.Warn("Packet seems empty..");
                            return;
                        }
                        readTotal += read;
                    }

                    StringBuilder sb = new StringBuilder();
                    sb.Append("Packet Data: ");
                    for (int i = 0; i < data.Length; i++)
                    {
                        sb.Append("{" + data[i] + "} ");
                    }

                    if (PACKET_DEBUG)
                    {
                        Logger.Log(sb.ToString());
                    }

                    //building up the received packet data using the received byte array
                    PacketBuilder pb  = new PacketBuilder().AddBytes(data);
                    Packet        pkt = pb.Build();

                    //if we have an accepted login packet
                    if (pkt.Family == PacketFamily.LOGIN)
                    {
                        if (pkt.Action == PacketAction.ACCEPT)
                        {
                            //debug output
                            _gameClient.DisplayMessage("Login accepted");
                            //we're going to set the state of the client to logged in
                            _gameClient.SetState(GameExecution.GameState.LoggedIn);
                            //while we still have players to read the data of
                            while (pkt.ReadPosition < pkt.RawData.Length)
                            {
                                int nameLength = pkt.ReadByte();
                                if (nameLength > 0)
                                {
                                    PlayerCharacter c = new PlayerCharacter()
                                    {
                                        Name   = pkt.ReadString(nameLength),
                                        Level  = pkt.ReadByte(),
                                        Gender = (Gender)pkt.ReadByte()
                                    };
                                    _gameClient.AddPlayerCharacter(c);
                                }
                            }
                        }
                        else
                        {
                            //if the login wasn't accepted, the person should get the reason as to why
                            _gameClient.DisplayMessage(pkt.ReadString(pkt.Length - 2));
                        }
                    }
                    else if (pkt.Family == PacketFamily.REGISTER)
                    {
                        if (pkt.Action == PacketAction.ACCEPT)
                        {
                            //registration was complete, we can now go back to the main menu to log in to the created account
                            _gameClient.SetState(GameExecution.GameState.MainMenu);
                            _gameClient.DisplayMessage("Account created! Username: "******"" : p.Name));
                        }
                    }
                    //server sent an announcement
                    else if (pkt.Family == PacketFamily.SERVER)
                    {
                        if (pkt.Action == PacketAction.TALK)
                        {
                            string message = pkt.ReadString(pkt.ReadInt());
                            _gameClient.DisplayMessage(message, "SERVER");
                        }
                    }
                    //if we got a ping request, we'll send back a pong request to satisfy the server
                    else if (pkt.Family == PacketFamily.CONNECTION)
                    {
                        if (pkt.Action == PacketAction.PING)
                        {
                            PacketBuilder pBuilder   = new PacketBuilder(PacketFamily.CONNECTION, PacketAction.PONG);
                            DateTime      now        = DateTime.Now.ToUniversalTime();
                            DateTime      fromServer = DateTime.ParseExact(pkt.ReadString(pkt.ReadInt()), "hh.mm.ss.ffffff", CultureInfo.InvariantCulture);
                            TimeSpan      t          = now.Subtract(fromServer);
                            string        nowString  = now.ToShortTimeString();
                            pBuilder = pBuilder.AddInt(nowString.Length).AddString(nowString);
                            Send(pBuilder.Build());
                            _gameClient.ShowPing(t.Milliseconds);
                        }
                    }
                    else if (pkt.Family == PacketFamily.ENEMY)
                    {
                        //an enemy has been added to the game
                        if (pkt.Action == PacketAction.WELCOME)
                        {
                            Enemy enemy = new Enemy();
                            enemy.ID        = pkt.ReadInt();
                            enemy.Name      = pkt.ReadString(pkt.ReadByte());
                            enemy.Facing    = (Direction)pkt.ReadByte();
                            enemy.X         = pkt.ReadInt();
                            enemy.Y         = pkt.ReadInt();
                            enemy.Vitality  = pkt.ReadInt();
                            enemy.Level     = pkt.ReadByte();
                            enemy.MaxHealth = pkt.ReadInt();
                            enemy.Health    = pkt.ReadInt();
                            enemy.SpriteID  = pkt.ReadInt();
                            enemy.ServerID  = pkt.ReadByte();
                            _gameClient.AddEnemy(enemy);
                        }
                        //an enemy moved
                        else if (pkt.Action == PacketAction.MOVE)
                        {
                            int  id     = pkt.ReadInt();
                            int  x      = pkt.ReadInt();
                            int  y      = pkt.ReadInt();
                            byte facing = pkt.ReadByte();
                            foreach (Enemy enemy in _gameClient.GetEnemies())
                            {
                                if (enemy.ServerID == id)
                                {
                                    enemy.X      = x;
                                    enemy.Y      = y;
                                    enemy.Facing = (Direction)facing;
                                    break;
                                }
                            }
                        }
                        //an enemy took damage
                        else if (pkt.Action == PacketAction.TAKE_DAMAGE)
                        {
                            int id = pkt.ReadInt();
                            int hp = pkt.ReadInt();

                            foreach (Enemy enemy in _gameClient.GetEnemies())
                            {
                                if (enemy.ID == id)
                                {
                                    enemy.Health = hp;
                                    break;
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Logger.Error(e.Message + " Stack " + e.StackTrace);
                    continue;
                }
            }
        }