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,71 +171,61 @@ internal void BeginConnectAsync(EndPoint endpoint, Socket socket, PhysicalConnec ...@@ -170,71 +171,61 @@ internal void BeginConnectAsync(EndPoint endpoint, Socket socket, PhysicalConnec
SocketAwaitable.OnCompleted(args); SocketAwaitable.OnCompleted(args);
} }
EndConnectAsync(awaitable, multiplexer, log, socket, physicalConnection); // Complete connection
} try
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); bool ignoreConnect = false;
} ShouldIgnoreConnect(physicalConnection, ref ignoreConnect);
throw; if (ignoreConnect) return;
} await awaitable;
}
private async void EndConnectAsync(SocketAwaitable awaitable, ConnectionMultiplexer multiplexer, TextWriter log, Socket socket, PhysicalConnection connection)
{
try
{
bool ignoreConnect = false;
ShouldIgnoreConnect(connection, ref ignoreConnect);
if (ignoreConnect) return;
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 {
{ physicalConnection.StartReading();
connection.StartReading(); }
} catch (Exception ex)
catch (Exception ex) {
{ ConnectionMultiplexer.TraceWithoutContext(ex.Message);
ConnectionMultiplexer.TraceWithoutContext(ex.Message); Shutdown(socket);
}
break;
default:
ConnectionMultiplexer.TraceWithoutContext("Aborting socket");
Shutdown(socket); Shutdown(socket);
} break;
break; }
default:
ConnectionMultiplexer.TraceWithoutContext("Aborting socket");
Shutdown(socket);
break;
} }
} catch (ObjectDisposedException)
catch (ObjectDisposedException)
{
multiplexer.LogLocked(log, "(socket shutdown)");
if (connection != null)
{ {
try { connection.Error(); } multiplexer.LogLocked(log, "(socket shutdown)");
try { physicalConnection?.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);
if (connection != null)
{ {
try { connection.Error(); } ConnectionMultiplexer.TraceWithoutContext(outer.Message);
try { physicalConnection?.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