public static ProtocolPacket Construct(byte[] packet, int start, int length) { MemoryStream stream = new MemoryStream(packet, start, length); BinaryReader reader = new BinaryReader(stream); return(ProtocolPacket.ReadRequest(reader)); }
public static ProtocolPacket ReadRequest(BinaryReader reader, ProtocolHeader header) { ProtocolPacket packet = null; switch (header.OpCode) { case CommandOpCode.Get: case CommandOpCode.GetK: case CommandOpCode.GetKQ: if (header.Magic == ProtocolPacket.RequestMagic) { packet = new GetRequest(header); } else if (header.Magic == ProtocolPacket.ResponseMagic) { packet = new GetResponse(header); } break; case CommandOpCode.Set: case CommandOpCode.SetQ: if (header.Magic == ProtocolPacket.RequestMagic) { packet = new SetRequest(header); } else if (header.Magic == ProtocolPacket.ResponseMagic) { packet = new SetResponse(header); } break; } if (packet != null) { packet.Read(reader); if (!packet.Validate()) { throw new ProtocolException() { Status = ResponseStatus.InvalidArguments }; } } else { throw new ProtocolException() { Status = ResponseStatus.UnknownCommand }; } return(packet); }
public static ProtocolPacket ReadRequest(BinaryReader reader) { return(ProtocolPacket.ReadRequest(reader, ProtocolHeader.ReadHeader(reader))); }