public static MyPacket BytesToPacket(byte[] bytes) { MyPacket packet = new MyPacket { PacketLength = BitConverter.ToInt32(bytes, 0), ID = BitConverter.ToInt32(bytes, 4), SourceAddress = new IPAddress(new byte[] { bytes[8], bytes[9], bytes[10], bytes[11] }), DestinationAddress = new IPAddress(new byte[] { bytes[12], bytes[13], bytes[14], bytes[15] }), Frequency = BitConverter.ToInt16(bytes, 16), Bandwith = BitConverter.ToInt16(bytes, 20), BitRate = BitConverter.ToInt16(bytes, 24), Performance = BitConverter.ToInt16(bytes, 26), Port = BitConverter.ToInt32(bytes, 28) }; packet.slotWindow.FirstSlot = BitConverter.ToInt32(bytes, 32); packet.slotWindow.NumberofSlots = BitConverter.ToInt32(bytes, 36); var usefulPayload = new List <byte>(); usefulPayload.AddRange(bytes.ToList() .GetRange(40, packet.PacketLength - PacketHeaderLength)); packet.Payload = Encoding.ASCII.GetString(usefulPayload.ToArray()); return(packet); }
public MyPacket Receive() { byte[] receiveBuffer = new byte[256]; int receivedBytes = Receive(receiveBuffer); if (Encoding.ASCII.GetString(receiveBuffer, 0, receivedBytes).Substring(0, 1).Equals("C")) { return(null); } return(MyPacket.BytesToPacket(receiveBuffer)); }
public int Send(MyPacket packet) { return(Send(packet.PacketToBytes())); }