예제 #1
0
        public static void SendResponseSelectChar(ClientManager MyClient, byte hero_order)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                int len = 0;
                using (BinaryWriter bw = new BinaryWriter(stream, Encoding.UTF8))
                {
                    bw.Write((byte)0x06); // packet type
                    bw.Write((byte)0x00); // response type


                    IPAddress ip       = IPAddress.Parse("127.0.0.1");
                    byte[]    ip_bytes = ip.GetAddressBytes();

                    bw.Write(ip_bytes);     // ZoneServer IP

                    bw.Write((short)20165); // ZoneServer port
                    bw.Write((byte)0x6b);   // ZoneServer public cripto

                    Random rd    = new Random();
                    int    Token = rd.Next(0xFFFFFF);
                    bw.Write((Int32)Token);

                    // Send token to ZoneServer
                    int id_idx = MyClient.data.id_idx;



                    // verify this values

                    // send to zs
                    ZS_Bind.SendToken(id_idx, Token, hero_order);
                    len = (int)bw.BaseStream.Length;
                }
                byte[] buffer = stream.GetBuffer();
                Array.Resize(ref buffer, len);
                MakePacketAndSend(MyClient, buffer);

                ClientFunctions.DisconnectClientFromID(MyClient.id);
            }
        }
예제 #2
0
        private static void ReceiveCallback(IAsyncResult ar)
        {
            byte[]        Data;
            ClientManager MyClient = (ClientManager)ar.AsyncState;
            Socket        client   = MyClient._socket;


            if (client == null)
            {
                return;
            }
            if (!client.Connected)
            {
                return;
            }
            int BufferSize = getPendingByteCount(client);

            if (BufferSize > 1000)
            {
                return;
            }

            try
            {
                int BytesReceive = client.EndReceive(ar);
                if (BytesReceive > 0)
                {
                    Data = new byte[BytesReceive];
                    Array.Copy(MyClient.buffer, Data, BytesReceive);
                    ReceiveData.ParsePacket(MyClient, Data);
                    MyClient.buffer = new byte[ClientManager.BufferSize];

                    // Process received data
                }
                else
                {
                    ClientFunctions.DisconnectClientFromID(MyClient.id);
                    ClientFunctions.RemoveClientFromInstance(MyClient);
                }
                client.BeginReceive(MyClient.buffer, 0, ClientManager.BufferSize, SocketFlags.None, new AsyncCallback(ReceiveCallback), MyClient);
            }
            catch (SocketException e)
            {
                if (e.ErrorCode == 10054)
                {
                    ClientFunctions.DisconnectClientFromID(MyClient.id);
                    ClientFunctions.RemoveClientFromInstance(MyClient);
                    //Console.WriteLine("Client disconnected!");
                    return;
                }
            }
            catch
            {
                ClientFunctions.DisconnectClientFromID(MyClient.id);
                ClientFunctions.RemoveClientFromInstance(MyClient);
                //Console.WriteLine("Client disconnected!");
                // add to log receive error
                // remove client connected if is a error: remove from clients list
                //
                return;
            }
        }