diff --git a/src/Microsoft.AspNet.Server.Kestrel/Http/Listener.cs b/src/Microsoft.AspNet.Server.Kestrel/Http/Listener.cs index 33657ea73c..8485869296 100644 --- a/src/Microsoft.AspNet.Server.Kestrel/Http/Listener.cs +++ b/src/Microsoft.AspNet.Server.Kestrel/Http/Listener.cs @@ -13,9 +13,8 @@ namespace Microsoft.AspNet.Server.Kestrel.Http /// public abstract class Listener : ListenerContext, IDisposable { - protected Listener(IMemoryPool memory) + protected Listener(ServiceContext serviceContext) : base(serviceContext) { - Memory = memory; } protected UvStreamHandle ListenSocket { get; private set; } diff --git a/src/Microsoft.AspNet.Server.Kestrel/Http/ListenerContext.cs b/src/Microsoft.AspNet.Server.Kestrel/Http/ListenerContext.cs index 4285f59311..1d8cd86059 100644 --- a/src/Microsoft.AspNet.Server.Kestrel/Http/ListenerContext.cs +++ b/src/Microsoft.AspNet.Server.Kestrel/Http/ListenerContext.cs @@ -10,11 +10,16 @@ namespace Microsoft.AspNet.Server.Kestrel.Http { public ListenerContext() { } - public ListenerContext(ListenerContext context) + public ListenerContext(ServiceContext serviceContext) { - Thread = context.Thread; - Application = context.Application; - Memory = context.Memory; + Memory = serviceContext.Memory; + } + + public ListenerContext(ListenerContext listenerContext) + { + Thread = listenerContext.Thread; + Application = listenerContext.Application; + Memory = listenerContext.Memory; } public KestrelThread Thread { get; set; } diff --git a/src/Microsoft.AspNet.Server.Kestrel/Http/ListenerPrimary.cs b/src/Microsoft.AspNet.Server.Kestrel/Http/ListenerPrimary.cs index 386489e88e..67204fc729 100644 --- a/src/Microsoft.AspNet.Server.Kestrel/Http/ListenerPrimary.cs +++ b/src/Microsoft.AspNet.Server.Kestrel/Http/ListenerPrimary.cs @@ -22,7 +22,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http // but it has no other functional significance private readonly ArraySegment> _dummyMessage = new ArraySegment>(new[] { new ArraySegment(new byte[] { 1, 2, 3, 4 }) }); - protected ListenerPrimary(IMemoryPool memory) : base(memory) + protected ListenerPrimary(ServiceContext serviceContext) : base(serviceContext) { } diff --git a/src/Microsoft.AspNet.Server.Kestrel/Http/ListenerSecondary.cs b/src/Microsoft.AspNet.Server.Kestrel/Http/ListenerSecondary.cs index 26105aee87..f0948b867b 100644 --- a/src/Microsoft.AspNet.Server.Kestrel/Http/ListenerSecondary.cs +++ b/src/Microsoft.AspNet.Server.Kestrel/Http/ListenerSecondary.cs @@ -15,9 +15,8 @@ namespace Microsoft.AspNet.Server.Kestrel.Http /// public abstract class ListenerSecondary : ListenerContext, IDisposable { - protected ListenerSecondary(IMemoryPool memory) + protected ListenerSecondary(ServiceContext serviceContext) : base(serviceContext) { - Memory = memory; } UvPipeHandle DispatchPipe { get; set; } diff --git a/src/Microsoft.AspNet.Server.Kestrel/Http/PipeListener.cs b/src/Microsoft.AspNet.Server.Kestrel/Http/PipeListener.cs index 15b43c78b6..2fd4dd3c32 100644 --- a/src/Microsoft.AspNet.Server.Kestrel/Http/PipeListener.cs +++ b/src/Microsoft.AspNet.Server.Kestrel/Http/PipeListener.cs @@ -11,7 +11,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http /// public class PipeListener : Listener { - public PipeListener(IMemoryPool memory) : base(memory) + public PipeListener(ServiceContext serviceContext) : base(serviceContext) { } diff --git a/src/Microsoft.AspNet.Server.Kestrel/Http/PipeListenerPrimary.cs b/src/Microsoft.AspNet.Server.Kestrel/Http/PipeListenerPrimary.cs index 3ee7f501bc..afb4c7c213 100644 --- a/src/Microsoft.AspNet.Server.Kestrel/Http/PipeListenerPrimary.cs +++ b/src/Microsoft.AspNet.Server.Kestrel/Http/PipeListenerPrimary.cs @@ -11,7 +11,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http /// public class PipeListenerPrimary : ListenerPrimary { - public PipeListenerPrimary(IMemoryPool memory) : base(memory) + public PipeListenerPrimary(ServiceContext serviceContext) : base(serviceContext) { } diff --git a/src/Microsoft.AspNet.Server.Kestrel/Http/PipeListenerSecondary.cs b/src/Microsoft.AspNet.Server.Kestrel/Http/PipeListenerSecondary.cs index 81d70485bf..40b34038d9 100644 --- a/src/Microsoft.AspNet.Server.Kestrel/Http/PipeListenerSecondary.cs +++ b/src/Microsoft.AspNet.Server.Kestrel/Http/PipeListenerSecondary.cs @@ -10,7 +10,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http /// public class PipeListenerSecondary : ListenerSecondary { - public PipeListenerSecondary(IMemoryPool memory) : base(memory) + public PipeListenerSecondary(ServiceContext serviceContext) : base(serviceContext) { } diff --git a/src/Microsoft.AspNet.Server.Kestrel/Http/TcpListener.cs b/src/Microsoft.AspNet.Server.Kestrel/Http/TcpListener.cs index 54739b725d..7c67208e3f 100644 --- a/src/Microsoft.AspNet.Server.Kestrel/Http/TcpListener.cs +++ b/src/Microsoft.AspNet.Server.Kestrel/Http/TcpListener.cs @@ -12,7 +12,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http /// public class TcpListener : Listener { - public TcpListener(IMemoryPool memory) : base(memory) + public TcpListener(ServiceContext serviceContext) : base(serviceContext) { } diff --git a/src/Microsoft.AspNet.Server.Kestrel/Http/TcpListenerPrimary.cs b/src/Microsoft.AspNet.Server.Kestrel/Http/TcpListenerPrimary.cs index 8a9546fdd8..098238bde1 100644 --- a/src/Microsoft.AspNet.Server.Kestrel/Http/TcpListenerPrimary.cs +++ b/src/Microsoft.AspNet.Server.Kestrel/Http/TcpListenerPrimary.cs @@ -12,7 +12,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http /// public class TcpListenerPrimary : ListenerPrimary { - public TcpListenerPrimary(IMemoryPool memory) : base(memory) + public TcpListenerPrimary(ServiceContext serviceContext) : base(serviceContext) { } diff --git a/src/Microsoft.AspNet.Server.Kestrel/Http/TcpListenerSecondary.cs b/src/Microsoft.AspNet.Server.Kestrel/Http/TcpListenerSecondary.cs index 5e125c0276..4cff07cedf 100644 --- a/src/Microsoft.AspNet.Server.Kestrel/Http/TcpListenerSecondary.cs +++ b/src/Microsoft.AspNet.Server.Kestrel/Http/TcpListenerSecondary.cs @@ -10,7 +10,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http /// public class TcpListenerSecondary : ListenerSecondary { - public TcpListenerSecondary(IMemoryPool memory) : base(memory) + public TcpListenerSecondary(ServiceContext serviceContext) : base(serviceContext) { } diff --git a/src/Microsoft.AspNet.Server.Kestrel/Infrastructure/KestrelThread.cs b/src/Microsoft.AspNet.Server.Kestrel/Infrastructure/KestrelThread.cs index 5a0338d899..f611d8857a 100644 --- a/src/Microsoft.AspNet.Server.Kestrel/Infrastructure/KestrelThread.cs +++ b/src/Microsoft.AspNet.Server.Kestrel/Infrastructure/KestrelThread.cs @@ -8,6 +8,7 @@ using System.Diagnostics; using System.Runtime.ExceptionServices; using System.Threading; using System.Threading.Tasks; +using Microsoft.Dnx.Runtime; namespace Microsoft.AspNet.Server.Kestrel { @@ -18,6 +19,7 @@ namespace Microsoft.AspNet.Server.Kestrel { private static Action _objectCallbackAdapter = (callback, state) => ((Action)callback).Invoke(state); private KestrelEngine _engine; + private readonly IApplicationShutdown _appShutdown; private Thread _thread; private UvLoopHandle _loop; private UvAsyncHandle _post; @@ -29,9 +31,10 @@ namespace Microsoft.AspNet.Server.Kestrel private bool _stopImmediate = false; private ExceptionDispatchInfo _closeError; - public KestrelThread(KestrelEngine engine) + public KestrelThread(KestrelEngine engine, ServiceContext serviceContext) { _engine = engine; + _appShutdown = serviceContext.AppShutdown; _loop = new UvLoopHandle(); _post = new UvAsyncHandle(); _thread = new Thread(ThreadStart); @@ -226,7 +229,7 @@ namespace Microsoft.AspNet.Server.Kestrel _closeError = ExceptionDispatchInfo.Capture(ex); // Request shutdown so we can rethrow this exception // in Stop which should be observable. - _engine.AppShutdown.RequestShutdown(); + _appShutdown.RequestShutdown(); } } diff --git a/src/Microsoft.AspNet.Server.Kestrel/KestrelEngine.cs b/src/Microsoft.AspNet.Server.Kestrel/KestrelEngine.cs index 161ba83d07..57fc44876a 100644 --- a/src/Microsoft.AspNet.Server.Kestrel/KestrelEngine.cs +++ b/src/Microsoft.AspNet.Server.Kestrel/KestrelEngine.cs @@ -14,6 +14,8 @@ namespace Microsoft.AspNet.Server.Kestrel { public class KestrelEngine : IDisposable { + private readonly ServiceContext _serviceContext; + public KestrelEngine(ILibraryManager libraryManager, IApplicationShutdown appShutdownService) : this(appShutdownService) { @@ -68,21 +70,23 @@ namespace Microsoft.AspNet.Server.Kestrel private KestrelEngine(IApplicationShutdown appShutdownService) { - AppShutdown = appShutdownService; + _serviceContext = new ServiceContext + { + AppShutdown = appShutdownService, + Memory = new MemoryPool() + }; + Threads = new List(); - Memory = new MemoryPool(); } public Libuv Libuv { get; private set; } - public IMemoryPool Memory { get; set; } - public IApplicationShutdown AppShutdown { get; private set; } public List Threads { get; private set; } public void Start(int count) { for (var index = 0; index != count; ++index) { - Threads.Add(new KestrelThread(this)); + Threads.Add(new KestrelThread(this, _serviceContext)); } foreach (var thread in Threads) @@ -122,16 +126,16 @@ namespace Microsoft.AspNet.Server.Kestrel if (single) { var listener = usingPipes ? - (Listener) new PipeListener(Memory) : - new TcpListener(Memory); + (Listener) new PipeListener(_serviceContext) : + new TcpListener(_serviceContext); listeners.Add(listener); listener.StartAsync(scheme, host, port, thread, application).Wait(); } else if (first) { var listener = usingPipes - ? (ListenerPrimary) new PipeListenerPrimary(Memory) - : new TcpListenerPrimary(Memory); + ? (ListenerPrimary) new PipeListenerPrimary(_serviceContext) + : new TcpListenerPrimary(_serviceContext); listeners.Add(listener); listener.StartAsync(pipeName, scheme, host, port, thread, application).Wait(); @@ -139,8 +143,8 @@ namespace Microsoft.AspNet.Server.Kestrel else { var listener = usingPipes - ? (ListenerSecondary) new PipeListenerSecondary(Memory) - : new TcpListenerSecondary(Memory); + ? (ListenerSecondary) new PipeListenerSecondary(_serviceContext) + : new TcpListenerSecondary(_serviceContext); listeners.Add(listener); listener.StartAsync(pipeName, thread, application).Wait(); } diff --git a/src/Microsoft.AspNet.Server.Kestrel/ServiceContext.cs b/src/Microsoft.AspNet.Server.Kestrel/ServiceContext.cs new file mode 100644 index 0000000000..bd09076565 --- /dev/null +++ b/src/Microsoft.AspNet.Server.Kestrel/ServiceContext.cs @@ -0,0 +1,15 @@ +// 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 Microsoft.AspNet.Server.Kestrel.Http; +using Microsoft.Dnx.Runtime; + +namespace Microsoft.AspNet.Server.Kestrel +{ + public class ServiceContext + { + public IApplicationShutdown AppShutdown { get; set; } + + public IMemoryPool Memory { get; set; } + } +}