Beispiel #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);
            }
        }
Beispiel #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());
        }