.net C# 异步Socket 发送类.


采用异步Socket发送时,对于对像的回收使用了池技术

   /// 
    /// 用于辅助异步发送Socket的帮肋类
    /// 
    public class AsyncSocketSendHelper
    {

        #region 客户端联接池
        /// 
        /// 接收SAEA池集合
        /// 
        private ConcurrentStack ReceiveSocketArgsPool = new ConcurrentStack();

        /// 
        /// 初始化接收socketargs对象池
        /// 
        protected void InitReceiveSocketArgsPool()
        {
            for (int ndx = 0; ndx < 3000 ; ndx++)
            {
                ReceiveSocketArgsPool.Push(CreateReceiveSocketArgs());
            }
        }

        /// 
        /// 创建一个接收SAEA对象,设置最大接收字节数
        /// 
        /// 
        protected virtual SocketAsyncEventArgs CreateReceiveSocketArgs()
        {
            SocketAsyncEventArgs e = new SocketAsyncEventArgs();
            e.Completed += IO_SendComleted;
            return e;
        }




        /// 
        /// 租赁一个接收SAEA对象
        /// 
        /// 
        protected virtual SocketAsyncEventArgs RentReveiveSocketArgs()
        {
            SocketAsyncEventArgs e;

            return ReceiveSocketArgsPool.TryPop(out e) ? e : CreateReceiveSocketArgs();
        }

        /// 
        /// 归还一个接收SAEA参数
        /// 
        /// 还池
        protected virtual void GivebackReceiveSocketArgs(SocketAsyncEventArgs e)
        {
            if (e != null)
            {
                e.UserToken = null;
                ReceiveSocketArgsPool.Push(e);
            }
        }

        #endregion

        /// 
        /// 发送数据服务
        /// 
        /// 用于发送的Socket对像
        /// 需要发送的数据
        /// 回调函数参数为:发送结果,目标节点,发送数据
        /// 用户数据 
        /// 
        public virtual void SendToAsync(Socket socket, byte[] buff, Action callBack = null, object userToken = null)
        {
            if (socket == null)
            {
                throw new NullReferenceException();
            }

            if (buff == null)
            {
                throw new NullReferenceException();
            }

            SocketAsyncEventArgs e = RentReveiveSocketArgs();
            //设置发送缓冲区
            e.SetBuffer(buff, 0, buff.Length);
            try
            {
                //用户标识
                var token = new AsyncSendResult
                {
                    Result = false,
                    RemoteEndPoint = socket.RemoteEndPoint.ToString(),
                    SendTime = DateTime.Now,
                    SendData = buff,
                    ResultTime = DateTime.Now,
                    CallBakc = callBack,
                    UserToKen = userToken,
                };
                e.UserToken = token;
                //发送数据
                if (!socket.SendAsync(e))
                {
                    IO_SendComleted(socket, e);
                }
            }
            catch (SocketException)
            {
                //还池
                GivebackReceiveSocketArgs(e);
            }
            catch (ObjectDisposedException)
            {
                //还池
                GivebackReceiveSocketArgs(e);
            }
        }

        /// 
        /// 发送数据后的完成功能
        /// 
        /// 
        /// 
        protected virtual void IO_SendComleted(object sender, SocketAsyncEventArgs e)
        {
            try
            {
                if (e.UserToken != null)
                {
                    AsyncSendResult token = e.UserToken as AsyncSendResult;
                    if (token != null)
                    {
                        //设置结果时间
                        token.ResultTime = DateTime.Now;

                        //发送数据OK
                        if (e.SocketError == SocketError.Success)
                        {
                            token.Result = true;
                            if (token.CallBakc != null)
                            {
                                token.CallBakc(token);
                            }

                        }
                        else
                        {
                            //发送数据失败
                            token.Result = false;
                            if (token.CallBakc != null)
                            {
                                token.CallBakc(token);
                            }
                        }
                    }
                }
            }
            finally
            {
                //还池
                GivebackReceiveSocketArgs(e);
            }
        }






    }

    /// 
    /// 异步发送结果
    /// 
    public class AsyncSendResult
    {
        /// 
        /// 结果
        /// 
        public bool Result { get; set; }

        /// 
        /// 目标节点
        /// 
        public string RemoteEndPoint { get; set; }

        /// 
        /// 发送数据
        /// 
        public byte[] SendData { get; set; }

        /// 
        /// 发送时间
        /// 
        public DateTime SendTime { get; set; }

        /// 
        /// 结果返回时间
        /// 
        public DateTime ResultTime { get; set; }


        /// 
        /// 获取或设置与此操作关联的用户或应用程序对象。
        /// 
        public object UserToKen { get; set; }

        /// 
        /// 用于回调的委托
        /// 
        internal Action CallBakc { get; set; }
    }



你可能感兴趣的:(C#,Socket)