/// <summary> /// 创建客户端。 /// </summary> /// <param name="endPoint">终结点。</param> /// <returns>传输客户端实例。</returns> public ITransportClient CreateClient(EndPoint endPoint) { var key = endPoint; if (_logger.IsEnabled(LogLevel.Debug)) { _logger.LogDebug($"准备为服务端地址:{key}创建客户端。"); } try { return(_clients.GetOrAdd(key , k => new Lazy <ITransportClient>(() => { var bootstrap = _bootstrap; var channel = bootstrap.ConnectAsync(k).Result; var messageListener = new MessageListener(); channel.GetAttribute(messageListenerKey).Set(messageListener); var messageSender = new DotNettyMessageClientSender(_transportMessageEncoder, channel); channel.GetAttribute(messageSenderKey).Set(messageSender); channel.GetAttribute(origEndPointKey).Set(k); var client = new TransportClient(messageSender, messageListener, _logger, _serviceExecutor); return client; } )).Value); } catch { _clients.TryRemove(key, out var value); throw; } }
/// <summary> /// 创建客户端。 /// </summary> /// <param name="endPoint">终结点。</param> /// <returns>传输客户端实例。</returns> public ITransportClient CreateClient(EndPoint endPoint) { var key = endPoint.ToString(); if (_logger.IsEnabled(LogLevel.Debug)) { _logger.Debug($"准备为服务端地址:{key}创建客户端。"); } return(_clients.GetOrAdd(key , k => new Lazy <ITransportClient>(() => { var messageListener = new MessageListener(); _bootstrap.Handler(new ActionChannelInitializer <ISocketChannel>(c => { var pipeline = c.Pipeline; pipeline.AddLast(new LengthFieldPrepender(4)); pipeline.AddLast(new LengthFieldBasedFrameDecoder(int.MaxValue, 0, 4, 0, 4)); pipeline.AddLast(new DefaultChannelHandler(messageListener, _serializer)); })); var bootstrap = _bootstrap; var channel = bootstrap.ConnectAsync(endPoint); var messageSender = new DotNettyMessageClientSender(_serializer, channel); var client = new TransportClient(messageSender, messageListener, _logger, _serializer); return client; } )).Value); }