private async Task Listen() { tcpListener.Start(); while (true) { TcpClient client; try { client = await tcpListener.AcceptTcpClientAsync(); } catch (SocketException e) { if (e.SocketErrorCode == SocketError.Interrupted) { return; } throw; } var stream = client.GetStream(); var server = new ApiServer <T>(stream); apiServers.TryAdd(server, client); server.Disconnected += (api, _) => { if (apiServers.TryRemove((ApiServer <T>)api, out TcpClient client)) { client.Dispose(); } api.Dispose(); }; SetupApi?.Invoke(server.Api); server.Start(); } }
private async Task Connect(ApiClient <T> oldApi = null) { tcpClient = new TcpClient(); await tcpClient.ConnectAsync(EndPoint.Address, EndPoint.Port); var stream = tcpClient.GetStream(); client = new ApiClient <T>(stream, stream, oldApi); client.Disconnected += Client_Disconnected; if (oldApi == null) { SetupApi?.Invoke(client.Api); } client.Start(); }
private async Task Connect(ApiClient <T> oldApi = null) { pipe = new NamedPipeClientStream(ServerName, PipeName, PipeDirection.InOut, PipeOptions.Asynchronous); await pipe.ConnectAsync(cancellationToken.Token); if (cancellationToken.IsCancellationRequested) { return; } client = new ApiClient <T>(pipe, pipe, oldApi); client.Disconnected += Client_Disconnected; if (oldApi == null) { SetupApi?.Invoke(client.Api); } client.Start(); }