示例#1
0
 public Packet ToPacket()
 {
     var packet = new Packet(_id);
     packet.Data = new byte[_currentWriteOfset];
     Buffer.BlockCopy(_buffer, 0, packet.Data, 0, _currentWriteOfset);
     return packet;
 }
示例#2
0
        public void PacketDecoder()
        {
            if (ChuckedRawPacket.Count > 0)
            {
                Packet p = new Packet();

                int packetsize = ChuckedRawPacket[1];

                lock (this)
                {
                    ChuckedRawPacket.TrimExcess();

                    p.Id = (PacketId)ChuckedRawPacket[0];
                    p.Data = ChuckedRawPacket.GetRange(2, packetsize - 2).ToArray();

                    ChuckedRawPacket.RemoveRange(0, packetsize);
                }

                queuedPackets.Enqueue(p);
                PacketDecoder();
            }
        }
示例#3
0
        public void SendPacket(Packet packet)
        {
            try
            {
                if (socket.Connected)
                {
                    lock (socket)
                    {
                        if (packet.Data == null)
                            return;

                        byte[] buffer = new byte[packet.Data.Length + 2];
                        buffer[0] = (byte)packet.Id;
                        buffer[1] = (byte)(packet.Data.Length + 2);

                        Buffer.BlockCopy(packet.Data, 0, buffer, 2, packet.Data.Length);

                        int bytesend = socket.Send(buffer);
                    }
                }
            }
            catch (Exception) { }
        }
示例#4
0
 public PacketBuilder(Packet p)
 {
     Buffer.BlockCopy(p.Data, 0, _buffer, 0, p.Data.Length);
     _id = p.Id;
 }