예제 #1
0
        /// <summary>
        /// 解析数据包
        /// </summary>
        /// <param name="recvBuilder">接收到的历史数据</param>
        /// <returns></returns>
        public static DataEventArgs Parse(ByteBuilder recvBuilder)
        {
            long count = recvBuilder.Count;
            long total = recvBuilder.GetInt32(0);

            // 数据一定要大于等于固定长度  包长小于等于数据长度
            if (recvBuilder.Count >= ConstLength && recvBuilder.GetInt32(0) <= recvBuilder.Count)
            {
                // 包长
                int totalLength = recvBuilder.ReadInt32();
                // cmdLength
                int cmdLength = recvBuilder.ReadInt32();

                int paramLength = recvBuilder.ReadInt32();

                int idLength = recvBuilder.ReadInt32();

                int errLength = recvBuilder.ReadInt32();
                // 哈希值
                int hashCode = recvBuilder.ReadInt32();

                int taskId = recvBuilder.ReadInt32();

                int statusCode = recvBuilder.ReadInt32();

                int tryTimes = recvBuilder.ReadInt32();

                int ipLength    = recvBuilder.ReadInt32();
                int pavalLength = recvBuilder.ReadInt32();

                String cmd       = Encoding.UTF8.GetString(recvBuilder.ReadRange(cmdLength), 0, cmdLength);
                String param     = Encoding.UTF8.GetString(recvBuilder.ReadRange(paramLength), 0, paramLength);
                String id        = Encoding.UTF8.GetString(recvBuilder.ReadRange(idLength), 0, idLength);
                byte[] errBinary = new Binary(recvBuilder.ReadRange(errLength)).Buffer;
                byte[] ipBinary  = new Binary(recvBuilder.ReadRange(ipLength)).Buffer;
                string ipAddress = string.Empty;
                if (ipBinary.Length > 0)
                {
                    ipAddress = Encoding.UTF8.GetString(ipBinary);
                }
                string lastError = string.Empty;
                if (errBinary.Length > 0)
                {
                    lastError = Encoding.UTF8.GetString(errBinary);
                }

                ArrayList Param = new ProtoSerializer().ToEntity <ArrayList>(Encoding.UTF8.GetString(recvBuilder.ReadRange(pavalLength)));
                // 实体长
                int entityLength = totalLength - ConstLength - cmdLength - paramLength - idLength - errLength - ipLength - pavalLength;
                // 实体数据
                Binary entityBinary = new Binary(recvBuilder.ReadRange(entityLength));
                // 校验长
                int checkLength = recvBuilder.ReadInt32();

                // 检验数据
                if (totalLength == checkLength)
                {
                    // 返回数据事件包给发送者
                    return(new DataEventArgs()
                    {
                        Binary = entityBinary, Param = Param, LastError = lastError, RemoteIpAddress = ipAddress, TryTimes = tryTimes, StatusCode = (StatusCode)statusCode, TaskId = taskId, ActionCmd = cmd, CallHashCode = hashCode, ActionParam = param, HttpSessionId = id
                    });
                }
                else
                {
                    Console.WriteLine("无效包 包转换失败 清除");
                    _log.Error("无效包 包转换失败 清除");
                    // 无效包 清除
                    recvBuilder.Clear();
                    return(new DataEventArgs()
                    {
                        Binary = null, LastError = "包转换失败", ActionCmd = CallActionCmd.Call.ToString(), StatusCode = StatusCode.Error, TaskId = 9999, CallHashCode = hashCode, ActionParam = param, HttpSessionId = id
                    });
                }
            }
            else
            {
                Console.WriteLine("空包 网络丢失 清除");
                _log.Error("空包 网络丢失 清除");
                recvBuilder.Clear();
                return(new DataEventArgs()
                {
                    Binary = null, LastError = "网络丢失", StatusCode = StatusCode.Error, ActionCmd = CallActionCmd.Call.ToString()
                });
            }
        }
예제 #2
0
        /// <summary>
        /// 转换为二进制数据
        /// </summary>
        /// <returns></returns>
        public byte[] ToByteArray()
        {
            string param       = new ProtoSerializer().ToString(Param);
            int    cmdLength   = this.ActionCmd.Length;
            int    paramLength = Encoding.UTF8.GetByteCount(this.ActionParam);
            int    idLength    = Encoding.UTF8.GetByteCount(HttpSessionId);
            int    errLength   = Encoding.UTF8.GetByteCount(LastError);
            //
            int pavalLength = Encoding.UTF8.GetByteCount(param);
            int ipLength    = 0;

            if (!string.IsNullOrEmpty(this.RemoteIpAddress))
            {
                ipLength = Encoding.UTF8.GetByteCount(RemoteIpAddress);
            }
            int capacity = ConstLength + cmdLength + paramLength + idLength + errLength + ipLength + pavalLength;

            if (this.Binary != null && this.Binary.Buffer != null)
            {
                capacity = capacity + this.Binary.Buffer.Length; // +实体数据长
            }

            ByteBuilder builder = new ByteBuilder(capacity);

            builder.Add(BitConverter.GetBytes(capacity));
            builder.Add(BitConverter.GetBytes(cmdLength));
            builder.Add(BitConverter.GetBytes(paramLength));
            builder.Add(BitConverter.GetBytes(idLength));
            builder.Add(BitConverter.GetBytes(errLength));
            builder.Add(BitConverter.GetBytes(this.CallHashCode));


            builder.Add(BitConverter.GetBytes(this.TaskId));
            builder.Add(BitConverter.GetBytes((int)this.StatusCode));
            builder.Add(BitConverter.GetBytes(this.TryTimes));
            builder.Add(BitConverter.GetBytes(ipLength));
            builder.Add(BitConverter.GetBytes(pavalLength));



            builder.Add(Encoding.UTF8.GetBytes(this.ActionCmd));

            builder.Add(Encoding.UTF8.GetBytes(this.ActionParam));

            builder.Add(Encoding.UTF8.GetBytes(this.HttpSessionId));



            if (!string.IsNullOrEmpty(RemoteIpAddress))
            {
                builder.Add(Encoding.UTF8.GetBytes(RemoteIpAddress));
            }

            if (!string.IsNullOrEmpty(LastError))
            {
                builder.Add(Encoding.UTF8.GetBytes(this.LastError));
            }

            builder.Add(Encoding.UTF8.GetBytes(param));

            if (this.Binary != null)
            {
                builder.Add(this.Binary.Buffer);
            }
            builder.Add(BitConverter.GetBytes(capacity));
            return(builder.GetBaseBuffer());
        }