public async Task OnServerDisconnected(TcpSocketSaeaClient client) { if (_onServerDisconnected != null) { await _onServerDisconnected(client); } }
/// <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; }
public async Task OnServerDataReceived(TcpSocketSaeaClient client, byte[] data, int offset, int count) { if (_onServerDataReceived != null) { await _onServerDataReceived(client, data, offset, count); } }
public Task OnServerConnected(TcpSocketSaeaClient client) { #if NET45 || NET451 return Task.FromResult(1); #else return Task.CompletedTask; #endif }
/// <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; }
public async Task OnServerDisconnected(TcpSocketSaeaClient client) { if (_onServerDisconnected != null) await _onServerDisconnected(client); }
public async Task OnServerDataReceived(TcpSocketSaeaClient client, byte[] data, int offset, int count) { if (_onServerDataReceived != null) await _onServerDataReceived(client, data, offset, count); }
public async Task OnServerDataReceived(TcpSocketSaeaClient client, byte[] data, int offset, int count) { if (_logger.IsEnabled(LogLevel.Information)) _logger.LogInformation("接收到数据包。"); var message = _transportMessageDecoder.Decode(data.Skip(offset).Take(count).ToArray()); if (_logger.IsEnabled(LogLevel.Information)) _logger.LogInformation("接收到消息:" + message.Id); _messageListener.OnReceived(new SimpleClientMessageSender(_transportMessageEncoder, client), message); #if NET45 || NET451 await Task.FromResult(1); #else await Task.CompletedTask; #endif }
public SimpleClientMessageSender(ITransportMessageEncoder transportMessageEncoder, TcpSocketSaeaClient client) : base(transportMessageEncoder) { _client = client; }