Ejemplo n.º 1
0
        private void ParseFirstClientMsg()
        {
            try
            {
                clientRecvMsg.Position = 6;
                byte protocolId = clientRecvMsg.GetByte();

                switch (protocolId)
                {
                case 0x01:
                    protocol = Protocol.Login;
                    break;

                case 0x0A:
                    protocol = Protocol.World;
                    break;

                default:
                    break;
                }
            }
            catch (Exception ex)
            {
                WriteDebug(ex.Message + "\nStackTrace: " + ex.StackTrace);
            }
        }
Ejemplo n.º 2
0
        private void ParseFirstClientMsg()
        {
            try
            {
                clientRecvMsg.Position = clientRecvMsg.GetPacketHeaderSize();

                OutgoingPacketType protocolId = (OutgoingPacketType)clientRecvMsg.GetByte();
                int position;

                switch (protocolId)
                {
                case OutgoingPacketType.LoginServerRequest:

                    protocol = Protocol.Login;
                    clientRecvMsg.GetUInt16();
                    ushort clientVersion = clientRecvMsg.GetUInt16();

                    clientRecvMsg.GetUInt32();
                    clientRecvMsg.GetUInt32();
                    clientRecvMsg.GetUInt32();

                    position = clientRecvMsg.Position;

                    clientRecvMsg.RsaOTDecrypt();

                    if (clientRecvMsg.GetByte() != 0)
                    {
                        Restart();
                        return;
                    }

                    xteaKey[0] = clientRecvMsg.GetUInt32();
                    xteaKey[1] = clientRecvMsg.GetUInt32();
                    xteaKey[2] = clientRecvMsg.GetUInt32();
                    xteaKey[3] = clientRecvMsg.GetUInt32();

                    if (Version.CurrentVersion >= 830)
                    {
                        clientRecvMsg.GetString();     // account name
                    }
                    else
                    {
                        clientRecvMsg.GetUInt32(); // account number
                    }
                    clientRecvMsg.GetString();     // password

                    if (isOtServer)
                    {
                        clientRecvMsg.RsaOTEncrypt(position);
                    }
                    else
                    {
                        clientRecvMsg.RsaCipEncrypt(position);
                    }

                    if (Version.CurrentVersion >= 830)
                    {
                        clientRecvMsg.AddAdler32();
                    }
                    clientRecvMsg.InsertPacketHeader();

                    serverTcp    = new TcpClient(loginServers[selectedLoginServer].Server, loginServers[selectedLoginServer].Port);
                    serverStream = serverTcp.GetStream();
                    serverStream.Write(clientRecvMsg.GetBuffer(), 0, clientRecvMsg.Length);
                    serverStream.BeginRead(serverRecvMsg.GetBuffer(), 0, 2, new AsyncCallback(ServerReadCallBack), null);
                    //clientStream.BeginRead(clientRecvMsg.GetBuffer(), 0, 2, new AsyncCallback(ClientReadCallBack), null);
                    break;

                case OutgoingPacketType.GameServerRequest:

                    protocol = Protocol.World;

                    clientRecvMsg.GetUInt16();
                    clientRecvMsg.GetUInt16();

                    position = clientRecvMsg.Position;

                    clientRecvMsg.RsaOTDecrypt();

                    if (clientRecvMsg.GetByte() != 0)
                    {
                        Restart();
                        return;
                    }

                    xteaKey[0] = clientRecvMsg.GetUInt32();
                    xteaKey[1] = clientRecvMsg.GetUInt32();
                    xteaKey[2] = clientRecvMsg.GetUInt32();
                    xteaKey[3] = clientRecvMsg.GetUInt32();

                    clientRecvMsg.GetByte();     // GM mode

                    if (Version.CurrentVersion >= 830)
                    {
                        clientRecvMsg.GetString();     // account name
                    }
                    else
                    {
                        clientRecvMsg.GetUInt32();     // account number
                    }

                    string characterName = clientRecvMsg.GetString();

                    clientRecvMsg.GetString();     // password

                    if (isOtServer)
                    {
                        clientRecvMsg.RsaOTEncrypt(position);
                    }
                    else
                    {
                        clientRecvMsg.RsaCipEncrypt(position);
                    }

                    if (Version.CurrentVersion >= 830)
                    {
                        clientRecvMsg.AddAdler32();
                    }
                    clientRecvMsg.InsertPacketHeader();

                    int index = GetSelectedIndex(characterName);

                    if (Version.CurrentVersion < 854)
                    {
                        serverTcp    = new TcpClient(BitConverter.GetBytes(charList[index].WorldIP).ToIPString(), charList[index].WorldPort);
                        serverStream = serverTcp.GetStream();
                    }

                    serverStream.Write(clientRecvMsg.GetBuffer(), 0, clientRecvMsg.Length);
                    serverStream.BeginRead(serverRecvMsg.GetBuffer(), 0, 2, new AsyncCallback(ServerReadCallBack), null);
                    clientStream.BeginRead(clientRecvMsg.GetBuffer(), 0, 2, new AsyncCallback(ClientReadCallBack), null);

                    break;

                default:
                    break;
                }
            }
            catch (Exception ex)
            {
                WriteDebug(ex.ToString());
                Restart();
            }
        }
