/// <summary> /// 开始接收一个异步操作传入的连接 /// </summary> protected virtual void BeginAcceptAsync(SocketAsyncEventArgs socketAsyncEventArgs) { try { if (socketAsyncEventArgs.SocketError == SocketError.Success) { //连接尝试接入成功 //这个这个同步信号 目的是为了控制 其服务器的停止或者启动的 _autoReset.WaitOne(); _autoReset.Set(); NewConnectArgs newConnectArgs = new NewConnectArgs(socketAsyncEventArgs); OnNewConnectEvent(newConnectArgs); if (!newConnectArgs.IsCanceled) { OnNewAppSession(new NewAppSessionArgs(socketAsyncEventArgs)); if (_socketBufferManager.SetBuffer(socketAsyncEventArgs)) { if (!socketAsyncEventArgs.AcceptSocket.ReceiveAsync(socketAsyncEventArgs)) { BeginReceiveAsync(socketAsyncEventArgs); } } else { RecycleSocketAsyncEventArgs(socketAsyncEventArgs); } } else { RecycleSocketAsyncEventArgs(socketAsyncEventArgs); } } else { RecycleSocketAsyncEventArgs(socketAsyncEventArgs); } } catch (Exception ex) { RecycleSocketAsyncEventArgs(socketAsyncEventArgs); _logger.Error(ex); } finally { //循环开始一个异步操作来接受一个传入的连接尝试 AcceptAsync(); } }
protected virtual void OnNewConnectEvent(NewConnectArgs newConnectArgs) { try { _socketServerEvent.OnNewConnect(AppSocketContainer.Ins, newConnectArgs); _logger.DebugFormat("客户端{0}连接接入!", newConnectArgs.SocketAsyncEventArgs.AcceptSocket.RemoteEndPoint.ToString()); } catch (Exception ex) { _logger.Error(ex); } }
/// <summary> /// 新连接接入 /// </summary> /// <param name="container"></param> /// <param name="newConnectArgs">这里可以设置 newConnectArgs.IsCanceled 来过滤这个连接</param> public virtual void OnNewConnect(AppSocketContainer container, NewConnectArgs newConnectArgs) { }