/// <summary> /// Converts the string representation of a message to its object equivalent. /// </summary> /// <param name="s">A string containing a message to convert.</param> /// <returns>An object equivalent to the message contained in <see cref="s"/>.</returns> public static Message Parse(string s) { if (s == null) { throw new ArgumentNullException(nameof(s)); } if (s.Equals(string.Empty)) { throw new InvalidCastException("The input string is empty."); } if (s.Trim().Equals(string.Empty)) { throw new InvalidCastException("The input string contains only spaces."); } int messageLength, command; Message message = new Message(); message.Content = s; if (s.Length < StructureLength.CMD_POS || s.Substring(0, StructureLength.HEAD_LEN) != header) { throw new CommunicationException(GetErrorMessage(FormatErrorType.MSGHDR), FormatErrorType.MSGHDR); } if (!int.TryParse(s.Substring(StructureLength.LEN_POS, StructureLength.LEN_LEN), out messageLength)) { throw new CommunicationException(GetErrorMessage(FormatErrorType.MSGDAT), FormatErrorType.MSGDAT); } if (messageLength < StructureLength.MIN_MSG_LEN || messageLength > StructureLength.MAX_MSG_LEN) { FormatErrorType error = (messageLength < StructureLength.MIN_MSG_LEN ? FormatErrorType.MSGMIN : FormatErrorType.MSGMAX); throw new CommunicationException(GetErrorMessage(error), error); } if (s.Length < messageLength) { throw new CommunicationException(GetErrorMessage(FormatErrorType.MSGLEN), FormatErrorType.MSGLEN); } if (s.Last() != footer) { throw new CommunicationException(GetErrorMessage(FormatErrorType.MSGFMT), FormatErrorType.MSGFMT); } message.Data = s.Substring(StructureLength.DAT_POS, messageLength - StructureLength.FIX_LEN).Split(fieldSeparator[0]).ToList(); if (!int.TryParse(s.Substring(StructureLength.CMD_POS, StructureLength.CMD_LEN), out command)) { throw new CommunicationException(GetErrorMessage(FormatErrorType.MSGCMD), FormatErrorType.MSGCMD); } if (Enum.IsDefined(typeof(Command), command) && command != (int)Command.Error) { message.Command = (Command)Enum.Parse(typeof(Command), command.ToString(), true); if (IsChecksumValid(s)) { return(message); } else { throw new CommunicationException(GetErrorMessage(FormatErrorType.MSGCKS), FormatErrorType.MSGCKS); } } else if (command == (int)Command.Error) { int internalErrorCode = Convert.ToInt32(message.Data[0]); string internalErrorMessage; if (message.Data.Count > 1) { internalErrorMessage = message.Data[1]; } else { internalErrorMessage = GetInternalErrorMessage(internalErrorCode); } InternalError internalError = new InternalError() { ErrorCode = internalErrorCode, Message = internalErrorMessage }; throw new CommunicationException(GetErrorMessage(FormatErrorType.GENSCS), FormatErrorType.GENSCS, internalError); } else { throw new CommunicationException(GetErrorMessage(FormatErrorType.MSGCMD), FormatErrorType.MSGCMD); } }
public CommunicationException(string message, FormatErrorType formatError, InternalError internalError) : base(message) { FormatError = formatError; InternalError = internalError; }