Ejemplo n.º 1
0
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="parameters">NodeClient参数信息</param>
        public DefaultNodeClient(NodeClientParameters parameters)
        {
            if (parameters == null)
            {
                throw new NodeClientException("NodeClientConfig is null.");
            }

            logger                = LoggerManager.ClientLoggerFactory.CreateLogger <DefaultNodeClient>();
            Host                  = parameters.Host;
            Port                  = parameters.Port;
            LocalHost             = parameters.LocalHost;
            LocalPort             = parameters.LocalPort;
            Serializer            = parameters.Serializer ?? throw new NodeClientException("Serializer is null.");
            ProtocolStackFactory  = parameters.ProtocolStackFactory ?? new DefaultProtocolStackFactory();
            loginHandler          = parameters.LoginHandler ?? new DefaultLoginHandler(null, Serializer, LoggerManager.ClientLoggerFactory);
            passiveClosedStrategy = parameters.PassiveClosedStrategy ?? new DefaultPassiveClosedStrategy(new DefaultPassiveClosedStrategyConfig(), LoggerManager.ClientLoggerFactory);
            client                = parameters.Communication;

            if (loginHandler != null)
            {
                client.OnSubmitLoginRequest += new SubmitLoginRequestDelegate(async() =>
                {
                    var loginInfo = await loginHandler.GetLoginInfo();
                    return(new LoginRequestData()
                    {
                        Body = loginInfo.Body,
                        Attachments = loginInfo.Attachments
                    });
                });

                client.OnRecieveLoginResponse += new RecieveLoginResponseDelegate((body, attachments) =>
                {
                    return(loginHandler.LoginResponseHandle(new LoginResponseInfo()
                    {
                        Body = body,
                        Attachments = attachments
                    }));
                });

                client.OnPassiveClosed += new PassiveClosedDelegate(client =>
                {
                    return(passiveClosedStrategy.Handle(client));
                });
            }
        }