示例#1
0
        private static void PrintModel(Object model, StringBuilder str, String indent, String[] printableClassPrefixes)
        {
            String newLine = Environment.NewLine;

            str.Append(model.GetType().FullName).Append("()").Append(newLine);
            str.Append(indent).Append("{").Append(newLine);
            PropertyInfo[] Properties = model.GetType().GetProperties();
            foreach (PropertyInfo property in Properties)
            {
                Object value = property.CanRead ? property.GetValue(model, null) : "null";
                str.Append(indent).Append(property.Name).Append(" = ");
                if (value != null)
                {
                    if (property.PropertyType.IsEnum)
                    {
                        str.Append(property.PropertyType.Name).Append(".")
                        .Append(Enum.Format(property.PropertyType, value, "G"));
                    }
                    else if (IsPrintableClass(value.GetType(), printableClassPrefixes))
                    {
                        PrintModel(value, str, indent + "\t", printableClassPrefixes);
                    }
                    else if (value is byte[])
                    {
                        byte[] val = (byte[])value;
                        str.Append(MessageUtility.HexToReadableString(val));
                    }
                    else if (value is string[])
                    {
                        string[] val = (string[])value;
                        str.Append("[");
                        if (val.Length > 0)
                        {
                            str.Append(String.Join(", ", val));
                        }
                        str.Append("]");
                    }
                    else
                    {
                        str.Append(value.ToString());
                    }
                }
                else
                {
                    str.Append("null");
                }
                str.Append(newLine);
            }
            str.Append(indent).Append("}");//.Append(newLine);
        }
示例#2
0
        private void WriteMessageProcessException(Exception e, String log)
        {
            StringBuilder sbLog = new StringBuilder(log);
            String        nl    = System.Environment.NewLine;

            if (e is MessageParserException)
            {
                sbLog.Append(" Message:").Append(nl);
                String message = MessageUtility.HexToReadableString(ReceivedMessage, 80);
                sbLog.Append(message);
            }
            else if (e is MessageCompilerException)
            {
                sbLog.Append(" Model:").Append(nl);
                sbLog.Append(Util.GetReadableStringFromModel(SentModel));
            }
            Logger.GetInstance().Write(e, sbLog.ToString());
        }
示例#3
0
        private MessageToModelConfig GetMessageToModelConfig()
        {
            MessageToModelConfig cfg = (_workerThread != null && _workerThread.MessageToModelConfig != null)
                ? _workerThread.MessageToModelConfig
                : MessageConfigs.GetQulifiedMessageToModel(_bytes);

            if (cfg == null)
            {
                byte[] bytes2 = new byte[config.MaskConfig.MinBytesCountToCheck < _bytes.Length
                    ? config.MaskConfig.MinBytesCountToCheck : _bytes.Length];
                Array.Copy(_bytes, bytes2, bytes2.Length);
                throw new MessageParserException("No matching config for this message. Check message-to-model elements in configuration file."
                                                 + Environment.NewLine
                                                 + bytes2.Length + " first bytes of message: " + MessageUtility.HexToReadableString(bytes2));
            }
            return(cfg);
        }
示例#4
0
        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);
        }