public Packet ReceivePacket(PacketID pid) { byte[] buff = new byte[0x10000]; while (true) { if (cliSkt.ReceiveBufferSize > 0) { try { cliSkt.Receive(buff); using (var rdr = new NReader(new MemoryStream(buff))) { int len = rdr.ReadInt32() - 5; byte id = rdr.ReadByte(); byte[] content = rdr.ReadBytes(len); ReceiveKey.Crypt(content, content.Length); Console.WriteLine((PacketID)id); if ((PacketID)id == pid) { return(new Packet((PacketID)id, content)); } } } catch { } } } }
public void SendPacket(Packet p) { MemoryStream s = new MemoryStream(); using (var wtr = new NWriter(s)) { byte[] content = p.body.ToArray(); byte[] ret = new byte[5 + content.Length]; content = SendKey.Crypt(content, content.Length); Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(ret.Length)), 0, ret, 0, 4); ret[4] = (byte)p.id; Buffer.BlockCopy(content, 0, ret, 5, content.Length); wtr.Write(ret); } cliSkt.Send(s.ToArray()); }