public void SendException(LJPException ex) { LJPExceptionDTO exc = new() { IClientLJPExceptionType = (int)ex.LJPExceptionType, Message = ex.Message, Code = ex.Code, SerializedData = ex.SerializedData }; SendResponse(exc); }
private LJPResponse ConvertToLJPResponse( ICollection <Type> tpeNamespace , byte[] aReceiveBuffer , bool bThrowExceptionWithNotData) { byte[] dinamycBufferToAllMessage = null; try { LJPResponse oLJPResponse = new LJPResponse(); var indexOfHeaderAndBodySeparator = ByteHelper.IndexOf(aReceiveBuffer , 0 , new byte[] { Convert.ToByte('\n'), Convert.ToByte('\n') }); if (indexOfHeaderAndBodySeparator == -1) { throw new LJPException("Bad Little Json Protocol, Expected Response Object") { LJPExceptionType = LJPExceptionType.BadProtocol }; } #region Extracting Header Of SendResponse Message var headerBytes = new byte[indexOfHeaderAndBodySeparator]; Array.Copy(aReceiveBuffer, headerBytes, indexOfHeaderAndBodySeparator); string headerUTF8 = Encoding.UTF8.GetString(headerBytes); var headerUTF8Splited = headerUTF8.Split('\n'); if (headerUTF8Splited.Length < 2 || indexOfHeaderAndBodySeparator == -1) { throw new LJPException("Bad Little Json Protocol, Expected Response Object") { LJPExceptionType = LJPExceptionType.BadProtocol }; } #if DEBUG logger.Debug($"Header=[{string.Join(", ", headerUTF8Splited) }]"); #endif //Receive Total of json object var lengthAndValueInString = headerUTF8Splited.Single(o => o.Contains("Length=")); var typeAndValueInString = headerUTF8Splited.Single(o => o.Contains("Type=")); #endregion var lengthInString = lengthAndValueInString.Split(new char[] { '=' }, 2)[1]; var typeInString = typeAndValueInString.Split(new char[] { '=' }, 2)[1]; oLJPResponse.iLength = int.Parse(lengthInString); try { Type tpePrimitive = TryToResolvePrimitivesType(typeInString); if (nameof(LJPExceptionDTO).Equals(typeInString, StringComparison.OrdinalIgnoreCase)) { oLJPResponse.tpeObject = reflector.GetType(typeof(LJPExceptionDTO), typeInString); } else if (tpePrimitive == null) { Type tpetryGetType = null; foreach (var tpeNamespaceFE in tpeNamespace) { if (reflector.TryGetType(tpeNamespaceFE, typeInString, out tpetryGetType)) { oLJPResponse.tpeObject = tpetryGetType; break; } } if (oLJPResponse.tpeObject == null) { throw new ArgumentException($"Type not found in namespaces:\n\"{string.Join(",", tpeNamespace)}\""); } } else { oLJPResponse.tpeObject = tpePrimitive; } } catch (Exception ex) { logger.Error(ex.ToString()); throw new LJPException($"Error: trying to get type \"{typeInString}\" in namespace \"{tpeNamespace}\"."); } //Receive the json object part int indexOfEndOfBody = ByteHelper.IndexOf(aReceiveBuffer, indexOfHeaderAndBodySeparator + 2, new byte[] { Convert.ToByte('\0') }); indexOfEndOfBody = indexOfEndOfBody == -1 ? aReceiveBuffer.Length : indexOfEndOfBody; var firstExtractOfBody = new byte[indexOfEndOfBody - (indexOfHeaderAndBodySeparator + 2)]; if (firstExtractOfBody.Length > oLJPResponse.iLength) { throw new LJPException($"Bad body length Had={firstExtractOfBody.Length}, Expected={oLJPResponse.iLength}."); } Buffer.BlockCopy(aReceiveBuffer, indexOfHeaderAndBodySeparator + 2, firstExtractOfBody, 0, firstExtractOfBody.Length); int receivedToNow = firstExtractOfBody.Length; dinamycBufferToAllMessage = new byte[oLJPResponse.iLength]; Buffer.BlockCopy(firstExtractOfBody, 0, dinamycBufferToAllMessage, 0, receivedToNow); //Whether there is more fragment of the object string receivedMessageInASCII = ""; while (receivedToNow < oLJPResponse.iLength) { Array.Clear(aReceiveBuffer, 0, aReceiveBuffer.Length); ClientConnector.Receive(aReceiveBuffer, 0, aReceiveBuffer.Length); int indexOfEnd = ByteHelper.IndexOf(aReceiveBuffer, 0, nullByteInArray); indexOfEnd = indexOfEnd == -1 ? aReceiveBuffer.Length : indexOfEnd; indexOfEnd = Math.Min(oLJPResponse.iLength - receivedToNow, indexOfEnd); Buffer.BlockCopy(aReceiveBuffer, 0, dinamycBufferToAllMessage, receivedToNow, indexOfEnd); receivedToNow += indexOfEnd; logger.Debug($"[ReceivePart={receivedMessageInASCII.Length}] [{receivedToNow}/{oLJPResponse.iLength}]"); if (indexOfEnd == 0 || indexOfEnd == -1) { break; } } var sObject = Encoding.UTF8.GetString(dinamycBufferToAllMessage); #if DEBUG logger.Debug($"Received Message: [{sObject.Replace("\n", "[nl]")}]"); #endif dynamic oObject = null; //using (MemoryStream oMS = new()) //{ //using StreamWriter oSW = new(oMS); //DataContractJsonSerializer oJsonSerializer = new(oLJPResponse.tpeObject); //oSW.Write(sObject); //oSW.Flush(); //oMS.Position = 0; //oObject = oJsonSerializer.ReadObject(oMS); //oLJPResponse.oObject = oObject; //} oObject = JsonConvert.DeserializeObject(sObject, oLJPResponse.tpeObject); oLJPResponse.oObject = oObject; if (oObject is LJPExceptionDTO && bThrowExceptionWithNotData) { var oLJPException = oObject as LJPExceptionDTO; var nLJPEExceptionType = (LJPExceptionType)oLJPException.IClientLJPExceptionType; var exception = new LJPException(oLJPException.Message, nLJPEExceptionType, oLJPException.Code) { SerializedData = oLJPException.SerializedData }; throw exception; } else if (oObject is LJPExceptionDTO && !bThrowExceptionWithNotData) { oLJPResponse.oObject = null; } return(oLJPResponse); } finally { dinamycBufferToAllMessage = null; } }