/// <summary> /// 处理连接请求 /// </summary> private void ProcessConnected(object obj) { TChannel channel = null; SocketAsyncEventArgs e = obj as SocketAsyncEventArgs; UserToken token = (UserToken)e.UserToken; if (e.SocketError == SocketError.Success) { // 创建频道 channel = new TChannel(); channel.InitChannel(e.ConnectSocket, token.PackageParseType); // 加入到频道列表 lock (_allChannels) { _allChannels.Add(channel); } } else { LogSystem.Log(ELogType.Error, $"ProcessConnected error : {e.SocketError}"); } // 回调函数 if (token.Callback != null) { token.Callback.Invoke(channel, e.SocketError); } }
private void AddChannel(TChannel channel) { if (channel == null) { return; } // 加入到频道列表 lock (_allChannels) { _allChannels.Add(channel); } }
/// <summary> /// 释放频道并从列表里移除 /// </summary> public void ReleaseChannel(TChannel channel) { if (channel == null) { return; } channel.Dispose(); // 从频道列表里删除 lock (_allChannels) { _allChannels.Remove(channel); } // 信号减弱 if (_maxAcceptedSemaphore != null) { _maxAcceptedSemaphore.Release(); } }
/// <summary> /// 处理接收请求 /// </summary> private void ProcessAccept(object obj) { SocketAsyncEventArgs e = obj as SocketAsyncEventArgs; if (e.SocketError == SocketError.Success) { // 创建频道 TChannel channel = new TChannel(); channel.InitChannel(e.AcceptSocket, _listenerPackageParseType); // 添加频道 AddChannel(channel); } else { LogSystem.Log(ELogType.Error, $"ProcessAccept error : {e.SocketError}"); } // 投递下一个接收请求 StartAccept(e); }
/// <summary> /// 初始化频道 /// </summary> public void InitChannel(TChannel channel) { Channel = channel; }