コード例 #1
0
ファイル: ZoneServer.cs プロジェクト: prologos/ZoneAgent562
        private void ZS_DataReceived(EventDrivenTCPClient sender, object data)
        {
            try
            {
                byte[]        buffer = (byte[])Convert.ChangeType(data, typeof(byte[]));
                List <byte[]> packetList;
                Packet.SplitPackets(buffer, buffer.Length, out packetList);
                foreach (byte[] sPacket in packetList)
                {
                    byte[] packet = sPacket;
                    Config.mConvert.Crypter.Decrypt(ref packet, ClientVer.v562);
                    MSG_HEAD_WITH_PROTOCOL pHeader = new MSG_HEAD_WITH_PROTOCOL();
                    pHeader.Deserialize(ref packet);
                    if (ZoneAgent._Players.ContainsKey(pHeader.dwPCID))
                    {
                        Client client = ZoneAgent._Players[pHeader.dwPCID];

                        if (pHeader.byCtrl == 0x01 && pHeader.byCmd == 0xE1)
                        {
                            //Zone Change Packet
                            _Main.UpdateLogMsg(string.Format("{0} {1} user zone changed {2}->{3}", client.Account, client.Uid, client.ZoneStatus, packet[0x0A]));
                            client.ZoneStatus = packet[0x0A];
                            continue;
                        }
                        else if (pHeader.wProtocol == 0x1800)
                        {
                            MSG_S2C_SAY pSay = new MSG_S2C_SAY();
                            pSay.Deserialize(ref packet);
                            if (pSayType.Notice == (pSayType)pSay.bySayType && "NOTICE" == pSay.szSayPC && uint.MaxValue == pSay.dwSayPCID)
                            {
                                ZoneAgent.szNotice = pSay.szWords;
                            }
                        }
                        else if (pHeader.wProtocol == 0x1107)
                        {
                            //world login: save character name and town
                            MSG_S2C_WORLD_LOGIN wLogin = new MSG_S2C_WORLD_LOGIN();
                            wLogin.Deserialize(ref packet);
                            client.Character = wLogin.szPCName;
                            client.Town      = wLogin.byTown;
                        }
                        //encrypt후 클라이언트로 send
                        //Config.mConvert.Convert_S2C(ref packet, client.Ver);
#if DEBUG
                        PacketLogger.LogPacket(packet, "server_to_client", client.Character);
#endif
                        ZoneAgent.Write(ref client, packet);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Write(string.Format("ZS_DataReceived: {0}", ex));
            }
        }
コード例 #2
0
        private void OnDataRead(IAsyncResult asyncResult)
        {
            Client client = asyncResult.AsyncState as Client;

            try
            {
                if (client == null)
                {
                    return;
                }
                NetworkStream networkStream = client.NetworkStream;
                IPEndPoint    newClientEp   = (IPEndPoint)client.TcpClient.Client.RemoteEndPoint;
                int           readSize      = networkStream.EndRead(asyncResult);
                if (readSize < 10)
                {
                    client.TcpClient.Client.Dispose();
                    return;
                }
                //클라가 보내는 처음 패킷(로그인 요청) 사이즈로 클라 버전 확인
                if (client.Ver == ClientVer.undefined)
                {
                    client.Ver = (ClientVer)readSize;
                }

                List <byte[]> packetList;
                Packet.SplitPackets(client.Buffer, readSize, out packetList);
                foreach (byte[] sPacket in packetList)
                {
                    byte[] packet = sPacket;
                    MSG_HEAD_WITH_PROTOCOL pHeader = new MSG_HEAD_WITH_PROTOCOL();
                    pHeader.Deserialize(ref packet);
                    switch (pHeader.byCtrl)
                    {
                    case 0x01:
                        switch (pHeader.byCmd)
                        {
                        case 0xE2:         // Account Login
                            MSG_CL2ZA_LOGIN nPlayer = new MSG_CL2ZA_LOGIN();
                            nPlayer.Deserialize(ref packet);
                            if (pHeader.dwPCID == nPlayer.dwPCID)
                            {
                                //유저 로그인은 새 쓰레드에서 처리
                                new Thread(() => isPreparedUser(ref client, nPlayer.dwPCID, nPlayer.szAccount, newClientEp.Address.ToString())).Start();
                            }
                            else
                            {
                                client.TcpClient.Client.Dispose();
                            }
                            break;

                        case 0xF0:         //TimeTick - Check Speed Hack
                            if (_Players.ContainsKey(client.Uid))
                            {
                                new Thread(() => CheckTimeTick(packet, ref client)).Start();
                            }
                            else
                            {
                                client.TcpClient.Client.Dispose();
                            }
                            break;

                        default:
                            Logger.NewPacket("CL->ZA", packet, client.IPadress, client.Ver);
                            client.TcpClient.Client.Dispose();
                            break;
                        }
                        break;

                    case 0x03:
                        if (_Players.ContainsKey(client.Uid))
                        {
                            //Convert: add uid, decrypt, if v578 -> v219(v562) type
                            Config.mConvert.Convert_C2S(ref packet, client.Uid, client.Ver, client.IPadress);
#if DEBUG
                            PacketLogger.LogPacket(packet, "client_to_server", client.Character);
#endif
                            pHeader.Deserialize(ref packet);

                            switch (pHeader.wProtocol)
                            {
                            case 0x1106:         //Character login
                            case 0x2322:         //Transfer Clan Mark
                            case 0x2323:         //Clan...
                            case 0xA001:         //Create Character
                            case 0xA002:         //Delete Character
                                //보내기전 패킷 암호화, ZS에 보내는 패킷은 562버전으로
                                Config.mConvert.Crypter.Encrypt(ref packet, ClientVer.v562);
                                ZoneServer.ZS[Config.AS_ID].Send(packet);
                                break;

                            case 0x1112:         //Teleport packet
                                new Thread(() => isPossibleTeleport(packet, ref client)).Start();
                                break;

                            case 0x1108:         //logout
                                //AS에 있을때는 cl->za->as : as에서 답 없음
                                //ZS에 있을때는 1108답장 있음 ->cl
                                client.Reason = 0;
                                Config.mConvert.Crypter.Encrypt(ref packet, ClientVer.v562);
                                ZoneServer.ZS[client.ZoneStatus].Send(packet);
                                break;

                            default:
                                //보내기전 패킷 암호화, ZS에 보내는 패킷은 562버전으로
                                Config.mConvert.Crypter.Encrypt(ref packet, ClientVer.v562);
                                ZoneServer.ZS[client.ZoneStatus].Send(packet);
                                break;
                            }
                        }
                        else
                        {
                            client.TcpClient.Client.Dispose();
                        }
                        break;

                    default:
                        Logger.NewPacket("CL->ZA", packet, client.IPadress, client.Ver);
                        client.TcpClient.Client.Dispose();
                        break;
                    }
                }
                networkStream.BeginRead(client.Buffer, 0, client.Buffer.Length, OnDataRead, client);
            }
            catch (Exception ex)
            {
#if DEBUG
                Logger.Write(string.Format("OnDataRead: {0}", ex));
#endif
            }
        }