private MCFunction CreateFucntion(MCFunction requestFunction, PacketDirection direction, MCQHeader qHeader, MCDataProcessor dataSource) { if (direction == PacketDirection.Request) { var cc = dataSource.ReadUShort(); var sc = dataSource.ReadUShort(); var functionRegistration = MCFunctionRegistry.Find(cc); if (functionRegistration != null) { return(functionRegistration.Generate(direction, sc, requestFunction)); } } else if (direction == PacketDirection.Response) { var qHeaderResponse = (MCQHeaderResponse)qHeader; if (qHeaderResponse.ResultCode == 0) { var functionRegistration = MCFunctionRegistry.Find(requestFunction.GetType()); if (functionRegistration != null) { return(functionRegistration.Generate(direction, requestFunction.GetSubCommandCode(), requestFunction)); } } else { return(new MCFunctionErrorResponse()); } } return(null); }
public MCPacket ReadPacket(Stream stream, MCFunction requestFunction) { var packet = new MCPacket(); var source = this.CreateDataProcessor(stream); source.ForceBigEndian = true; var frameId = source.ReadUShort(); source.ForceBigEndian = false; var frame = MCFrame.Find(frameId); var direction = frame.GetDirection(frameId); var subHeader = MCSubHeader.Find(frame).CreateInstance(); subHeader.Read(source); packet.SubHeader = subHeader; var qHeader = this.CreateQHeader(direction); qHeader.ReadPre(source); packet.QHeader = qHeader; ushort dataLength = (ushort)(source.ReadUShort() / source.LengthPerByte); var data = source.ReadBytes(dataLength); using (var ms = new MemoryStream(data)) { var dataSource = this.CreateDataProcessor(ms); dataSource.IgnoreConvert = true; qHeader.ReadPost(dataSource); var function = this.CreateFucntion(requestFunction, direction, qHeader, dataSource); if (function != null) { function.Read(dataSource); } packet.Function = function; } return(packet); }
public abstract MCFunction Generate(PacketDirection direction, ushort subCommandCode, MCFunction request);