private void IO_Completed(object sender, SocketAsyncEventArgs e) { switch (e.LastOperation) { case SocketAsyncOperation.Receive: ProcessReceive(e); break; case SocketAsyncOperation.Send: { lstSendArgs.Push(e); ProcessSend(e); if (e.UserToken.Equals("a") || e.UserToken.Equals("b")) { //不做 } else { CacheEntity entity = null; if (dic_entity.TryRemove(e.UserToken.ToString(), out entity)) { entity.Dispose(); } } } break; default: throw new ArgumentException("The last operation completed on the socket was not a receive or send"); } }
/// <summary> /// 关闭客户端通信 /// </summary> /// <param name="e"></param> private void CloseClientSocket(SocketAsyncEventArgs e) { AsyncTcpUserToken token = e.UserToken as AsyncTcpUserToken; lock (m_clients) { m_clients.Remove(token); } //如果有事件,则调用事件,发送客户端数量变化通知 if (ClientNumberChange != null) { ClientNumberChange(-1, token); } try { token.Socket.Shutdown(SocketShutdown.Send); } catch (Exception) { } token.Socket.Close(); Interlocked.Decrement(ref m_clientCount); if (token != null && !string.IsNullOrEmpty(token.UserInfo)) { CacheEntity entity = null; if (dic_Entity.TryRemove(token.UserInfo, out entity)) { //客户端发送使用的缓存要释放 entity.Dispose(); } e.UserToken = null; e.SetBuffer(null, 0, 0); e.Dispose();//新建的,必须释放,不是来自缓存 return; } //接收的一定是缓存的 e.UserToken = new AsyncTcpUserToken(); m_pool.Push(e); }
/// <summary> /// 处理发送完成后的信息 /// </summary> /// <param name="e"></param> private void ProcessSend(SocketAsyncEventArgs e) { //TCP这里不好判断,缓存全部回收 if (e.SocketError == SocketError.Success) { // done echoing data back to the client //AsyncUserToken token = (AsyncUserToken)e.UserToken; // read the next block of data send from the client //bool willRaiseEvent = token.Socket.ReceiveAsync(e); //if (!willRaiseEvent) //{ // ProcessReceive(e); //} //发送完成回收 AsyncTcpUserToken token = e.UserToken as AsyncTcpUserToken; if (token != null && !string.IsNullOrEmpty(token.UserInfo)) { CacheEntity entity = null; if (dic_Entity.TryRemove(token.UserInfo, out entity)) { //客户端发送使用的缓存要释放 entity.Dispose(); } e.UserToken = null; e.SetBuffer(null, 0, 0); e.Dispose();//新建的,必须释放,不是来自缓存 return; } if (e.Count < m_bufferManager.BufferSize) { //直接释放内存,不是来自缓存 e.SetBuffer(null, 0, 0); e.UserToken = null; m_pool.Push(e); } else { //缓存回收,发送的需要回收,接收一定循环,发送不是 if (IsFixCacheSize) { m_bufferManager.FreeBuffer(e); } else { m_bufferManager.FreePoolBuffer(e); } m_pool.Push(e); } } else { CloseClientSocket(e); } }
/// <summary> /// 发送数据;从缓存中来的数据 /// </summary> /// <param name="token">客户端连接</param> /// <param name="entity">发送的缓存实体</param> /// <param name="flage">标记,默认null表示发送继续使用客户端缓存发送,否则由服务端分包使用服务端缓存发送</param> public void SendData(AsyncTcpUserToken token, CacheEntity entity, string flage = null) { if (string.IsNullOrEmpty(flage)) { //继续使用客户端缓存发送,不能分包使用服务端缓存 SocketAsyncEventArgs sendArg = new SocketAsyncEventArgs(); sendArg.Completed += this.IO_Completed; sendArg.SetBuffer(entity.Buffer, entity.Offset, entity.Length); token.UserInfo = Interlocked.Increment(ref entityid).ToString(); dic_Entity[token.UserInfo] = entity; sendArg.UserToken = token; if (token.Socket.SendAsync(sendArg)) { ProcessSend(sendArg); } } else { SendMessage(token, entity.Buffer); entity.Dispose(); } }