コード例 #1
0
        private void ProcessPackets(byte[] payloadData)
        {
            try
            {
                //get packetlength
                byte[] tmparray = new byte[2];
                tmparray[0] = payloadData[0];
                tmparray[1] = payloadData[1];
                ushort packetLength = BitConverter.ToUInt16(tmparray, 0);

                if (payloadData.Length >= packetLength)
                {
                    byte spacer = payloadData[2]; // skip 1 byte

                    byte[] packetData = new byte [payloadData.Length - 3];
                    //payloadData.CopyTo(packetData, 3);
                    Array.Copy(payloadData, 3, packetData, 0, packetLength - 3);
                    DecryptPacket(packetData);
                    byte[] packetBytes = packetData;

                    //Get packet id
                    L2RPacket packetReader = new L2RPacket(packetData);
                    ushort    packetId     = (ushort)(packetReader.ReadUInt16() - 1);
                    Console.Out.WriteLineAsync("-Packet ID: " + packetId + "\r");

                    PacketFactory factory = new ConcretePacketFactory();
                    IL2RPacket    l2rpckt = factory.GetPacket(packetId, packetReader);

                    //FIRE L2RPacketArrivalEvent
                    L2RPacketArrivalEventArgs args = new L2RPacketArrivalEventArgs
                    {
                        ID          = packetId,
                        Packet      = l2rpckt,
                        PacketBytes = packetBytes
                    };
                    OnL2RPacketArrival(args);


                    packetQue.Enqueue(l2rpckt);
                }
            }
            catch (Exception ex)
            {
                _incomingBuffer.Clear();
                Console.WriteLine(ex.ToString());
            }
        }
コード例 #2
0
        private void ProcessPackets(byte[] payloadData)
        {
            try
            {
                if (payloadData == null || payloadData.Length < 2)
                {
                    return;
                }

                //get packetlength
                byte[] tmparray = new byte[2];
                tmparray[0] = payloadData[0];
                tmparray[1] = payloadData[1];
                ushort packetLength = BitConverter.ToUInt16(tmparray, 0);

                if (payloadData.Length >= packetLength)
                {
                    byte spacer = payloadData[2]; // skip 1 byte

                    byte[] packetData = new byte [payloadData.Length - 3];
                    //payloadData.CopyTo(packetData, 3);
                    Array.Copy(payloadData, 3, packetData, 0, packetLength - 3);
                    DecryptPacket(packetData);
                    byte[] packetBytes = packetData;

                    //Get packet id
                    L2RPacket packetReader = new L2RPacket(packetData);
                    ushort    packetId     = (ushort)(packetReader.ReadUInt16() - 1);

                    PacketFactory factory = new ConcretePacketFactory();
                    IL2RPacket    l2rpckt = factory.GetPacket(packetId, packetReader);

                    /*** Fixed Queue ***/

                    _triggerEvent = true;

                    foreach (var id in _packetIdQueue)
                    {
                        if (id == packetId)
                        {
                            _triggerEvent = false;
                        }
                    }

                    _packetIdQueue.Enqueue(packetId);

                    if (_triggerEvent)
                    {
                        //FIRE L2RPacketArrivalEvent
                        L2RPacketArrivalEventArgs args = new L2RPacketArrivalEventArgs
                        {
                            ID          = packetId,
                            Packet      = l2rpckt,
                            PacketBytes = packetBytes
                        };

                        OnL2RPacketArrival(args);

                        //packetQue.Enqueue(l2rpckt);
                    }

                    /*** end Fixed Queue ***/
                }
            }
            catch (Exception ex)
            {
                _incomingBuffer.Clear();

                PacketError?.Invoke(this, new L2RPacketErrorEventArgs(ex));
            }
        }