/// <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; }
/// <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; }