Ejemplo n.º 3
0
        protected bool ParsePacketFromClient(Client client, NetworkMessage msg, NetworkMessage outMsg)
        {
            bool packetKnown = true;
            OutgoingPacket packet = null;
            OutgoingPacketType type = (OutgoingPacketType)msg.PeekByte();
            //System.Console.WriteLine(type.ToString());

            switch (type)
            {
                case OutgoingPacketType.ChannelClose:
                    packet = new Packets.Outgoing.ChannelClosePacket(client);
                    if (packet.ParseMessage(msg, PacketDestination.Server))
                    {
                        if (ReceivedChannelCloseOutgoingPacket != null)
                            packet.Forward = ReceivedChannelCloseOutgoingPacket.Invoke(packet);

                        if (packet.Forward)
                            packet.ToNetworkMessage(outMsg);
                    }
                    break;
                case OutgoingPacketType.ChannelOpen:
                    packet = new Packets.Outgoing.ChannelOpenPacket(client);
                    if (packet.ParseMessage(msg, PacketDestination.Server))
                    {
                        if (ReceivedChannelOpenOutgoingPacket != null)
                            packet.Forward = ReceivedChannelOpenOutgoingPacket.Invoke(packet);

                        if (packet.Forward)
                            packet.ToNetworkMessage(outMsg);
                    }
                    break;
                case OutgoingPacketType.PlayerSpeech:
                    packet = new Packets.Outgoing.PlayerSpeechPacket(client);
                    if (packet.ParseMessage(msg, PacketDestination.Server))
                    {
                        if (ReceivedPlayerSpeechOutgoingPacket != null)
                            packet.Forward = ReceivedPlayerSpeechOutgoingPacket.Invoke(packet);

                        if (packet.Forward)
                            packet.ToNetworkMessage(outMsg);
                    }
                    break;
                case OutgoingPacketType.Attack:
                    packet = new Packets.Outgoing.AttackPacket(client);
                    if (packet.ParseMessage(msg, PacketDestination.Server))
                    {
                        if (ReceivedAttackOutgoingPacket != null)
                            packet.Forward = ReceivedAttackOutgoingPacket.Invoke(packet);

                        if (packet.Forward)
                            packet.ToNetworkMessage(outMsg);
                    }
                    break;
                case OutgoingPacketType.Follow:
                    packet = new Packets.Outgoing.FollowPacket(client);
                    if (packet.ParseMessage(msg, PacketDestination.Server))
                    {
                        if (ReceivedFollowOutgoingPacket != null)
                            packet.Forward = ReceivedFollowOutgoingPacket.Invoke(packet);

                        if (packet.Forward)
                            packet.ToNetworkMessage(outMsg);
                    }
                    break;
                case OutgoingPacketType.LookAt:
                    packet = new Packets.Outgoing.LookAtPacket(client);
                    if (packet.ParseMessage(msg, PacketDestination.Server))
                    {
                        if (ReceivedLookAtOutgoingPacket != null)
                            packet.Forward = ReceivedLookAtOutgoingPacket.Invoke(packet);

                        if (packet.Forward)
                            packet.ToNetworkMessage(outMsg);
                    }
                    break;
                case OutgoingPacketType.ItemUse:
                    packet = new Packets.Outgoing.ItemUsePacket(client);
                    if (packet.ParseMessage(msg, PacketDestination.Server))
                    {
                        if (ReceivedItemUseOutgoingPacket != null)
                            packet.Forward = ReceivedItemUseOutgoingPacket.Invoke(packet);

                        if (packet.Forward)
                            packet.ToNetworkMessage(outMsg);
                    }
                    break;
                case OutgoingPacketType.ItemUseOn:
                    packet = new Packets.Outgoing.ItemUseOnPacket(client);
                    if (packet.ParseMessage(msg, PacketDestination.Server))
                    {
                        if (ReceivedItemUseOnOutgoingPacket != null)
                            packet.Forward = ReceivedItemUseOnOutgoingPacket.Invoke(packet);

                        if (packet.Forward)
                            packet.ToNetworkMessage(outMsg);
                    }
                    break;
                case OutgoingPacketType.ItemMove:
                    packet = new Packets.Outgoing.ItemMovePacket(client);
                    if (packet.ParseMessage(msg, PacketDestination.Server))
                    {
                        if (ReceivedItemMoveOutgoingPacket != null)
                            packet.Forward = ReceivedItemMoveOutgoingPacket.Invoke(packet);

                        if (packet.Forward)
                            packet.ToNetworkMessage(outMsg);
                    }
                    break;
                case OutgoingPacketType.CancelMove:
                    packet = new Packets.Outgoing.CancelMovePacket(client);
                    if (packet.ParseMessage(msg, PacketDestination.Server))
                    {
                        if (ReceivedCancelMoveOutgoingPacket != null)
                            packet.Forward = ReceivedCancelMoveOutgoingPacket.Invoke(packet);

                        if (packet.Forward)
                            packet.ToNetworkMessage(outMsg);
                    }
                    break;
                case OutgoingPacketType.ItemUseBattlelist:
                    packet = new Packets.Outgoing.ItemUseBattlelistPacket(client);
                    if (packet.ParseMessage(msg, PacketDestination.Server))
                    {
                        if (ReceivedItemUseBattlelistOutgoingPacket != null)
                            packet.Forward = ReceivedItemUseBattlelistOutgoingPacket.Invoke(packet);

                        if (packet.Forward)
                            packet.ToNetworkMessage(outMsg);
                    }
                    break;
                case OutgoingPacketType.Logout:
                    packet = new Packets.Outgoing.LogoutPacket(client);
                    if (packet.ParseMessage(msg, PacketDestination.Server))
                    {
                        if (ReceivedLogoutOutgoingPacket != null)
                            packet.Forward = ReceivedLogoutOutgoingPacket.Invoke(packet);

                        if (packet.Forward)
                            packet.ToNetworkMessage(outMsg);
                    }
                    break;
                case OutgoingPacketType.ContainerClose:
                    packet = new Packets.Outgoing.ContainerClosePacket(client);
                    if (packet.ParseMessage(msg, PacketDestination.Server))
                    {
                        if (ReceivedContainerCloseOutgoingPacket != null)
                            packet.Forward = ReceivedContainerCloseOutgoingPacket.Invoke(packet);

                        if (packet.Forward)
                            packet.ToNetworkMessage(outMsg);
                    }
                    break;
                case OutgoingPacketType.ContainerOpenParent:
                    packet = new Packets.Outgoing.ContainerOpenParentPacket(client);
                    if (packet.ParseMessage(msg, PacketDestination.Server))
                    {
                        if (ReceivedContainerOpenParentOutgoingPacket != null)
                            packet.Forward = ReceivedContainerOpenParentOutgoingPacket.Invoke(packet);

                        if (packet.Forward)
                            packet.ToNetworkMessage(outMsg);
                    }
                    break;
                case OutgoingPacketType.ShopBuy:
                    packet = new Packets.Outgoing.ShopBuyPacket(client);
                    if (packet.ParseMessage(msg, PacketDestination.Server))
                    {
                        if (ReceivedShopBuyOutgoingPacket != null)
                            packet.Forward = ReceivedShopBuyOutgoingPacket.Invoke(packet);

                        if (packet.Forward)
                            packet.ToNetworkMessage(outMsg);
                    }
                    break;
                case OutgoingPacketType.ShopSell:
                    packet = new Packets.Outgoing.ShopSellPacket(client);
                    if (packet.ParseMessage(msg, PacketDestination.Server))
                    {
                        if (ReceivedShopSellOutgoingPacket != null)
                            packet.Forward = ReceivedShopSellOutgoingPacket.Invoke(packet);

                        if (packet.Forward)
                            packet.ToNetworkMessage(outMsg);
                    }
                    break;
                case OutgoingPacketType.TurnDown:
                    msg.GetByte();
                    packet = new Packets.Outgoing.TurnPacket(client, Tibia.Constants.Direction.Down);

                    if (ReceivedTurnOutgoingPacket != null)
                        packet.Forward = ReceivedTurnOutgoingPacket.Invoke(packet);

                    if (packet.Forward)
                        packet.ToNetworkMessage(outMsg);
                    break;
                case OutgoingPacketType.TurnUp:
                    msg.GetByte();
                    packet = new Packets.Outgoing.TurnPacket(client, Tibia.Constants.Direction.Up);

                    if (ReceivedTurnOutgoingPacket != null)
                        packet.Forward = ReceivedTurnOutgoingPacket.Invoke(packet);

                    if (packet.Forward)
                        packet.ToNetworkMessage(outMsg);

                    break;
                case OutgoingPacketType.TurnLeft:
                    msg.GetByte();
                    packet = new Packets.Outgoing.TurnPacket(client, Tibia.Constants.Direction.Left);

                    if (ReceivedTurnOutgoingPacket != null)
                        packet.Forward = ReceivedTurnOutgoingPacket.Invoke(packet);

                    if (packet.Forward)
                        packet.ToNetworkMessage(outMsg);

                    break;
                case OutgoingPacketType.TurnRight:
                    msg.GetByte();
                    packet = new Packets.Outgoing.TurnPacket(client, Tibia.Constants.Direction.Right);

                    if (ReceivedTurnOutgoingPacket != null)
                        packet.Forward = ReceivedTurnOutgoingPacket.Invoke(packet);

                    if (packet.Forward)
                        packet.ToNetworkMessage(outMsg);

                    break;
                case OutgoingPacketType.MoveDown:
                    msg.GetByte();
                    packet = new Packets.Outgoing.MovePacket(client, Tibia.Constants.Direction.Down);

                    if (ReceivedMoveOutgoingPacket != null)
                        packet.Forward = ReceivedMoveOutgoingPacket.Invoke(packet);

                    if (packet.Forward)
                        packet.ToNetworkMessage(outMsg);

                    break;
                case OutgoingPacketType.MoveDownLeft:
                    msg.GetByte();
                    packet = new Packets.Outgoing.MovePacket(client, Tibia.Constants.Direction.DownLeft);

                    if (ReceivedMoveOutgoingPacket != null)
                        packet.Forward = ReceivedMoveOutgoingPacket.Invoke(packet);

                    if (packet.Forward)
                        packet.ToNetworkMessage(outMsg);

                    break;
                case OutgoingPacketType.MoveDownRight:
                    msg.GetByte();
                    packet = new Packets.Outgoing.MovePacket(client, Tibia.Constants.Direction.DownRight);

                    if (ReceivedMoveOutgoingPacket != null)
                        packet.Forward = ReceivedMoveOutgoingPacket.Invoke(packet);

                    if (packet.Forward)
                        packet.ToNetworkMessage(outMsg);

                    break;
                case OutgoingPacketType.MoveLeft:
                    msg.GetByte();
                    packet = new Packets.Outgoing.MovePacket(client, Tibia.Constants.Direction.Left);

                    if (ReceivedMoveOutgoingPacket != null)
                        packet.Forward = ReceivedMoveOutgoingPacket.Invoke(packet);

                    if (packet.Forward)
                        packet.ToNetworkMessage(outMsg);

                    break;
                case OutgoingPacketType.MoveRight:
                    msg.GetByte();
                    packet = new Packets.Outgoing.MovePacket(client, Tibia.Constants.Direction.Right);

                    if (ReceivedMoveOutgoingPacket != null)
                        packet.Forward = ReceivedMoveOutgoingPacket.Invoke(packet);

                    if (packet.Forward)
                        packet.ToNetworkMessage(outMsg);

                    break;
                case OutgoingPacketType.MoveUp:
                    msg.GetByte();
                    packet = new Packets.Outgoing.MovePacket(client, Tibia.Constants.Direction.Up);

                    if (ReceivedMoveOutgoingPacket != null)
                        packet.Forward = ReceivedMoveOutgoingPacket.Invoke(packet);

                    if (packet.Forward)
                        packet.ToNetworkMessage(outMsg);

                    break;
                case OutgoingPacketType.MoveUpLeft:
                    msg.GetByte();
                    packet = new Packets.Outgoing.MovePacket(client, Tibia.Constants.Direction.UpLeft);

                    if (ReceivedMoveOutgoingPacket != null)
                        packet.Forward = ReceivedMoveOutgoingPacket.Invoke(packet);

                    if (packet.Forward)
                        packet.ToNetworkMessage(outMsg);

                    break;
                case OutgoingPacketType.MoveUpRight:
                    msg.GetByte();
                    packet = new Packets.Outgoing.MovePacket(client, Tibia.Constants.Direction.UpRight);

                    if (ReceivedMoveOutgoingPacket != null)
                        packet.Forward = ReceivedMoveOutgoingPacket.Invoke(packet);

                    if (packet.Forward)
                        packet.ToNetworkMessage(outMsg);

                    break;
                case OutgoingPacketType.AutoWalk:
                    packet = new Packets.Outgoing.AutoWalkPacket(client);

                    if (packet.ParseMessage(msg, PacketDestination.Server))
                    {
                        if (ReceivedAutoWalkOutgoingPacket != null)
                            packet.Forward = ReceivedAutoWalkOutgoingPacket.Invoke(packet);

                        if (packet.Forward)
                            packet.ToNetworkMessage(outMsg);
                    }
                    break;
                case OutgoingPacketType.VipAdd:
                    packet = new Packets.Outgoing.VipAddPacket(client);

                    if (packet.ParseMessage(msg, PacketDestination.Server))
                    {
                        if (ReceivedVipAddOutgoingPacket != null)
                            packet.Forward = ReceivedVipAddOutgoingPacket.Invoke(packet);

                        if (packet.Forward)
                            packet.ToNetworkMessage(outMsg);
                    }
                    break;
                case OutgoingPacketType.VipRemove:
                    packet = new Packets.Outgoing.VipRemovePacket(client);

                    if (packet.ParseMessage(msg, PacketDestination.Server))
                    {
                        if (ReceivedVipRemoveOutgoingPacket != null)
                            packet.Forward = ReceivedVipRemoveOutgoingPacket.Invoke(packet);

                        if (packet.Forward)
                            packet.ToNetworkMessage(outMsg);
                    }
                    break;
                case OutgoingPacketType.ItemRotate:
                    packet = new Packets.Outgoing.ItemRotatePacket(client);

                    if (packet.ParseMessage(msg, PacketDestination.Server))
                    {
                        if (ReceivedItemRotateOutgoingPacket != null)
                            packet.Forward = ReceivedItemRotateOutgoingPacket.Invoke(packet);

                        if (packet.Forward)
                            packet.ToNetworkMessage(outMsg);
                    }
                    break;
                case OutgoingPacketType.SetOutfit:
                    packet = new Packets.Outgoing.SetOutfitPacket(client);

                    if (packet.ParseMessage(msg, PacketDestination.Server))
                    {
                        if (ReceivedSetOutfitOutgoingPacket != null)
                            packet.Forward = ReceivedSetOutfitOutgoingPacket.Invoke(packet);

                        if (packet.Forward)
                            packet.ToNetworkMessage(outMsg);
                    }
                    break;
                case OutgoingPacketType.AutoWalkCancel:
                    packet = new Packets.Outgoing.AutoWalkCancelPacket(client);

                    if (packet.ParseMessage(msg, PacketDestination.Server))
                    {
                        if (ReceivedAutoWalkCancelOutgoingPacket != null)
                            packet.Forward = ReceivedAutoWalkCancelOutgoingPacket.Invoke(packet);

                        if (packet.Forward)
                            packet.ToNetworkMessage(outMsg);
                    }
                    break;
                case OutgoingPacketType.Ping:
                    packet = new Packets.Outgoing.PingPacket(client);

                    if (packet.ParseMessage(msg, PacketDestination.Server))
                    {
                        if (ReceivedPingOutgoingPacket != null)
                            packet.Forward = ReceivedPingOutgoingPacket.Invoke(packet);

                        if (packet.Forward)
                            packet.ToNetworkMessage(outMsg);
                    }
                    break;
                case OutgoingPacketType.FightModes:
                    packet = new Packets.Outgoing.FightModesPacket(client);

                    if (packet.ParseMessage(msg, PacketDestination.Server))
                    {
                        if (ReceivedFightModesOutgoingPacket != null)
                            packet.Forward = ReceivedFightModesOutgoingPacket.Invoke(packet);

                        if (packet.Forward)
                            packet.ToNetworkMessage(outMsg);
                    }
                    break;
                case OutgoingPacketType.TileUpdate:
                    packet = new Packets.Outgoing.TileUpdatePacket(client);

                    if (packet.ParseMessage(msg, PacketDestination.Server))
                    {
                        if (ReceivedTitleUpdateOutgoingPacket != null)
                            packet.Forward = ReceivedTitleUpdateOutgoingPacket.Invoke(packet);

                        if (packet.Forward)
                            packet.ToNetworkMessage(outMsg);
                    }
                    break;
                case OutgoingPacketType.ShopClose:
                    packet = new Packets.Outgoing.ShopClosePacket(client);

                    if (packet.ParseMessage(msg, PacketDestination.Server))
                    {
                        if (ReceivedShopCloseOutgoingPacket != null)
                            packet.Forward = ReceivedShopCloseOutgoingPacket.Invoke(packet);

                        if (packet.Forward)
                            packet.ToNetworkMessage(outMsg);
                    }
                    break;
                case OutgoingPacketType.NpcChannelClose:
                    packet = new Packets.Outgoing.NpcChannelClosePacket(client);

                    if (packet.ParseMessage(msg, PacketDestination.Server))
                    {
                        if (ReceivedNpcChannelCloseOutgoingPacket != null)
                            packet.Forward = ReceivedNpcChannelCloseOutgoingPacket.Invoke(packet);

                        if (packet.Forward)
                            packet.ToNetworkMessage(outMsg);
                    }
                    break;
                case OutgoingPacketType.PrivateChannelOpen:
                    packet = new Packets.Outgoing.PrivateChannelOpenPacket(client);

                    if (packet.ParseMessage(msg, PacketDestination.Server))
                    {
                        if (ReceivedPrivateChannelOpenOutgoingPacket != null)
                            packet.Forward = ReceivedPrivateChannelOpenOutgoingPacket.Invoke(packet);

                        if (packet.Forward)
                            packet.ToNetworkMessage(outMsg);
                    }
                    break;
                case OutgoingPacketType.ChannelList:
                    packet = new Packets.Outgoing.ChannelListPacket(client);

                    if (packet.ParseMessage(msg, PacketDestination.Server))
                    {
                        if (ReceivedChannelListOutgoingPacket != null)
                            packet.Forward = ReceivedChannelListOutgoingPacket.Invoke(packet);

                        if (packet.Forward)
                            packet.ToNetworkMessage(outMsg);
                    }
                    break;
                default:
                    packetKnown = false;
                    break;
            }

            return packetKnown;
        }
