/// <summary> /// 接收具体操作 /// </summary> /// <param name="msaea"></param> private void ProcessReceive(MySocketAsyncEventArgs msaea) { SocketSession session = (SocketSession)msaea.UserToken; // e.BytesTransferred获取套接字操作中传输的字节数。不大于0,则对方SOCKET已关闭 if (msaea.BytesTransferred > 0) { if (msaea.SocketError == SocketError.Success) { if (ReceiveEvent != null) { byte[] data = new byte[msaea.BytesTransferred]; System.Buffer.BlockCopy(Buffer.Buffer, msaea.Offset, data, 0, msaea.BytesTransferred); ReceiveEvent(null, new ReceiveEventArgs { Receive = data, Session = session }); } } else { Log.WriteLog(msaea.SocketError.ToString(), LogType.INFO); } ////接收下一个数据包 bool resultFlag = session.Client.ReceiveAsync(msaea); if (!resultFlag) { ProcessReceive(msaea); } } else { CloseClientSocket(msaea, CloseReason.PASSIVE_CLOSE); } }
/// <summary> /// 接入连接具体操作 /// </summary> /// <param name="saea"></param> private void ProcessAccept(SocketAsyncEventArgs saea) { try { Interlocked.Increment(ref _CurConn);//改当前连接数 string guid = Guid.NewGuid().ToString("N"); SocketSession session = new SocketSession { Client = saea.AcceptSocket, SessionId = guid }; _dic.TryAdd(guid, session); //添加SokcetSession MySocketAsyncEventArgs msaea = Buffer.GetSocketEvent(); //从池中获取一个SocketAsyncEventArgs msaea.UserToken = session; msaea.SetBuffer(Buffer.Buffer, msaea.CurrentIndex * this.BufferSize, this.BufferSize); //设置连接的缓冲区 msaea.Completed -= IO_Completed; msaea.Completed += IO_Completed; _argsDic.TryAdd(guid, msaea);//添加SocketAsyncEventArgs NewSessionEvent?.Invoke(null, new SessionEventArgs { Type = SessionType.NEW, Endpoint = saea.AcceptSocket.RemoteEndPoint.ToString(), Session = session }); bool resultFlag = saea.AcceptSocket.ReceiveAsync(msaea); if (!resultFlag) { ProcessReceive(msaea); } }catch (Exception e) { Log.WriteLog(e.TargetSite + "->" + e.Message, LogType.INFO); } StartAccept(saea); }
/// <summary> /// 数据发送具体执行 /// </summary> /// <param name="msae"></param> private void ProcessSend(MySocketAsyncEventArgs msae) { if (msae.SocketError == SocketError.Success) { if (SendEvent != null) { SocketSession session = msae.UserToken as SocketSession; SendEvent(null, new SendEventArgs { Session = session }); } } else { CloseClientSocket(msae, CloseReason.PASSIVE_CLOSE); } }
//关闭SOCKET连接 private void CloseClientSocket(MySocketAsyncEventArgs msaea, CloseReason reason) { SocketSession session = (SocketSession)msaea.UserToken; CloseSessionEvent?.Invoke(null, new SessionEventArgs { Type = SessionType.CLOSED, Reason = reason, Endpoint = session.Client.RemoteEndPoint.ToString() }); try { _dic.TryRemove(session.SessionId, out SocketSession outValue); _argsDic.TryRemove(session.SessionId, out MySocketAsyncEventArgs eventArgs); }//移除关闭的连接 catch (Exception e) { Log.WriteLog(e.TargetSite + "->" + e.Message, LogType.ERROR); } try { session.Client.Shutdown(SocketShutdown.Send); } catch (Exception e) { } try { session.Client.Close(); } catch (Exception e) { } Interlocked.Decrement(ref _CurConn); //当前连接数自减 msaea.UserToken = null; Buffer.FreeSocketEvent(msaea); //把关闭的事件对象,放入集合中 }