示例#1
0
 public async Task ExecuteHandler(RecvOpcode opcode, Client client, PacketReader reader)
 {
     PacketHandler handler;
     if (handlers.TryGetValue(opcode, out handler))
     {
         await handler(client, reader);
     }
     else throw new Exception("No handler found for: " + ((byte)opcode).ToString("X2"));
 }
示例#2
0
        public void BeginRead(IAsyncResult asyncResult)
        {
            try
            {
                int received = Stream.EndRead(asyncResult);
                if (received != 0)
                {
                    while (received >= 6)
                    {
                        int length = BitConverter.ToUInt16(buffer, 0) & 0x7FFF;

                        byte[] temp = new byte[length + 2];
                        Array.Copy(buffer, 2, temp, 0, temp.Length);

                        int bits = Id % 7 + 1;
                        BitShift.Unshift(temp, bits);

                        byte[]     opcode = new byte[] { temp[0], temp[1] };
                        RecvOpcode packet = (RecvOpcode)BitConverter.ToUInt16(opcode, 0);
                        Type       t      = Type.GetType("PiercingBlow.Login.Network.Recv." + packet.ToString());

                        if (t != null)
                        {
                            ClientPacket clientpacket = (ClientPacket)Activator.CreateInstance(t);
                            clientpacket.Client = this;
                            clientpacket.Init(temp);
                            //clientpacket.Process(temp);
                        }
                        else
                        {
                            Log.Info("PacketId = {0}", BitConverter.ToUInt16(opcode, 0), buffer.Length);
                            Log.Trace(temp.ToHex());
                        }
                        received -= length + 4;
                        Array.Copy(buffer, length + 4, buffer, 0, received); // << Копируем оставшиеся данные в начало буфера
                    }
                    Stream.BeginRead(buffer, 0, buffer.Length, BeginRead, Stream);
                }
                else
                {
                    OnDisconnected(this);
                }
            }
            catch (IOException)
            {
                OnDisconnected(this);
            }
            catch (Exception ex)
            {
                OnDisconnected(this);
                //Log.Error(ex);
                Log.Error($"{ex.Message}\n{ex.StackTrace}");
            }
        }
示例#3
0
        public async Task ExecuteHandler(RecvOpcode opcode, Client client, PacketReader reader)
        {
            PacketHandler handler;

            if (handlers.TryGetValue(opcode, out handler))
            {
                await handler(client, reader);
            }
            else
            {
                throw new Exception("No handler found for: " + ((byte)opcode).ToString("X2"));
            }
        }
示例#4
0
 public void StartHandlers()
 {
     if (!handling)
     {
         handling = true;
         Task.Factory.StartNew(async() =>
         {
             while (connected)
             {
                 try
                 {
                     RecvOpcode opcode = await reader.ReadOpcode();
                     await PacketManager.Instance.ExecuteHandler(opcode, this, reader);
                 }
                 catch (Exception ex)
                 {
                     Console.WriteLine(ex.Message);
                     Disconnect();
                 }
             }
         });
         timer.Start();
     }
 }
示例#5
0
 public PacketHandlerAttribute(RecvOpcode opcode)
 {
     this.Opcode = opcode;
 }
 public PacketHandlerAttribute(RecvOpcode opcode)
 {
     this.Opcode = opcode;
 }