/// <summary> /// Reads a single single response from the stream and processes it. /// </summary> /// <param name="stream"></param> private void ProcessResponse(Stream stream) { Response response; long requestId = 0; if (WriteRequestIdInResponse) { byte[] requestIdBytes = new byte[sizeof(long)]; stream.Read(requestIdBytes, 0, requestIdBytes.Length); requestId = BitConverter.ToInt64(requestIdBytes, 0); } byte[] responseTypeBytes = new byte[2]; stream.Read(responseTypeBytes, 0, responseTypeBytes.Length); Response.Type responseType = (Response.Type)BitConverter.ToInt16(responseTypeBytes, 0); //Reading a response's header... byte[] cmdSzBytes = new byte[CmdSizeHolderBytesCount]; stream.Read(cmdSzBytes, 0, CmdSizeHolderBytesCount); int commandSize = HelperFxn.ToInt32(cmdSzBytes, 0, CmdSizeHolderBytesCount); byte[] cmdBytes = new byte[commandSize]; stream.Read(cmdBytes, 0, commandSize); CommandResponse cmdRespose = new CommandResponse(false, new Address()); cmdRespose.CacheId = _cacheId; cmdRespose.Src = this.ServerAddress; if (WriteRequestIdInResponse) { cmdRespose.NeedsDeserialization = true; cmdRespose.RequestId = requestId; cmdRespose.SerializedResponse = cmdBytes; cmdRespose.Type = responseType; } else { using (Stream tempStream = new ClusteredMemoryStream(cmdBytes)) response = ResponseHelper.DeserializeResponse(responseType, tempStream); cmdRespose.NeedsDeserialization = false; if (response != null) { cmdRespose.Result = response; } } if (_perfStatsColl.IsEnabled) { _perfStatsColl.IncrementClientResponsesPerSecStats(1); } if (cmdRespose != null) { _container.ProcessResponse(cmdRespose, _serverAddress); } }
private void DeserializeRawResponsesIfPresent() { if (_rawResponses != null && _rawResponses.Count > 0) { for (int i = 0; i < _rawResponses.Count; i++) { CommandResponse respnse = _rawResponses[i]; using (Stream tempStream = new ClusteredMemoryStream(respnse.SerializedResponse)) respnse.Result = ResponseHelper.DeserializeResponse(respnse.Type, tempStream); respnse.NeedsDeserialization = false; AddResponse(respnse.Src, respnse); } _rawResponses.Clear(); } }
protected async Task <IOTPResponse> ExecuteCommand(IOTPCommand command) { //send Command-begin request with command type await CommHelper.SendRequest(ReqType.Command, reqLCG.GenerateValue(), new byte[1] { command.CommandType }, 0, 1); //receive OK answer await ReceiveOkAnswer(); //transfer command-data var cmdData = command.SerializedData; var dataLeft = cmdData.Length; while (dataLeft > 0) { var dataLen = dataLeft; if (dataLen > Config.CMD_MAX_PLSZ) { dataLen = Config.CMD_MAX_PLSZ; } //send CommandData request await CommHelper.SendRequest(ReqType.CommandData, reqLCG.GenerateValue(), cmdData, cmdData.Length - dataLeft, dataLen); //receive OK answer await ReceiveOkAnswer(); dataLeft -= dataLen; } PreCommandCommit(); //send Command-commit request await CommHelper.SendRequest(ReqType.Command, reqLCG.GenerateValue(), new byte[1] { 0 }, 0, 1); //receive OK answer await ReceiveOkAnswer(); PostCommandCommit(); //request response from device await CommHelper.SendRequest(ReqType.DataRequest, reqLCG.GenerateValue(), new byte[1] { 0 }, 0, 1); //receive dataMarker answer, that contains information about pending response var answer = await CommHelper.ReceiveAnswer(); if (answer.ansType != AnsType.DataMarker) { throw new Exception($"Received non DataMarker answer: {answer.ansType}"); } if (answer.seq != ansLCG.GenerateValue()) { throw new Exception("Sequence number mismatch in DataMarker answer"); } var respType = answer.payload[0]; //receive data fragments var totalResponseSz = 0; var responseFragments = new List <byte[]>(); while (totalResponseSz < Config.MAX_RESPONSE_SZ) { //request response-data from device await CommHelper.SendRequest(ReqType.DataRequest, reqLCG.GenerateValue(), new byte[1] { 1 }, 0, 1); //receive answer, that contains data chunk answer = await CommHelper.ReceiveAnswer(); if (answer.ansType != AnsType.Data) { throw new Exception($"Received non Data answer: {answer.ansType}"); } if (answer.seq != ansLCG.GenerateValue()) { throw new Exception("Sequence number mismatch in DataMarker answer"); } //generate response=object if (answer.payload.Length == 0) { var serializedData = new byte[totalResponseSz]; var serializedPos = 0; foreach (var frag in responseFragments) { Buffer.BlockCopy(frag, 0, serializedData, serializedPos, frag.Length); serializedPos += frag.Length; } return(ResponseHelper.DeserializeResponse(respType, serializedData)); } responseFragments.Add(answer.payload); totalResponseSz += answer.payload.Length; } throw new Exception("Response data size reached!"); }