Skip to content
This repository has been archived by the owner on Dec 18, 2018. It is now read-only.

Commit

Permalink
Add IHttpConnectionFeature.ConnectionId.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tratcher committed Feb 17, 2016
1 parent f89c959 commit a97f6b3
Show file tree
Hide file tree
Showing 15 changed files with 113 additions and 117 deletions.
32 changes: 15 additions & 17 deletions src/Microsoft.AspNetCore.Server.Kestrel/Http/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Globalization;
using System.Net;
using System.Threading;
using Microsoft.AspNetCore.Server.Kestrel.Filter;
Expand All @@ -24,40 +25,36 @@ public class Connection : ConnectionContext, IConnectionControl
private Frame _frame;
private ConnectionFilterContext _filterContext;
private LibuvStream _libuvStream;
private readonly long _connectionId;

private readonly SocketInput _rawSocketInput;
private readonly SocketOutput _rawSocketOutput;

private readonly object _stateLock = new object();
private ConnectionState _connectionState;

private IPEndPoint _remoteEndPoint;
private IPEndPoint _localEndPoint;

public Connection(ListenerContext context, UvStreamHandle socket) : base(context)
{
_socket = socket;
ConnectionControl = this;

_connectionId = Interlocked.Increment(ref _lastConnectionId);
ConnectionId = Interlocked.Increment(ref _lastConnectionId).ToString(CultureInfo.InvariantCulture);

_rawSocketInput = new SocketInput(Memory2, ThreadPool);
_rawSocketOutput = new SocketOutput(Thread, _socket, Memory2, this, _connectionId, Log, ThreadPool, WriteReqPool);
_rawSocketOutput = new SocketOutput(Thread, _socket, Memory2, this, ConnectionId, Log, ThreadPool, WriteReqPool);
}

public void Start()
{
Log.ConnectionStart(_connectionId);
Log.ConnectionStart(ConnectionId);

// Start socket prior to applying the ConnectionFilter
_socket.ReadStart(_allocCallback, _readCallback, this);

var tcpHandle = _socket as UvTcpHandle;
if (tcpHandle != null)
{
_remoteEndPoint = tcpHandle.GetPeerIPEndPoint();
_localEndPoint = tcpHandle.GetSockIPEndPoint();
RemoteEndPoint = tcpHandle.GetPeerIPEndPoint();
LocalEndPoint = tcpHandle.GetSockIPEndPoint();
}

// Don't initialize _frame until SocketInput and SocketOutput are set to their final values.
Expand All @@ -78,6 +75,7 @@ public void Start()
Connection = _libuvStream,
Address = ServerAddress
};
PrepareRequest = _filterContext.PrepareRequest;

try
{
Expand Down Expand Up @@ -170,12 +168,12 @@ private void OnRead(UvStreamHandle handle, int status)

if (normalRead)
{
Log.ConnectionRead(_connectionId, readCount);
Log.ConnectionRead(ConnectionId, readCount);
}
else
{
_socket.ReadStop();
Log.ConnectionReadFin(_connectionId);
Log.ConnectionReadFin(ConnectionId);
}

Exception error = null;
Expand All @@ -194,18 +192,18 @@ private void OnRead(UvStreamHandle handle, int status)

private Frame CreateFrame()
{
return FrameFactory(this, _remoteEndPoint, _localEndPoint, _filterContext?.PrepareRequest);
return FrameFactory(this);
}

void IConnectionControl.Pause()
{
Log.ConnectionPause(_connectionId);
Log.ConnectionPause(ConnectionId);
_socket.ReadStop();
}

void IConnectionControl.Resume()
{
Log.ConnectionResume(_connectionId);
Log.ConnectionResume(ConnectionId);
_socket.ReadStart(_allocCallback, _readCallback, this);
}

Expand All @@ -222,7 +220,7 @@ void IConnectionControl.End(ProduceEndType endType)
}
_connectionState = ConnectionState.Shutdown;

Log.ConnectionWriteFin(_connectionId);
Log.ConnectionWriteFin(ConnectionId);
_rawSocketOutput.End(endType);
break;
case ProduceEndType.ConnectionKeepAlive:
Expand All @@ -231,7 +229,7 @@ void IConnectionControl.End(ProduceEndType endType)
return;
}

Log.ConnectionKeepAlive(_connectionId);
Log.ConnectionKeepAlive(ConnectionId);
break;
case ProduceEndType.SocketDisconnect:
if (_connectionState == ConnectionState.Disconnected)
Expand All @@ -240,7 +238,7 @@ void IConnectionControl.End(ProduceEndType endType)
}
_connectionState = ConnectionState.Disconnected;

Log.ConnectionDisconnect(_connectionId);
Log.ConnectionDisconnect(ConnectionId);
_rawSocketOutput.End(endType);
break;
}
Expand Down
18 changes: 18 additions & 0 deletions src/Microsoft.AspNetCore.Server.Kestrel/Http/ConnectionContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Net;
using Microsoft.AspNetCore.Http.Features;

namespace Microsoft.AspNetCore.Server.Kestrel.Http
{
public class ConnectionContext : ListenerContext
Expand All @@ -18,10 +22,24 @@ public ConnectionContext(ConnectionContext context) : base(context)
SocketInput = context.SocketInput;
SocketOutput = context.SocketOutput;
ConnectionControl = context.ConnectionControl;
RemoteEndPoint = context.RemoteEndPoint;
LocalEndPoint = context.LocalEndPoint;
ConnectionId = context.ConnectionId;
PrepareRequest = context.PrepareRequest;
}

public SocketInput SocketInput { get; set; }

