private void AcceptNewClient(SocketAsyncEventArgs e) { if (e.SocketError == SocketError.Success) { //Get the socket for the accepted client connection and put it into the //ReadEventArg object user token SocketAsyncEventArgsProxy socketEventArgsProxy = m_ReadWritePool.Pop(); socketEventArgsProxy.Socket = e.AcceptSocket; if (Config.Debug) { Console.Write("-"); } //Console.WriteLine("Client {0} now in .", e.AcceptSocket.RemoteEndPoint.ToString()); // Console.WriteLine("Now {0}", _sessions.Count); AsyncSocketSession session = RegisterSession <AsyncSocketSession>(e.AcceptSocket); session.SocketAsyncProxy = socketEventArgsProxy; session.Config = Config; //Notice here :订阅了事件 //在会话结束的时候要执行取消会话的操作 否则由还存在调用关系导致GC无法正常进行 session.Closed += new EventHandler <SocketSessionClosedEventArgs>(session_Closed); session.OnRequestReceived += new EventHandler <SocketPlainTextEventArgs>(OnRequestReceived); _tcpClientConnected.Set(); session.Start(); _sessions.Add(session);// Demo } else { _tcpClientConnected.Set(); } }
/// <summary> /// Add a SocketAsyncEventArg instance to the pool /// </summary> /// <param name="item">The SocketAsyncEventArgs instance to add to the pool</param> public void Push(SocketAsyncEventArgsProxy item) { if (item == null) { throw new ArgumentNullException("Items added to a SocketAsyncEventArgsPool cannot be null"); } lock (m_pool) { item.Socket = null; m_pool.Push(item); } }