Exemple #1
0
        /// <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);
            }
        }
 /// <summary>
 /// 断开连接
 /// </summary>
 public void DisconnectServer()
 {
     State = ENetworkState.Disconnect;
     if (_channel != null)
     {
         _server.ReleaseChannel(_channel);
         _channel = null;
     }
 }
Exemple #3
0
        private void AddChannel(TChannel channel)
        {
            if (channel == null)
            {
                return;
            }

            // 加入到频道列表
            lock (_allChannels)
            {
                _allChannels.Add(channel);
            }
        }
 private void OnConnectServer(TChannel channel, SocketError error)
 {
     LogSystem.Log(ELogType.Log, $"Server connect result : {error}");
     if (error == SocketError.Success)
     {
         _channel = channel;
         State    = ENetworkState.Connected;
     }
     else
     {
         State = ENetworkState.Disconnect;
     }
 }
Exemple #5
0
        /// <summary>
        /// 释放频道并从列表里移除
        /// </summary>
        public void ReleaseChannel(TChannel channel)
        {
            if (channel == null)
            {
                return;
            }

            channel.Dispose();

            // 从频道列表里删除
            lock (_allChannels)
            {
                _allChannels.Remove(channel);
            }

            // 信号减弱
            if (_maxAcceptedSemaphore != null)
            {
                _maxAcceptedSemaphore.Release();
            }
        }
Exemple #6
0
        /// <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);
        }
Exemple #7
0
 /// <summary>
 /// 初始化频道
 /// </summary>
 public void InitChannel(TChannel channel)
 {
     Channel = channel;
 }