private void CheckMessageLength() { byte[] lenHdr = new byte[_msgCfg.LengthHeader]; Array.Copy(_bytes, 0, lenHdr, 0, _msgCfg.LengthHeader); int length = (int)MessageUtility.BytesToInt(lenHdr); if (_bytes.Length - _msgCfg.LengthHeader != length) { throw new MessageParserException("Incompatible length header value with the actual message length."); } _token += _msgCfg.LengthHeader; }
private byte[] Receive() { if (MessageStream == null) { return(null); } int readLength = config.MaskConfig.MinBytesCountToCheck; byte[] checkData = new byte[readLength]; int offset = 0, numberReadData = 0; do { numberReadData = MessageStream.Receive(checkData, offset, readLength); offset += numberReadData; readLength -= numberReadData; } while (readLength > 0 && numberReadData > 0); config.MessageToModelConfig cfg = config.MessageConfigs.GetQulifiedMessageToModel(checkData); MessageToModelConfig = cfg; if (cfg == null) { throw new MessageProcessorException("Unrecognized message. Check message-to-model elements in configuration file." + Environment.NewLine + readLength + " first bytes of message: " + MessageUtility.HexToReadableString(checkData)); } byte[] lengthHeader = new byte[cfg.ModelCfg.MessageCfg.LengthHeader]; Array.Copy(checkData, lengthHeader, lengthHeader.Length); int messageLength = (int)MessageUtility.BytesToInt(lengthHeader); messageLength += lengthHeader.Length; byte[] data = new byte[messageLength]; Array.Copy(checkData, data, checkData.Length); offset = checkData.Length; readLength = messageLength - checkData.Length; do { numberReadData = MessageStream.Receive(data, offset, readLength); offset += numberReadData; readLength -= numberReadData; } while (readLength > 0 && numberReadData > 0); return(data); }