Added comments and more tiny code clean up

This commit is contained in:
David Fowler 2016-10-01 03:22:09 -07:00
parent 5195e75b74
commit 5baaaac22d
1 changed files with 30 additions and 8 deletions

View File

@ -42,38 +42,58 @@ namespace Microsoft.AspNetCore.Sockets
} }
else else
{ {
// REVIEW: Errors?
// Get the end point mapped to this http connection // Get the end point mapped to this http connection
var endpoint = (EndPoint)context.RequestServices.GetRequiredService<TEndPoint>(); var endpoint = (EndPoint)context.RequestServices.GetRequiredService<TEndPoint>();
// Server sent events transport // Server sent events transport
if (context.Request.Path.StartsWithSegments(path + "/sse")) if (context.Request.Path.StartsWithSegments(path + "/sse"))
{ {
// Get the connection state for the current http context
var connectionState = GetOrCreateConnection(context); var connectionState = GetOrCreateConnection(context);
var sse = new ServerSentEvents((HttpChannel)connectionState.Connection.Channel); var channel = (HttpChannel)connectionState.Connection.Channel;
var sse = new ServerSentEvents(channel);
// Register this transport for disconnect
RegisterDisconnect(context, sse); RegisterDisconnect(context, sse);
var ignore = endpoint.OnConnected(connectionState.Connection); // Call into the end point passing the connection
var endpointTask = endpoint.OnConnected(connectionState.Connection);
// Start the transport
await sse.ProcessRequest(context); await sse.ProcessRequest(context);
// Transport has ended so kill the channel
connectionState.Connection.Channel.Dispose(); connectionState.Connection.Channel.Dispose();
// Wait for the user code to unwind
await endpointTask;
_manager.RemoveConnection(connectionState.Connection.ConnectionId); _manager.RemoveConnection(connectionState.Connection.ConnectionId);
} }
else if (context.Request.Path.StartsWithSegments(path + "/ws")) else if (context.Request.Path.StartsWithSegments(path + "/ws"))
{ {
// Get the connection state for the current http context
var connectionState = GetOrCreateConnection(context); var connectionState = GetOrCreateConnection(context);
var ws = new WebSockets((HttpChannel)connectionState.Connection.Channel); var channel = (HttpChannel)connectionState.Connection.Channel;
var ws = new WebSockets(channel);
// Register this transport for disconnect
RegisterDisconnect(context, ws); RegisterDisconnect(context, ws);
var ignore = endpoint.OnConnected(connectionState.Connection); // Call into the end point passing the connection
var endpointTask = endpoint.OnConnected(connectionState.Connection);
// Start the transport
await ws.ProcessRequest(context); await ws.ProcessRequest(context);
// Transport has ended so kill the channel
connectionState.Connection.Channel.Dispose(); connectionState.Connection.Channel.Dispose();
// Wait for the user code to unwind
await endpointTask;
_manager.RemoveConnection(connectionState.Connection.ConnectionId); _manager.RemoveConnection(connectionState.Connection.ConnectionId);
} }
else if (context.Request.Path.StartsWithSegments(path + "/poll")) else if (context.Request.Path.StartsWithSegments(path + "/poll"))
@ -87,12 +107,10 @@ namespace Microsoft.AspNetCore.Sockets
// Treat reserved connections like new ones // Treat reserved connections like new ones
if (connectionState.Connection.Channel == null) if (connectionState.Connection.Channel == null)
{ {
var channel = new HttpChannel(_channelFactory);
// REVIEW: The connection manager should encapsulate this... // REVIEW: The connection manager should encapsulate this...
connectionState.Active = true; connectionState.Active = true;
connectionState.LastSeen = DateTimeOffset.UtcNow; connectionState.LastSeen = DateTimeOffset.UtcNow;
connectionState.Connection.Channel = channel; connectionState.Connection.Channel = new HttpChannel(_channelFactory);
isNewConnection = true; isNewConnection = true;
} }
} }
@ -106,13 +124,17 @@ namespace Microsoft.AspNetCore.Sockets
// Raise OnConnected for new connections only since polls happen all the time // Raise OnConnected for new connections only since polls happen all the time
if (isNewConnection) if (isNewConnection)
{ {
// REVIEW: We should await this task after disposing the connection
var ignore = endpoint.OnConnected(connectionState.Connection); var ignore = endpoint.OnConnected(connectionState.Connection);
} }
var longPolling = new LongPolling((HttpChannel)connectionState.Connection.Channel); var channel = (HttpChannel)connectionState.Connection.Channel;
var longPolling = new LongPolling(channel);
// Register this transport for disconnect
RegisterDisconnect(context, longPolling); RegisterDisconnect(context, longPolling);
// Start the transport
await longPolling.ProcessRequest(context); await longPolling.ProcessRequest(context);
_manager.MarkConnectionInactive(connectionState.Connection.ConnectionId); _manager.MarkConnectionInactive(connectionState.Connection.ConnectionId);