/// <summary> /// Разбирает последовательность байт в объект со значениями из данной последовательности. /// В случае неверной структуры, длины или ошибок возвращает значение null. /// </summary> /// <param name="response">Ответ от удаленного сервера.</param> public static ForwardOpenResponse Parse(MessageRouterResponse response) { ForwardOpenResponse forwardOpenResponse = new ForwardOpenResponse(); if (response == null || response.ResponseData == null) { return(null); } List <byte> bytes = response.ResponseData; if (response.GeneralStatus == 0) { if (bytes.Count >= 26) { forwardOpenResponse.OtoTConnectionID = (uint)(bytes[0] | bytes[1] << 8 | bytes[2] << 16 | bytes[3] << 24); forwardOpenResponse.TtoOConnectionID = (uint)(bytes[4] | bytes[5] << 8 | bytes[6] << 16 | bytes[7] << 24); forwardOpenResponse.ConnectionSerialNumber = (ushort)(bytes[8] | bytes[9] << 8); forwardOpenResponse.OriginatorVendorID = (ushort)(bytes[10] | bytes[11] << 8); forwardOpenResponse.OriginatorSerialNumber = (uint)(bytes[12] | bytes[13] << 8 | bytes[14] << 16 | bytes[15] << 24); forwardOpenResponse.OtoTActualPacketInterval = (uint)(bytes[16] | bytes[17] << 8 | bytes[18] << 16 | bytes[19] << 24); forwardOpenResponse.TtoOActualPacketInterval = (uint)(bytes[20] | bytes[21] << 8 | bytes[22] << 16 | bytes[23] << 24); int applicationReplySize = bytes[24]; for (int ix = 0; ix < applicationReplySize; ix += 2) { int index = ix + 26; if (index >= bytes.Count) { return(null); } forwardOpenResponse.ApplicationReply.Add(bytes[index]); forwardOpenResponse.ApplicationReply.Add(bytes[index + 1]); } forwardOpenResponse.IsSuccessful = true; } else { return(null); } } else { if (bytes.Count == 10) { forwardOpenResponse.ConnectionSerialNumber = (ushort)(bytes[0] | bytes[1] << 8); forwardOpenResponse.OriginatorVendorID = (ushort)(bytes[2] | bytes[3] << 8); forwardOpenResponse.OriginatorSerialNumber = (uint)(bytes[4] | bytes[5] << 8 | bytes[6] << 16 | bytes[7] << 24); forwardOpenResponse.RemainingPathSize = bytes[8]; forwardOpenResponse.IsSuccessful = false; } else { return(null); } } return(forwardOpenResponse); }
/// <summary> /// Разбирает последовательность байт в объект со значениями из данной последовательности. /// В случае неверной структуры, длины или ошибок возвращает значение null. /// </summary> /// <param name="response">Ответ от удаленного сервера.</param> public static ForwardCloseResponse Parse(MessageRouterResponse response) { ForwardCloseResponse forwardCloseResponse = new ForwardCloseResponse(); if (response == null || response == null) { return(null); } List <byte> bytes = response.ResponseData; forwardCloseResponse.IsSuccessful = false; if (bytes.Count >= 9) { forwardCloseResponse.ConnectionSerialNumber = (ushort)(bytes[0] | bytes[1] << 8); forwardCloseResponse.OriginatorVendorID = (ushort)(bytes[2] | bytes[3] << 8); forwardCloseResponse.OriginatorSerialNumber = (uint)(bytes[4] | bytes[5] << 8 | bytes[6] << 16 | bytes[7] << 24); int applicationReplySize = bytes[8]; if (response.GeneralStatus == 0) { for (int ix = 0; ix < applicationReplySize; ix++) { int index = ix * 2 + 10; if (index >= bytes.Count || applicationReplySize * 2 != bytes.Count - 10) { return(null); } forwardCloseResponse.ApplicationReply.Add(bytes[index]); forwardCloseResponse.ApplicationReply.Add(bytes[index + 1]); } forwardCloseResponse.IsSuccessful = true; } } else { return(null); } return(forwardCloseResponse); }
/// <summary> /// Разбирает последовательность байт в объект со значениями из данной последовательности. /// В случае неверной структуры, длины или ошибок возвращает значение null. /// </summary> /// <param name="bytes">Последовательность байт.</param> public static MessageRouterResponse Parse(List <byte> bytes) { MessageRouterResponse messageRouterResponse = new MessageRouterResponse(); if (bytes == null) { return(null); } messageRouterResponse.ReplyServiceCode = bytes[0]; messageRouterResponse.GeneralStatus = bytes[2]; int additionalStatusSize = bytes[3]; int index = 4; for (int ix = 0; ix < additionalStatusSize; ix++) { index = ix * 2 + 4; if (index >= bytes.Count || additionalStatusSize * 2 > bytes.Count - 4) { return(null); } messageRouterResponse.AdditionalStatus.Add((ushort)(bytes[index] | bytes[index + 1] << 8)); } if (additionalStatusSize > 0) { index += 2; } if (index < bytes.Count) { messageRouterResponse.ResponseData.AddRange(bytes.GetRange(index, bytes.Count - index)); } return(messageRouterResponse); }