0
|
1 ???using System; |
|
2 using System.Net; |
|
3 using System.Net.Sockets; |
|
4 using System.Threading; |
|
5 using System.Collections.Generic; |
|
6 |
|
7 namespace UCIS.Net { |
|
8 public class TCPServer { |
|
9 public class ModuleCollection : Dictionary<byte, IModule> { |
|
10 public void Add(char Key, IModule Value) { |
|
11 base.Add((byte)Key, Value); |
|
12 } |
|
13 public void Remove(char Key) { |
|
14 base.Remove((byte)Key); |
|
15 } |
|
16 } |
|
17 |
|
18 public class ClientAcceptedEventArgs : EventArgs { |
|
19 private TCPStream _client; |
|
20 internal ClientAcceptedEventArgs(TCPStream client) { |
|
21 _client = client; |
|
22 } |
|
23 public TCPStream Client { get { return _client; } } |
|
24 } |
|
25 |
|
26 public event EventHandler<ClientAcceptedEventArgs> ClientAccepted; |
|
27 |
|
28 private Socket _Listener; |
|
29 private UCIS.ThreadPool _ThreadPool; |
|
30 private NetworkConnectionList _Clients = new NetworkConnectionList(); |
|
31 private ModuleCollection _Modules = new ModuleCollection(); |
|
32 private IModule _CatchAllModule = null; |
|
33 private Int32 _ThrottleCounter; |
|
34 private Int32 _ThrottleBurst = 10; |
|
35 private Int32 _ThrottleRate = 0; |
|
36 private Timer _ThrottleTimer = null; |
|
37 |
|
38 public TCPServer() { |
|
39 _ThreadPool = UCIS.ThreadPool.DefaultPool; |
|
40 } |
|
41 |
|
42 public NetworkConnectionList Clients { |
|
43 get { return _Clients; } |
|
44 } |
|
45 |
|
46 public ModuleCollection Modules { |
|
47 get { return _Modules; } |
|
48 } |
|
49 |
|
50 public IModule CatchAllModule { |
|
51 get { |
|
52 return _CatchAllModule; |
|
53 } |
|
54 set { |
|
55 _CatchAllModule = value; |
|
56 } |
|
57 } |
|
58 |
|
59 public Int32 ThrottleRate { |
|
60 get { return _ThrottleRate; } |
|
61 set { |
|
62 if (value < 0) throw new ArgumentOutOfRangeException("value"); |
|
63 _ThrottleRate = value; |
|
64 if (_Listener == null) return; |
|
65 if (value == 0 && _ThrottleTimer != null) { |
|
66 _ThrottleTimer.Dispose(); |
|
67 _ThrottleTimer = null; |
|
68 } else if (value > 0 && _ThrottleTimer == null) { |
|
69 _ThrottleTimer = new Timer(ThrottleCallback, null, 1000, 1000); |
|
70 } |
|
71 } |
|
72 } |
|
73 public Int32 ThrottleBurst { |
|
74 get { return _ThrottleBurst; } |
|
75 set { |
|
76 if (value < 1) throw new ArgumentOutOfRangeException("value"); |
|
77 _ThrottleBurst = value; |
|
78 } |
|
79 } |
|
80 |
|
81 public EndPoint LocalEndPoint { get { return _Listener.LocalEndPoint; } } |
|
82 |
|
83 public void Listen(int Port) { |
|
84 Listen(AddressFamily.InterNetwork, Port); |
|
85 } |
|
86 public void Listen(AddressFamily af, int Port) { |
|
87 Stop(); |
|
88 |
|
89 _Listener = new Socket(af, SocketType.Stream, ProtocolType.Tcp); |
|
90 |
|
91 _Listener.Bind(new IPEndPoint(af == AddressFamily.InterNetworkV6 ? IPAddress.IPv6Any : IPAddress.Any, Port)); |
|
92 _Listener.Listen(25); |
|
93 _ThrottleCounter = _ThrottleBurst; |
|
94 _Listener.BeginAccept(AcceptCallback, null); |
|
95 |
|
96 if (_ThrottleRate > 0) { |
|
97 _ThrottleTimer = new Timer(ThrottleCallback, null, 1000, 1000); |
|
98 } |
|
99 } |
|
100 |
|
101 public void Stop() { |
|
102 if (_ThrottleTimer != null) _ThrottleTimer.Dispose(); |
|
103 if (_Listener != null && _Listener.IsBound) _Listener.Close(); |
|
104 _Listener = null; |
|
105 } |
|
106 |
|
107 public void Close() { |
|
108 Close(true); |
|
109 } |
|
110 public void Close(bool CloseClients) { |
|
111 Stop(); |
|
112 if (CloseClients) { |
|
113 _Clients.CloseAll(); |
|
114 if (_Clients.Count > 0) Console.WriteLine("TCPServer.Close: Warning: " + _Clients.Count.ToString() + " connections were not properly terminated."); |
|
115 } else { |
|
116 _Clients.Clear(); |
|
117 } |
|
118 } |
|
119 |
|
120 private void AcceptCallback(System.IAsyncResult ar) { |
|
121 Client Client = null; |
|
122 Socket Socket = null; |
|
123 if (_Listener == null) return; |
|
124 try { |
|
125 Socket = _Listener.EndAccept(ar); |
|
126 } catch (ObjectDisposedException) { |
|
127 Console.WriteLine("TCPServer.AcceptCallback Listener object has been disposed. Aborting."); |
|
128 return; |
|
129 } catch (SocketException ex) { |
|
130 Console.WriteLine("TCPServer.AcceptCallback SocketException: " + ex.Message); |
|
131 Socket = null; |
|
132 } |
|
133 if (Socket != null) { |
|
134 try { |
|
135 Client = new Client(Socket, this); |
|
136 _Clients.Add(Client); |
|
137 if (ClientAccepted != null) ClientAccepted(this, new ClientAcceptedEventArgs(Client)); |
|
138 Client.Start(_ThreadPool); |
|
139 } catch (Exception ex) { |
|
140 Console.WriteLine(ex.ToString()); |
|
141 } |
|
142 } |
|
143 if (_ThrottleCounter > 0 || _ThrottleRate == 0) { |
|
144 _ThrottleCounter--; |
|
145 _Listener.BeginAccept(AcceptCallback, null); |
|
146 } |
|
147 } |
|
148 |
|
149 private void ThrottleCallback(Object state) { |
|
150 if (_Listener == null) return; |
|
151 if (_ThrottleRate == 0) return; |
|
152 if (_ThrottleCounter >= _ThrottleBurst) return; |
|
153 if (_ThrottleCounter <= 0) { |
|
154 _Listener.BeginAccept(AcceptCallback, null); |
|
155 } |
|
156 _ThrottleRate += _ThrottleRate; |
|
157 } |
|
158 |
|
159 public interface IModule { |
|
160 // Return value: True = Close connection, False = keep alive |
|
161 bool Accept(TCPStream Stream); |
|
162 } |
|
163 |
|
164 private class Client : TCPStream { |
|
165 private TCPServer _Server; |
|
166 private UCIS.ThreadPool.WorkItem _WorkItem; |
|
167 private IModule _Module; |
|
168 private byte _MagicNumber; |
|
169 |
|
170 public TCPServer Server { |
|
171 get { return _Server; } |
|
172 } |
|
173 public IModule Module { |
|
174 get { return _Module; } |
|
175 } |
|
176 |
|
177 private void _Stream_Closed(object sender, EventArgs e) { |
|
178 _WorkItem = null; |
|
179 _Module = null; |
|
180 _Server = null; |
|
181 base.Closed -= _Stream_Closed; |
|
182 } |
|
183 |
|
184 internal Client(Socket Socket, TCPServer Server) : base(Socket) { |
|
185 _Server = Server; |
|
186 base.Closed += _Stream_Closed; |
|
187 this.Tag = Server; |
|
188 } |
|
189 |
|
190 internal void Start(UCIS.ThreadPool Pool) { |
|
191 _WorkItem = Pool.QueueWorkItem(WorkerProc, null); |
|
192 } |
|
193 |
|
194 private void WorkerProc(object state) { |
|
195 bool CloseSocket = true; |
|
196 try { |
|
197 try { |
|
198 //base.NoDelay = true; |
|
199 base.ReadTimeout = 5000; |
|
200 //Console.WriteLine("TCPServer: Accepted connection from " + base.Socket.RemoteEndPoint.ToString()); |
|
201 _MagicNumber = (byte)base.PeekByte(); |
|
202 } catch (TimeoutException ex) { |
|
203 Console.WriteLine("TCPServer: Caught TimeoutException while reading magic number: " + ex.Message); |
|
204 return; |
|
205 } |
|
206 if (_Server._Modules.TryGetValue(_MagicNumber, out _Module)) { |
|
207 this.Tag = _Module; |
|
208 CloseSocket = _Module.Accept(this); |
|
209 } else if (_Server._CatchAllModule != null) { |
|
210 this.Tag = _Server._CatchAllModule; |
|
211 CloseSocket = _Server._CatchAllModule.Accept(this); |
|
212 } else { |
|
213 this.Tag = this; |
|
214 Console.WriteLine("TCPServer: Unknown magic number: " + _MagicNumber.ToString()); |
|
215 } |
|
216 } catch (ThreadAbortException) { |
|
217 Console.WriteLine("TCPServer: Caught ThreadAbortException"); |
|
218 } catch (SocketException ex) { |
|
219 Console.WriteLine("TCPServer: Caught SocketException: " + ex.Message); |
|
220 } catch (Exception ex) { |
|
221 Console.WriteLine("TCPServer: Caught Exception: " + ex.ToString()); |
|
222 } finally { |
|
223 try { |
|
224 if (CloseSocket) base.Close(); |
|
225 } catch { } |
|
226 } |
|
227 } |
|
228 } |
|
229 } |
|
230 } |