public Packet ToPacket() { var packet = new Packet(_id); packet.Data = new byte[_currentWriteOfset]; Buffer.BlockCopy(_buffer, 0, packet.Data, 0, _currentWriteOfset); return packet; }
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(); } }
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) { } }
public PacketBuilder(Packet p) { Buffer.BlockCopy(p.Data, 0, _buffer, 0, p.Data.Length); _id = p.Id; }