Ejemplo n.º 4
0
        private void loadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "Smart AutoLooter Files (*.salf)|*.salf";
            dialog.Title = "Open File";

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                Tibia.Packets.NetworkMessage msg = new NetworkMessage(null, System.IO.File.ReadAllBytes(dialog.FileName));

                try
                {
                    ushort count = msg.GetUInt16();

                    for (int i = 0; i < count; i++)
                    {
                        LootItem lootItem = new LootItem(msg.GetUInt16(), msg.GetByte(), msg.GetString());
                        listBoxItems.Items.Add(lootItem);
                        _lootItems.Add(lootItem);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
Ejemplo n.º 5
0
        private void LoginServerReceived(IAsyncResult ar)
        {
            try
            {
                int dataLength = loginSocket.EndReceive(ar);
                if (dataLength > 0)
                {
                    byte[] tmp = new byte[dataLength];
                    Array.Copy(dataLoginServer, tmp, dataLength);
                    NetworkMessage msg = new NetworkMessage(tmp);
                    msg.PrepareToRead(xteaKey.ToUInt32Array());
                    msg.GetUInt16();
                    while (msg.Position < msg.Length)
                    {
                        byte cmd = msg.GetByte();
                        string message;
                        switch (cmd)
                        {
                            case 0x0A: //Error message
                                message = msg.GetString();
                                if (LoginServer_OnError != null)
                                    LoginServer_OnError(message);
                                break;
                            case 0x0B: //For your information
                                message = msg.GetString();
                                if (LoginServer_FYI != null)
                                    LoginServer_FYI(message);
                                break;
                            case 0x14: //MOTD
                                message = msg.GetString();
                                if (LoginServer_MOTD != null)
                                    LoginServer_MOTD(message);
                                break;
                            case 0x1E: //Patching exe/dat/spr messages
                            case 0x1F:
                            case 0x20:
                                if (LoginServer_Patching != null)
                                    LoginServer_Patching("A new client is available.");
                                return;
                            case 0x28: //Select another login server   
                                if (LoginServer_SelectAnother != null)
                                    LoginServer_SelectAnother("Select another login server.");
                                if (retry)
                                {
                                    if (loginServerIndex < maxLoginServers - 1)
                                    {
                                        loginServerIndex++;
                                        TryLoginServer();
                                    }
                                    else
                                    {
                                        if (LoginServer_CouldNotConnect != null)
                                            LoginServer_CouldNotConnect("Select another login server.");

                                        loginSocket.Disconnect(true);
                                        if (Socket_Disconnected != null)
                                            Socket_Disconnected("dataLength<=0");
                                    }
                                }
                                break;
                            case 0x64: //character list
                                int nChars = (int)msg.GetByte();
                                charList = new CharacterLoginInfo[nChars];

                                for (int i = 0; i < nChars; i++)
                                {
                                    charList[i].CharName = msg.GetString();
                                    charList[i].WorldName = msg.GetString();
                                    charList[i].WorldIP = msg.GetUInt32();
                                    charList[i].WorldIPString = IPBytesToString(BitConverter.GetBytes(charList[i].WorldIP), 0);
                                    charList[i].WorldPort = msg.GetUInt16();
                                }

                                if (LoginServer_CharList != null)
                                    LoginServer_CharList("Charlist received.");

                                loginSocket.Disconnect(true);
                                if (Socket_Disconnected != null)
                                    Socket_Disconnected("Charlist received.");
                                return;
                            default:
                                //Notify about an unknown message
                                if (LoginServer_UnknownMsg != null)
                                    LoginServer_UnknownMsg(msg.Data.ToHexString());

                                loginSocket.Disconnect(true);
                                if (Socket_Disconnected != null)
                                    Socket_Disconnected("Unknown Message.");
                                break;
                        }
                    }
                }
                else //we didn't receive anything
                {
                    if (LoginServer_ReceivedNothing != null)
                        LoginServer_ReceivedNothing("Nothing received on LoginServerIndex=" + loginServerIndex);
                    if (retry)
                    {
                        if (loginServerIndex < maxLoginServers - 1)
                        {
                            loginServerIndex++;
                            TryLoginServer();
                        }
                        else
                        {
                            if (LoginServer_CouldNotConnect != null)
                                LoginServer_CouldNotConnect("dataLength<=0");

                            loginSocket.Disconnect(true);
                            if (Socket_Disconnected != null)
                                Socket_Disconnected("dataLength<=0");
                        }
                    }
                }
            }
            catch (Exception)
            {
            }
        }