コード例 #1
0
        /// <summary>
        /// 解析元数据
        /// </summary>
        /// <param name="data">元数据</param>
        /// <returns>
        /// 返回能否解析的一个标示
        /// </returns>
        public override List <T> Parse <T>(byte[] data)
        {
            int      offset = 0;
            int      totalLength;
            List <T> messages = new List <T>();

            try
            {
                while (offset < data.Length)
                {
                    totalLength = BitConverter.ToInt32(data, offset);
                    if (totalLength > data.Length)
                    {
                        _tracing.Error("#Parse message failed, illegal total length. #length: " + totalLength);
                        return(messages);
                    }
                    byte[] messageData = totalLength == data.Length
                                             ? data
                                             : ByteArrayHelper.GetReallyData(data, offset, totalLength);
                    int  protocolId  = messageData[5];
                    int  serviceId   = messageData[6];
                    int  detailsId   = messageData[7];
                    Type messageType = GetMessageType(protocolId, serviceId, detailsId);
                    if (messageType == null)
                    {
                        _tracing.Error("#Parse message failed, illegal message protocol. #protocol id={0}, service id={1}, detalis id={2}\r\nTarget protocol stack: {3} ", protocolId, serviceId, detailsId, this);
                        return(messages);
                    }
                    offset += messageData.Length;
                    BaseMessage message;
                    try
                    {
                        //使用智能对象引擎进行解析
                        message = IntellectObjectEngine.GetObject <BaseMessage>(messageType, messageData);
                        if (message == null)
                        {
                            _tracing.Error("#Parse message failed, parse result = null. #protocol id={0}, service id={1}, detalis id={2}: ", protocolId, serviceId, detailsId);
                            return(messages);
                        }
                    }
                    catch (System.Exception ex)
                    {
                        _tracing.Error(ex, "#Parse message failed.");
                        continue;
                    }
                    messages.Add((T)(object)message);
                    if (data.Length - offset < 4)
                    {
                        break;
                    }
                }
                return(messages);
            }
            catch (System.Exception ex)
            {
                _tracing.Error(ex, "#Parse message failed.");
                return(messages);
            }
        }