public static DiscoveryPacket ReadPacket(byte[] data) { DiscoveryPacket res = new DiscoveryPacket(); res.IsValid = false; try { using (BinaryReader buf = new BinaryReader(new MemoryStream(data))) { ushort shortHeaderValue = buf.ReadUInt16(); if (shortHeaderValue != BBProtocol.PROTOCOL_HEADER) { res.ParseError = "Protocol header mismatch, not a packet we are looking for"; return(res); } ushort nPacketSize = buf.ReadUInt16(); byte byOpcode = buf.ReadByte(); if (nPacketSize > buf.BaseStream.Length - buf.BaseStream.Position) { res.ParseError = "Packet size smaller than expected, discard"; return(res); } else if (nPacketSize > BBProtocol.MAX_DISCPACKETSIZE_WITHOUTHEADER) { res.ParseError = "Packet size above max, discard"; return(res); } switch (byOpcode) { case BBProtocol.OP_DISCOVERY_ANSWER: { res.m_byOpcode = byOpcode; DiscoveryPacket_Answer dpAnswer = new DiscoveryPacket_Answer(res); dpAnswer.ProcessData(buf); return(dpAnswer); } default: { res.ParseError = "Opcode unsupported"; return(res); } } } } catch (IOException e) { res.ParseError = "Exception while reading packet: " + e.Message; return(res); } }
protected DiscoveryPacket(DiscoveryPacket src) { IsValid = src.IsValid; m_byOpcode = src.m_byOpcode; ParseError = src.ParseError; }
internal DiscoveryPacket_Answer(DiscoveryPacket src) : base(src) { }