Example #1
0
        public override DataEventArgs GetResult(DataEventArgs e, ChannelPool channel)
        {
            try
            {
                if (channel.Client == null || channel.Client.Connected != true)
                {
                    channel.Client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    channel.Client.Connect(channel.IpPoint);
                    channel.Available = true;
                }
                byte[] _sbuffer = e.ToByteArray();
                channel.Client.Send(_sbuffer, _sbuffer.Length, SocketFlags.None);

                ByteBuilder builder = new ByteBuilder(1);

                byte[] _recbuff = new byte[2048];
                int    i        = channel.Client.Receive(_recbuff, 2048, SocketFlags.None);
                builder.Add(_recbuff.Take(i).ToArray());
                int total = builder.GetInt32(0);
                while (channel.Client.Available > 0)
                {
                    i = channel.Client.Receive(_recbuff, 2048, SocketFlags.None);
                    builder.Add(_recbuff, 0, i);
                }
                while (total > builder.Count)
                {
                    i = channel.Client.Receive(_recbuff, 2048, SocketFlags.None);
                    builder.Add(_recbuff, 0, i);
                }
                if (total == builder.Count)
                {
                    DataEventArgs d = DataEventArgs.Parse(builder);
                    d.StatusCode = StatusCode.Success;
                    Channels[e.CallHashCode].ActiveHash = 0;
                    return(d);
                }
                Channels[e.CallHashCode].ActiveHash = 0;
                e.StatusCode = StatusCode.TimeOut;
                return(e);
            }
            catch (SocketException se)
            {
                foreach (ChannelPool p in Channels)
                {
                    if (p.IpPoint == channel.IpPoint)
                    {
                        p.Available = false;
                    }
                }
                e.StatusCode = StatusCode.Error;
                return(e);
            }

            catch (Exception ex)
            {
                Console.WriteLine(ex);
                e.StatusCode = StatusCode.Error;
                return(e);
            }
        }
Example #2
0
        /// <summary>
        /// 转换为二进制数据
        /// </summary>
        /// <returns></returns>
        public byte[] ToByteArray()
        {
            string param       = new JsonSerializer().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());
        }
Example #3
0
        /// <summary>
        /// 解析数据包
        /// </summary>
        /// <param name="recvBuilder">接收到的历史数据</param>
        /// <returns></returns>
        public static DataEventArgs Parse(ByteBuilder recvBuilder)
        {
            int count = recvBuilder.Count;
            int 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 JsonSerializer().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()
                });
            }
        }