public ISocketOutput SocketOutput { get; set; }

public IConnectionControl ConnectionControl { get; set; }

public IPEndPoint RemoteEndPoint { get; set; }

public IPEndPoint LocalEndPoint { get; set; }

public string ConnectionId { get; set; }

public Action<IFeatureCollection> PrepareRequest { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@ bool IHttpUpgradeFeature.IsUpgradableRequest

int IHttpConnectionFeature.LocalPort { get; set; }

string IHttpConnectionFeature.ConnectionId { get; set; }

object IFeatureCollection.this[Type key]
{
get { return FastFeatureGet(key); }
Expand Down
27 changes: 7 additions & 20 deletions src/Microsoft.AspNetCore.Server.Kestrel/Http/Frame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,26 +71,11 @@ public abstract partial class Frame : FrameContext, IFrameControl

private HttpVersionType _httpVersion;

private readonly IPEndPoint _localEndPoint;
private readonly IPEndPoint _remoteEndPoint;
private readonly Action<IFeatureCollection> _prepareRequest;

private readonly string _pathBase;

public Frame(ConnectionContext context)
: this(context, remoteEndPoint: null, localEndPoint: null, prepareRequest: null)
{
}

public Frame(ConnectionContext context,
IPEndPoint remoteEndPoint,
IPEndPoint localEndPoint,
Action<IFeatureCollection> prepareRequest)
: base(context)
{
_remoteEndPoint = remoteEndPoint;
_localEndPoint = localEndPoint;
_prepareRequest = prepareRequest;
_pathBase = context.ServerAddress.PathBase;
if (ReuseStreams)
{
Expand Down Expand Up @@ -227,13 +212,15 @@ public void Reset()
DuplexStream = null;

var httpConnectionFeature = this as IHttpConnectionFeature;
httpConnectionFeature.RemoteIpAddress = _remoteEndPoint?.Address;
httpConnectionFeature.RemotePort = _remoteEndPoint?.Port ?? 0;
httpConnectionFeature.RemoteIpAddress = RemoteEndPoint?.Address;
httpConnectionFeature.RemotePort = RemoteEndPoint?.Port ?? 0;

httpConnectionFeature.LocalIpAddress = LocalEndPoint?.Address;
httpConnectionFeature.LocalPort = LocalEndPoint?.Port ?? 0;

httpConnectionFeature.LocalIpAddress = _localEndPoint?.Address;
httpConnectionFeature.LocalPort = _localEndPoint?.Port ?? 0;
httpConnectionFeature.ConnectionId = ConnectionId;

_prepareRequest?.Invoke(this);
PrepareRequest?.Invoke(this);

_manuallySetRequestAbortToken = null;
_abortedCts = null;
Expand Down
11 changes: 1 addition & 10 deletions src/Microsoft.AspNetCore.Server.Kestrel/Http/FrameOfT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,7 @@ public class Frame<TContext> : Frame

public Frame(IHttpApplication<TContext> application,
ConnectionContext context)
: this(application, context, remoteEndPoint: null, localEndPoint: null, prepareRequest: null)
{
}

public Frame(IHttpApplication<TContext> application,
ConnectionContext context,
IPEndPoint remoteEndPoint,
IPEndPoint localEndPoint,
Action<IFeatureCollection> prepareRequest)
: base(context, remoteEndPoint, localEndPoint, prepareRequest)
: base(context)
{
_application = application;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.AspNetCore.Server.Kestrel/Http/SocketOutput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class SocketOutput : ISocketOutput
private readonly KestrelThread _thread;
private readonly UvStreamHandle _socket;
private readonly Connection _connection;
private readonly long _connectionId;
private readonly string _connectionId;
private readonly IKestrelTrace _log;
private readonly IThreadPool _threadPool;

Expand Down Expand Up @@ -58,7 +58,7 @@ public SocketOutput(
UvStreamHandle socket,
MemoryPool2 memory,
Connection connection,
long connectionId,
string connectionId,
IKestrelTrace log,
IThreadPool threadPool,
Queue<UvWriteReq> writeReqPool)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,33 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Infrastructure
{
public interface IKestrelTrace : ILogger
{
void ConnectionStart(long connectionId);
void ConnectionStart(string connectionId);

void ConnectionStop(long connectionId);
void ConnectionStop(string connectionId);

void ConnectionRead(long connectionId, int count);
void ConnectionRead(string connectionId, int count);

void ConnectionPause(long connectionId);
void ConnectionPause(string connectionId);

void ConnectionResume(long connectionId);
void ConnectionResume(string connectionId);

void ConnectionReadFin(long connectionId);
void ConnectionReadFin(string connectionId);

void ConnectionWriteFin(long connectionId);
void ConnectionWriteFin(string connectionId);

void ConnectionWroteFin(long connectionId, int status);
void ConnectionWroteFin(string connectionId, int status);

void ConnectionKeepAlive(long connectionId);
void ConnectionKeepAlive(string connectionId);

void ConnectionDisconnect(long connectionId);
void ConnectionDisconnect(string connectionId);

void ConnectionWrite(long connectionId, int count);
void ConnectionWrite(string connectionId, int count);

void ConnectionWriteCallback(long connectionId, int status);
void ConnectionWriteCallback(string connectionId, int status);

void ConnectionError(long connectionId, Exception ex);
void ConnectionError(string connectionId, Exception ex);

void ConnectionDisconnectedWrite(long connectionId, int count, Exception ex);
void ConnectionDisconnectedWrite(string connectionId, int count, Exception ex);

void ApplicationError(Exception ex);
}
Expand Down
Loading

0 comments on commit a97f6b3

Please sign in to comment.