Esempio n. 1
0
        /// <summary>
        /// 启动会话循环接收
        /// </summary>
        /// <param name="session">会话</param>
        private async void LoopReceiveAsync(TcpSessionBase session)
        {
            // 通知插件会话已连接
            var context = this.CreateContext(session);

            if (this.plugManager.RaiseConnected(this, context) == false)
            {
                return;
            }

            try
            {
                await session.AuthenticateAsync().ConfigureAwait(false);

                if (this.plugManager.RaiseAuthenticated(this, context))
                {
                    session.LoopReceiveAsync();
                }
            }
            catch (Exception ex)
            {
                this.plugManager.RaiseException(this, ex);
                this.ReuseSession(session);
            }
        }
Esempio n. 2
0
 /// <summary>
 /// 回收复用会话对象
 /// 关闭会话并通知连接断开
 /// </summary>
 /// <param name="session">会话对象</param>
 private void ReuseSession(TcpSessionBase session)
 {
     if (this.sessionManager.Remove(session) == true)
     {
         var context = this.CreateContext(session);
         this.plugManager.RaiseDisconnected(this, context);
     }
 }
Esempio n. 3
0
 /// <summary>
 /// 创建上下文对象
 /// </summary>
 /// <param name="session">当前会话</param>
 /// <returns></returns>
 private IContenxt CreateContext(TcpSessionBase session)
 {
     return(new DefaultContext
     {
         Session = session,
         StreamReader = session.StreamReader,
         AllSessions = this.sessionManager
     });
 }
Esempio n. 4
0
 /// <summary>
 /// 收到请求数据
 /// </summary>
 /// <param name="session">会话对象</param>
 /// <returns></returns>
 private async Task OnRequestAsync(TcpSessionBase session)
 {
     try
     {
         var context = this.CreateContext(session);
         if (this.plugManager.RaiseRequested(this, context))
         {
             await this.middlewareManager.RaiseInvoke(context);
         }
     }
     catch (Exception ex)
     {
         this.plugManager.RaiseException(this, ex);
     }
 }
Esempio n. 5
0
 /// <summary>
 /// SSL支持的Tcp客户端抽象类
 /// </summary>
 /// <param name="targetHost">目标主机</param>
 /// <param name="certificateValidationCallback">远程证书验证回调</param>
 /// <exception cref="ArgumentNullException"></exception>
 public TcpClientBase(string targetHost, RemoteCertificateValidationCallback certificateValidationCallback)
 {
     this.session = new SslTcpSession(targetHost, certificateValidationCallback);
     this.BindHandler(this.session);
 }
Esempio n. 6
0
 /// <summary>
 /// Tcp客户端抽象类
 /// </summary>
 public TcpClientBase()
 {
     this.session = new IocpTcpSession();
     this.BindHandler(this.session);
 }
Esempio n. 7
0
 /// <summary>
 /// 关闭连接处理
 /// </summary>
 /// <param name="session">会话</param>
 private void DisconnectHandler(TcpSessionBase session)
 {
     session.Shutdown();
     this.OnDisconnected();
     this.LoopReconnectAsync();
 }
Esempio n. 8
0
 /// <summary>
 /// 接收处理
 /// </summary>
 /// <param name="session">会话</param>
 private Task ReceiveHandler(TcpSessionBase session)
 {
     return(this.OnReceiveAsync(session.StreamReader));
 }
Esempio n. 9
0
 /// <summary>
 /// 绑定会话的处理方法
 /// </summary>
 /// <param name="session">会话</param>
 private void BindHandler(TcpSessionBase session)
 {
     session.ReceiveCompletedHandler = this.ReceiveHandler;
     session.DisconnectHandler       = this.DisconnectHandler;
 }