public Connection GetConnection(SlioAddress endpoint) { var connectionEntry = GetConnectionEntry(endpoint); lock (connectionEntry) { var connection = connectionEntry.GetConnection(); if (connection == null || !connection.IsConnected) { if (connection != null && !connection.IsConnected) { connection.Dispose(); connection = null; } if (connection == null) { var newConnection = new OutboundConnection(serviceProvider); try { newConnection.Connect(endpoint); } catch { newConnection.Dispose(); throw; } connection = newConnection; connectionEntry.Connections.Add(newConnection); } } return(connection); } }
public async Task <OutboundConnection> OpenOutboundConnectionAsync(Task <OutboundConnection> openTask) { var conn = await openTask.ConfigureAwait(false); this.OutboundConnection = conn; return(conn); }
private static void AddOutboundConnection(IPEndPoint endPoint, ICollection <OutboundConnection> outbounds) { var outbound = new OutboundConnection(endPoint); outbound.DataSent += OutboundOnDataSent; outbound.Connected += OutboundOnConnected; outbound.ConnectionError += OutboundOnConnectionError; outbounds.Add(outbound); }
public ValueTask <ConnectionContext> Connect(EndPoint endpoint) { var tcs = new TaskCompletionSource <ConnectionContext>(TaskCreationOptions.RunContinuationsAsynchronously); var context = OutboundConnection.Create(endpoint, tcs, _memoryPool, _options, _scheduler); _scheduler.ScheduleAsyncAddAndConnect(context.Socket, context); return(new ValueTask <ConnectionContext>(tcs.Task)); }
private async Task <OutboundConnection> OpenOutboundConnectionAsync <T>(IPEndPoint endPoint, bool secure, Func <IPEndPoint, T> connectionFactory) where T : OutboundConnection { if (OutboundConnection != null) { if (!OutboundConnection.RemoteEndPoint.Equals(endPoint)) { Log.Debug("{0}: Current outbound connection is for {1}, can't reuse for {2}", InboundConnection.RemoteEndPoint, OutboundConnection.RemoteEndPoint, endPoint); OutboundConnection.Close(); OutboundConnection = null; } else if (OutboundConnection.IsSecure != secure) { Log.Debug("{0}: Current outbound connection {0} secure, can't reuse", InboundConnection.RemoteEndPoint); OutboundConnection.Close(); OutboundConnection = null; } else { if (OutboundConnection.IsConnected) { Log.Debug("{0}: Reusing outbound connection to {1}", InboundConnection.RemoteEndPoint, OutboundConnection.RemoteEndPoint); return(OutboundConnection); } Log.Debug("{0}: Detected stale outbound connection, recreating", InboundConnection.RemoteEndPoint); OutboundConnection.Close(); OutboundConnection = null; } } var conn = connectionFactory(endPoint); await conn.OpenAsync().ConfigureAwait(false); Log.Debug("{0}: Outbound connection to {1} established", InboundConnection.RemoteEndPoint, conn.RemoteEndPoint); OutboundConnection = conn; return(conn); }