.net C# 异步Socket 发送类.
来源:互联网 发布:阿里域名隐私保护 编辑:程序博客网 时间:2024/06/05 14:25
采用异步Socket发送时,对于对像的回收使用了池技术
/// <summary> /// 用于辅助异步发送Socket的帮肋类 /// </summary> public class AsyncSocketSendHelper { #region 客户端联接池 /// <summary> /// 接收SAEA池集合 /// </summary> private ConcurrentStack<SocketAsyncEventArgs> ReceiveSocketArgsPool = new ConcurrentStack<SocketAsyncEventArgs>(); /// <summary> /// 初始化接收socketargs对象池 /// </summary> protected void InitReceiveSocketArgsPool() { for (int ndx = 0; ndx < 3000 ; ndx++) { ReceiveSocketArgsPool.Push(CreateReceiveSocketArgs()); } } /// <summary> /// 创建一个接收SAEA对象,设置最大接收字节数 /// </summary> /// <returns></returns> protected virtual SocketAsyncEventArgs CreateReceiveSocketArgs() { SocketAsyncEventArgs e = new SocketAsyncEventArgs(); e.Completed += IO_SendComleted; return e; } /// <summary> /// 租赁一个接收SAEA对象 /// </summary> /// <returns></returns> protected virtual SocketAsyncEventArgs RentReveiveSocketArgs() { SocketAsyncEventArgs e; return ReceiveSocketArgsPool.TryPop(out e) ? e : CreateReceiveSocketArgs(); } /// <summary> /// 归还一个接收SAEA参数 /// </summary> /// <param name="e">还池</param> protected virtual void GivebackReceiveSocketArgs(SocketAsyncEventArgs e) { if (e != null) { e.UserToken = null; ReceiveSocketArgsPool.Push(e); } } #endregion /// <summary> /// 发送数据服务 /// </summary> /// <param name="socket">用于发送的Socket对像</param> /// <param name="buff">需要发送的数据</param> /// <param name="callBack">回调函数参数为:发送结果,目标节点,发送数据</param> /// <param name="userToken">用户数据 </param> /// <returns></returns> public virtual void SendToAsync(Socket socket, byte[] buff, Action<AsyncSendResult> 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); } } /// <summary> /// 发送数据后的完成功能 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> 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); } } } /// <summary> /// 异步发送结果 /// </summary> public class AsyncSendResult { /// <summary> /// 结果 /// </summary> public bool Result { get; set; } /// <summary> /// 目标节点 /// </summary> public string RemoteEndPoint { get; set; } /// <summary> /// 发送数据 /// </summary> public byte[] SendData { get; set; } /// <summary> /// 发送时间 /// </summary> public DateTime SendTime { get; set; } /// <summary> /// 结果返回时间 /// </summary> public DateTime ResultTime { get; set; } /// <summary> /// 获取或设置与此操作关联的用户或应用程序对象。 /// </summary> public object UserToKen { get; set; } /// <summary> /// 用于回调的委托 /// </summary> internal Action<AsyncSendResult> CallBakc { get; set; } }
0 0
- .net C# 异步Socket 发送类.
- .net socket与完成端口、异步发送
- C# Socket异步通讯客户端之发送数据
- C#中异步SOCKET发送数据时内存问题
- C#异步SOCKET发送帮肋类,支持UDP,TCP
- .net socket与完成端口、异步发送相关研究
- C#发送电子邮件 (异步)
- C#发送电子邮件 (异步)
- Socket异步发送接收,Windows
- C# server socket 异步
- C#异步Socket编程
- C# Socket异步通信
- C# Socket 异步 聊天室
- C# Socket异步通信
- C# Socket异步通信
- C# 异步Socket
- C# Socket异步通信
- c#异步socket
- android应用开发及系统有用代码片段总结
- 第13章、布局Layouts之RelativeLayout相对布局(从零开始学Android)
- 任意进制的转换(base2-base36)
- Eclipse “cannot be resolved to a type” error
- LeetCode——Palindrome Number
- .net C# 异步Socket 发送类.
- RemoteViews用法一:widget简单用法
- 类的继承与组合
- anctionbar样式 自定义属性
- 第14章、布局Layouts之FrameLayout框架布局(从零开始学Android)
- ERROR OGG-00524 DDL trace log file [\ggs_ddl_trace.log], error code -20782 error message ORA-207
- 用PPT转PDF转换器把PPT格式转成PDF
- 第15章、布局Layouts之TableLayout表格布局(从零开始学Android)
- iOS 在xcode6 百度地图开发 注意事项