/// <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)); }); } }
private IList <INodeClient> BuildNodeClient() { var nodeClientList = new List <INodeClient>(); var nodeClientParametersList = NodeClientParameters.Create(connectionInfos, serializer, communicationFactory, loginHandler, protocolStackFactory, passiveClosedStrategy); foreach (var parameters in nodeClientParametersList) { var nodeClient = nodeClientFactory == null ? new DefaultNodeClient(parameters) : nodeClientFactory(parameters); if (nodeClient == null) { throw new InvalidOperationException("Load XNode client instance failed."); } nodeClientList.Add(nodeClient); } return(nodeClientList); }
/// <summary> /// 创建NodeClientParameters实例 /// </summary> /// <param name="connectionInfoList">连接信息</param> /// <param name="serializer">序列化器实例</param> /// <param name="communicationFactory">底层客户端通信组件工厂</param> /// <param name="loginHandler">登录处理器</param> /// <param name="protocolStackFactory">协议栈工厂实例</param> /// <param name="passiveClosedStrategy">客户端被动关闭处理策略</param> /// <returns></returns> public static IList <NodeClientParameters> Create(IList <ConnectionInfo> connectionInfoList, ISerializer serializer, Func <ConnectionInfo, IClient> communicationFactory, ILoginHandler loginHandler = null, IProtocolStackFactory protocolStackFactory = null, IPassiveClosedStrategy passiveClosedStrategy = null) { var list = new List <NodeClientParameters>(); foreach (var connectionInfo in connectionInfoList) { var item = new NodeClientParameters() { Host = connectionInfo.Host, Port = connectionInfo.Port, LocalHost = connectionInfo.LocalHost, LocalPort = connectionInfo.LocalPort, Serializer = serializer, ProtocolStackFactory = protocolStackFactory, LoginHandler = loginHandler, PassiveClosedStrategy = passiveClosedStrategy, Communication = communicationFactory(connectionInfo) }; list.Add(item); } return(list); }