Commit 97d9e375 authored by Nick Craver's avatar Nick Craver Committed by Nick Craver

Collapse EndConnectAsync into BeginConnectAsync for simplicity

This fixes net462, but not netcoreapp test hangs
parent b81cba51
...@@ -137,27 +137,28 @@ internal static Socket CreateSocket(EndPoint endpoint) ...@@ -137,27 +137,28 @@ internal static Socket CreateSocket(EndPoint endpoint)
return socket; return socket;
} }
static void ConfigureTimeout(SocketAsyncEventArgs args, int timeoutMilliseconds) private static void ConfigureTimeout(SocketAsyncEventArgs args, int timeoutMilliseconds)
{ {
var timeout = Task.Delay(timeoutMilliseconds); var timeout = Task.Delay(timeoutMilliseconds);
timeout.ContinueWith((t, state) => timeout.ContinueWith((_, state) =>
{ {
var a = (SocketAsyncEventArgs)state; var a = (SocketAsyncEventArgs)state;
try { Socket.CancelConnectAsync(a); } catch { } try { Socket.CancelConnectAsync(a); } catch { }
try try { ((SocketAwaitable)a.UserToken).Complete(0, SocketError.TimedOut); } catch { }
{ ((SocketAwaitable)a.UserToken).Complete(0, SocketError.TimedOut); }
catch { }
}, args); }, args);
} }
internal void BeginConnectAsync(EndPoint endpoint, Socket socket, PhysicalConnection physicalConnection, ConnectionMultiplexer multiplexer, TextWriter log)
internal async void BeginConnectAsync(EndPoint endpoint, Socket socket, PhysicalConnection physicalConnection, ConnectionMultiplexer multiplexer, TextWriter log)
{ {
var formattedEndpoint = Format.ToString(endpoint); var formattedEndpoint = Format.ToString(endpoint);
multiplexer.LogLocked(log, "BeginConnect: {0}", formattedEndpoint); multiplexer.LogLocked(log, "BeginConnect: {0}", formattedEndpoint);
var awaitable = new SocketAwaitable(); var awaitable = new SocketAwaitable();
var args = new SocketAsyncEventArgs(); var args = new SocketAsyncEventArgs
args.UserToken = awaitable; {
args.RemoteEndPoint = endpoint; UserToken = awaitable,
RemoteEndPoint = endpoint
};
args.Completed += SocketAwaitable.Callback; args.Completed += SocketAwaitable.Callback;
try try
{ {
...@@ -170,34 +171,21 @@ internal void BeginConnectAsync(EndPoint endpoint, Socket socket, PhysicalConnec ...@@ -170,34 +171,21 @@ internal void BeginConnectAsync(EndPoint endpoint, Socket socket, PhysicalConnec
SocketAwaitable.OnCompleted(args); SocketAwaitable.OnCompleted(args);
} }
EndConnectAsync(awaitable, multiplexer, log, socket, physicalConnection); // Complete connection
}
catch (NotImplementedException ex)
{
if (!(endpoint is IPEndPoint))
{
throw new InvalidOperationException("BeginConnect failed with NotImplementedException; consider using IP endpoints, or enable ResolveDns in the configuration", ex);
}
throw;
}
}
private async void EndConnectAsync(SocketAwaitable awaitable, ConnectionMultiplexer multiplexer, TextWriter log, Socket socket, PhysicalConnection connection)
{
try try
{ {
bool ignoreConnect = false; bool ignoreConnect = false;
ShouldIgnoreConnect(connection, ref ignoreConnect); ShouldIgnoreConnect(physicalConnection, ref ignoreConnect);
if (ignoreConnect) return; if (ignoreConnect) return;
await awaitable; await awaitable;
var socketMode = connection == null ? SocketMode.Abort : await connection.ConnectedAsync(socket, log, this).ForAwait(); switch (physicalConnection == null ? SocketMode.Abort : await physicalConnection.ConnectedAsync(socket, log, this).ForAwait())
switch (socketMode)
{ {
case SocketMode.Async: case SocketMode.Async:
multiplexer.LogLocked(log, "Starting read"); multiplexer.LogLocked(log, "Starting read");
try try
{ {
connection.StartReading(); physicalConnection.StartReading();
} }
catch (Exception ex) catch (Exception ex)
{ {
...@@ -214,27 +202,30 @@ private async void EndConnectAsync(SocketAwaitable awaitable, ConnectionMultiple ...@@ -214,27 +202,30 @@ private async void EndConnectAsync(SocketAwaitable awaitable, ConnectionMultiple
catch (ObjectDisposedException) catch (ObjectDisposedException)
{ {
multiplexer.LogLocked(log, "(socket shutdown)"); multiplexer.LogLocked(log, "(socket shutdown)");
if (connection != null) try { physicalConnection?.Error(); }
{
try { connection.Error(); }
catch (Exception inner) catch (Exception inner)
{ {
ConnectionMultiplexer.TraceWithoutContext(inner.Message); ConnectionMultiplexer.TraceWithoutContext(inner.Message);
} }
} }
}
catch (Exception outer) catch (Exception outer)
{ {
ConnectionMultiplexer.TraceWithoutContext(outer.Message); ConnectionMultiplexer.TraceWithoutContext(outer.Message);
if (connection != null) try { physicalConnection?.Error(); }
{
try { connection.Error(); }
catch (Exception inner) catch (Exception inner)
{ {
ConnectionMultiplexer.TraceWithoutContext(inner.Message); ConnectionMultiplexer.TraceWithoutContext(inner.Message);
} }
} }
} }
catch (NotImplementedException ex)
{
if (!(endpoint is IPEndPoint))
{
throw new InvalidOperationException("BeginConnect failed with NotImplementedException; consider using IP endpoints, or enable ResolveDns in the configuration", ex);
}
throw;
}
} }
partial void OnDispose(); partial void OnDispose();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment