private async Task RunServerAsync() { logger.Debug("Entered RunServerAsync"); while (!shutdownCancellationTokenSource.IsCancellationRequested) { try { var listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); listener.Bind(configuration.RemoteEndpoint); listener.Listen(1337); logger.Debug($"Began listening to endpoint {configuration.RemoteEndpoint}."); __listenerSocket = listener; while (!shutdownCancellationTokenSource.IsCancellationRequested) { var client = await Task.Factory.FromAsync(listener.BeginAccept, listener.EndAccept, null).ConfigureAwait(false); Console.BackgroundColor = ConsoleColor.Red; logger.Debug($"Got client socket from {client.RemoteEndPoint}."); Console.BackgroundColor = ConsoleColor.Black; var routingContext = new TcpRoutingContext(configuration, tcpRoutingContextContainer, client, inboundMessageDispatcher, identity, routingTable, peerTable, payloadUtils); tcpRoutingContextContainer.AddOrThrow(routingContext); routingContext.RunAsync().Forget(); } } catch (SocketException) { await Task.Delay(kConnectionRetryIntervalMillis).ConfigureAwait(false); } catch (ObjectDisposedException) { // socket disposed } } logger.Debug("Leaving RunServerAsync"); }
private async Task RunClientAsync() { while (!shutdownCancellationTokenSource.IsCancellationRequested) { try { var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); client.Connect(configuration.RemoteEndpoint); var routingContext = new TcpRoutingContext(configuration, tcpRoutingContextContainer, client, inboundMessageDispatcher, identity, routingTable, peerTable, payloadUtils); tcpRoutingContextContainer.AddOrThrow(routingContext); await routingContext.RunAsync().ConfigureAwait(false); } catch (SocketException) { await Task.Delay(kConnectionRetryIntervalMillis).ConfigureAwait(false); } } }
public void UnassociateRemoteIdentityOrThrow(Guid remoteId, TcpRoutingContext routingContext) { clientRoutingContextsByClientId.RemoveOrThrow(remoteId, routingContext); }
public bool TryGetByRemoteId(Guid remoteId, out TcpRoutingContext routingContext) { return clientRoutingContextsByClientId.TryGetValue(remoteId, out routingContext); }
public void RemoveOrThrow(TcpRoutingContext routingContext) { if (!routingContexts.TryRemove(routingContext)) { throw new InvalidStateException(); } }
public void AddOrThrow(TcpRoutingContext routingContext) { if (!routingContexts.TryAdd(routingContext)) { throw new InvalidStateException(); } }