This repository has been archived by the owner on May 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
FastcgiServer.cs
207 lines (176 loc) · 5.68 KB
/
FastcgiServer.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.IO;
using System.Runtime.InteropServices;
using System.Reflection;
namespace CSharpUtils.Fastcgi
{
public class FastcgiServer
{
ManualResetEvent AcceptMutex;
String Address;
ushort Port;
//TcpListener TcpListener;
Socket ServerSocket;
bool Async = false;
bool Debug = false;
//bool Debug = true;
//[DllImport("Kernel32")]
//public static extern bool SetConsoleCtrlHandler(HandlerRoutine Handler, bool Add);
// A delegate type to be used as the handler routine
// for SetConsoleCtrlHandler.
//public delegate bool HandlerRoutine(CtrlTypes CtrlType);
// An enumerated type for the control messages
// sent to the handler routine.
public enum CtrlTypes
{
CTRL_C_EVENT = 0,
CTRL_BREAK_EVENT,
CTRL_CLOSE_EVENT,
CTRL_LOGOFF_EVENT = 5,
CTRL_SHUTDOWN_EVENT
}
event Action OnShutdown;
/// <summary>
/// Handles the shutdown of the server to be able to execute code.
/// </summary>
/// <param name="CtrlType"></param>
/// <returns>True to avoid exiting. False to allow exiting.</returns>
private bool HandleShutdown(CtrlTypes CtrlType)
{
Console.WriteLine("Closing... ({0})", CtrlType);
if (OnShutdown != null) OnShutdown();
return false;
}
public void Listen(string UnixEndPoint)
{
ServerSocket = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.IP);
//EndPoint EndPoint = (EndPoint)Assembly.LoadWithPartialName("Mono.Posix").GetType("Mono.Unix.UnixEndPoint").GetConstructor(new Type[] { typeof(String) }).Invoke(null, new object[] { UnixEndPoint });
//EndPoint EndPoint = (EndPoint)Assembly.LoadWithPartialName("Mono.Posix").GetType("Mono.Unix.UnixEndPoint").GetConstructor(new Type[] { typeof(String) }).Invoke(new object[] { UnixEndPoint });
EndPoint EndPoint = (EndPoint)Assembly.Load("Mono.Posix").GetType("Mono.Unix.UnixEndPoint").GetConstructor(new Type[] { typeof(String) }).Invoke(new object[] { UnixEndPoint });
Console.WriteLine(EndPoint);
ServerSocket.Bind(EndPoint);
Console.WriteLine("Listen at '{0}'", UnixEndPoint);
ServerSocket.Listen(128);
ListenCommon();
}
private void ListenCommon()
{
//SetConsoleCtrlHandler(new HandlerRoutine(HandleShutdown), true);
while (true)
{
if (Debug)
{
Console.WriteLine("Waiting a connection...");
}
if (Async)
{
/*
AcceptMutex.Reset();
{
TcpListener.BeginAcceptSocket(new AsyncCallback(HandleAcceptedCallbackAsync), TcpListener);
}
AcceptMutex.WaitOne();
*/
}
else
{
//ThreadPool.QueueUserWorkItem(HandleAcceptedSocket, TcpListener.AcceptSocket());
//ThreadPool.
HandleAcceptedSocketInNewThread(ServerSocket.Accept());
}
}
}
public void Listen(ushort Port, string Address = "0.0.0.0")
{
this.Port = Port;
this.Address = Address;
AcceptMutex = new ManualResetEvent(false);
ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
ServerSocket.Bind(new IPEndPoint(IPAddress.Parse(Address), Port));
Console.WriteLine("Listen at {0}:{1}", Address, Port);
ServerSocket.Listen(128);
ListenCommon();
}
protected void HandleAcceptedSocketInNewThread(Socket AcceptedSocket)
{
ThreadPool.QueueUserWorkItem(HandleAcceptedSocket, AcceptedSocket);
/*
new Thread(delegate()
{
HandleAcceptedSocket(AcceptedSocket);
}).Start();
*/
}
/*
class ConnectionState
{
public AsyncCallback ReadAsyncCallback;
public Socket ClientSocket;
public const int DataSize = 1024;
public byte[] Data = new byte[DataSize];
public ProduceConsumeBuffer<byte> Buffer = new ProduceConsumeBuffer<byte>();
public ConnectionState(Socket ClientSocket, AsyncCallback ReadAsyncCallback)
{
this.ClientSocket = ClientSocket;
this.ReadAsyncCallback = ReadAsyncCallback;
}
public void BeginReceive()
{
ClientSocket.BeginReceive(Data, 0, DataSize, 0, ReadAsyncCallback, this);
}
}
void HandleAcceptedCallbackAsync(IAsyncResult AsyncResult) {
Socket ClientSocket = TcpListener.EndAcceptSocket(AsyncResult);
AcceptMutex.Set();
var ConnectionState = new ConnectionState(ClientSocket, new AsyncCallback(HandleReadCallbackAsync));
ConnectionState.BeginReceive();
Console.WriteLine("HandleAcceptedCallbackAsync");
}
void HandleReadCallbackAsync(IAsyncResult AsyncResult)
{
ConnectionState ConnectionState = (ConnectionState)AsyncResult.AsyncState;
Socket ClientSocket = ConnectionState.ClientSocket;
int ReadedCount = ClientSocket.EndReceive(AsyncResult);
Console.WriteLine("HandleReadCallbackAsync");
if (ReadedCount > 0)
{
ConnectionState.Buffer.Produce(ConnectionState.Data, 0, ReadedCount);
Console.WriteLine(ConnectionState.Buffer.ConsumeRemaining);
ConnectionState.BeginReceive();
}
else
{
ClientSocket.Close();
}
}
*/
protected void HandleAcceptedSocket(Object _Socket)
{
var Socket = (Socket)_Socket;
if (Debug)
{
Console.WriteLine("HandleAcceptedSocket: " + Socket);
}
//Console.Write(".");
var FastcgiHandler = new FastcgiHandler(new FastcgiPipeSocket(Socket), Debug);
FastcgiHandler.HandleFastcgiRequest += HandleFascgiRequest;
FastcgiHandler.Reader.ReadAllPackets();
}
virtual protected void HandleFascgiRequest(FastcgiRequest FastcgiRequest)
{
using (var TextWriter = new StreamWriter(FastcgiRequest.StdoutStream))
{
TextWriter.WriteLine("X-Dynamic: C#");
TextWriter.WriteLine("Content-Type: text/html; charset=utf-8");
TextWriter.WriteLine("");
TextWriter.WriteLine("Not Implemented");
}
}
}
}