This repository has been archived by the owner on Feb 11, 2023. It is now read-only.
/
Server.cs
260 lines (230 loc) · 8.51 KB
/
Server.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/* **********************************************************************************
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This source code is subject to terms and conditions of the Microsoft Public
* License (Ms-PL). A copy of the license can be found in the license.htm file
* included in this distribution.
*
* You must not remove this notice, or any other, from this software.
*
* **********************************************************************************/
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Remoting;
using System.Threading;
using System.Web;
using System.Web.Hosting;
using System.Web.Compilation;
namespace Cassini
{
public class Server : MarshalByRefObject
{
string _virtualPath;
string _physicalPath;
bool _shutdownInProgress;
Host _host;
m2net.Connection _mongrel2Connection;
/// <summary>
/// This takes ownership of the mongrel2Connection. It will be disposed when the server is stopped
/// and the connection should probably not be reused anyways due to the threading issue with
/// ZMQ.
/// </summary>
/// <param name="mongrel2Connection"></param>
/// <param name="virtualPath"></param>
/// <param name="physicalPath"></param>
public Server(m2net.Connection mongrel2Connection, string virtualPath, string physicalPath)
{
_mongrel2Connection = mongrel2Connection;
_virtualPath = virtualPath;
_physicalPath = physicalPath[physicalPath.Length - 1] == Path.DirectorySeparatorChar ? physicalPath : physicalPath + Path.DirectorySeparatorChar;
}
public override object InitializeLifetimeService()
{
// never expire the license
return null;
}
public m2net.Connection Mongrel2Connection
{
get
{
return this._mongrel2Connection;
}
}
public string VirtualPath
{
get
{
return _virtualPath;
}
}
public string PhysicalPath
{
get
{
return _physicalPath;
}
}
//
// Socket listening
//
static Socket CreateSocketBindAndListen(AddressFamily family, IPAddress address, int port)
{
var socket = new Socket(family, SocketType.Stream, ProtocolType.Tcp);
socket.Bind(new IPEndPoint(address, port));
socket.Listen((int)SocketOptionName.MaxConnections);
return socket;
}
public void Start()
{
ThreadPool.QueueUserWorkItem(delegate
{
while (!_shutdownInProgress)
{
try
{
m2net.Request acceptedSocket = _mongrel2Connection.Receive();
ThreadPool.QueueUserWorkItem(delegate
{
if (!_shutdownInProgress)
{
var conn = new Connection(this, acceptedSocket);
//// wait for at least some input
//if (conn.WaitForRequestBytes() == 0)
//{
// conn.WriteErrorAndClose(400);
// return;
//}
// find or create host
Host host = GetHost();
if (host == null)
{
conn.WriteErrorAndClose(500);
return;
}
// process request in worker app domain
host.ProcessRequest(conn);
}
});
}
catch
{
Thread.Sleep(100);
}
}
});
}
public void Stop()
{
_shutdownInProgress = true;
try
{
if (_mongrel2Connection != null)
{
_mongrel2Connection.Dispose();
}
}
catch
{
}
finally
{
_mongrel2Connection = null;
}
try
{
if (_host != null)
{
_host.Shutdown();
}
while (_host != null)
{
Thread.Sleep(100);
}
}
catch
{
}
finally
{
_host = null;
}
}
// called at the end of request processing
// to disconnect the remoting proxy for Connection object
// and allow GC to pick it up
internal void OnRequestEnd(Connection conn)
{
RemotingServices.Disconnect(conn);
}
public void HostStopped()
{
_host = null;
}
Host GetHost()
{
if (_shutdownInProgress)
return null;
Host host = _host;
if (host == null)
{
lock (this)
{
host = _host;
if (host == null)
{
host = (Host)CreateWorkerAppDomainWithHost(_virtualPath, _physicalPath, typeof(Host));
host.Configure(this, _virtualPath, _physicalPath);
_host = host;
}
}
}
return host;
}
static object CreateWorkerAppDomainWithHost(string virtualPath, string physicalPath, Type hostType)
{
// this creates worker app domain in a way that host doesn't need to be in GAC or bin
// using BuildManagerHost via private reflection
string uniqueAppString = string.Concat(virtualPath, physicalPath).ToLowerInvariant();
string appId = (uniqueAppString.GetHashCode()).ToString("x", CultureInfo.InvariantCulture);
// create BuildManagerHost in the worker app domain
var appManager = ApplicationManager.GetApplicationManager();
// call BuildManagerHost.RegisterAssembly to make Host type loadable in the worker app domain
var buildManagerHostType = typeof(HttpRuntime).Assembly.GetType("System.Web.Compilation.BuildManagerHost");
if (buildManagerHostType != null)
{
//on the Microsoft .NET framework we can make sure the DLL is loaded into the ASP.NET AppDomain by
//using this internal path. A nice solution does not exist for Mono (as far as I can tell), so this
//DLL will just need to be in the probing path or in the GAC.
var buildManagerHost = appManager.CreateObject(appId, buildManagerHostType, virtualPath, physicalPath, false);
buildManagerHostType.InvokeMember(
"RegisterAssembly",
BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.NonPublic,
null,
buildManagerHost,
new object[2] { hostType.Assembly.FullName, hostType.Assembly.Location });
}
// create Host in the worker app domain
try
{
return appManager.CreateObject(appId, hostType, virtualPath, physicalPath, false);
}
catch (FileNotFoundException ex)
{
Console.WriteLine(ex);
Console.WriteLine();
Console.WriteLine("Failed to create host type.");
Console.WriteLine("m2net.AspNetHandler.dll needs to either be put into the GAC or the directory containing it has to be put on");
Console.WriteLine("web app's probing path via its web.config file. See:");
Console.WriteLine("http://msdn.microsoft.com/en-us/library/823z9h8w(VS.80).aspx");
return null;
}
}
}
}