/// <summary>
        /// 创建客户端。
        /// </summary>
        /// <param name="endPoint">终结点。</param>
        /// <returns>传输客户端实例。</returns>
        public ITransportClient CreateClient(EndPoint endPoint)
        {
            var key = endPoint.ToString();
            return _clients.GetOrAdd(key, k => new Lazy<ITransportClient>(() =>
            {
                var messageListener = new MessageListener();
                Func<TcpSocketSaeaClient> clientFactory = () =>
                {
                    var client = new TcpSocketSaeaClient((IPEndPoint)endPoint, async (c, data, offset, count) =>
                     {
                         if (_logger.IsEnabled(LogLevel.Information))
                             _logger.LogInformation("接收到数据包。");

                         var transportMessageDecoder = _transportMessageCodecFactory.GetDecoder();
                         var transportMessageEncoder = _transportMessageCodecFactory.GetEncoder();
                         var message = transportMessageDecoder.Decode(data.Skip(offset).Take(count).ToArray());

                         if (_logger.IsEnabled(LogLevel.Information))
                             _logger.LogInformation("接收到消息:" + message.Id);

                         await messageListener.OnReceived(new SimpleClientMessageSender(transportMessageEncoder, c), message);
                     });
                    client.Connect().Wait();
                    return client;
                };
                return new TransportClient(new SimpleClientMessageSender(_transportMessageCodecFactory.GetEncoder(), clientFactory), messageListener, _logger, _serviceExecutor);
            })).Value;
        }
Exemple #2
0
        /// <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));
                    }));

                    var bootstrap = _bootstrap;
                    var channel = bootstrap.ConnectAsync(endPoint);
                    var messageSender = new NettyMessageClientSender(channel);
                    var client = new TransportClient(messageSender, messageListener, _logger, _serializer);
                    return client;
                }
                )).Value;
        }
 /// <summary>
 /// 创建客户端。
 /// </summary>
 /// <param name="endPoint">终结点。</param>
 /// <returns>传输客户端实例。</returns>
 public ITransportClient CreateClient(EndPoint endPoint)
 {
     var config = new TcpSocketSaeaClientConfiguration();
     var key = endPoint.ToString();
     return _clients.GetOrAdd(key, k => new Lazy<ITransportClient>(() =>
     {
         var messageListener = new MessageListener();
         var client = new TcpSocketSaeaClient((IPEndPoint)endPoint, new SimpleMessageDispatcher(messageListener, _transportMessageCodecFactory, _logger), config);
         client.Connect().Wait();
         return new TransportClient(new SimpleClientMessageSender(_transportMessageCodecFactory.GetEncoder(), client), messageListener, _logger, _serviceExecutor);
     })).Value;
 }