forked from kazenif/ImacocoNow_Taxi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PolicyFileServer.cs
339 lines (290 loc) · 7.83 KB
/
PolicyFileServer.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace PCGPS
{
/// <summary>
/// crossdomain.xml 形式の、アクセス許可情報を取得するためのサーバ
/// デフォルトポート : 843
/// </summary>
class PolicyFileServer
{
/// <summary>
/// 待ち受けポート
/// </summary>
//const int Port = 843;
const int Port = 3210;
/// <summary>
/// ポリシーファイル
/// </summary>
string Policy = "";
/// <summary>
/// TCP 待ち受けオブジェクト
/// </summary>
TcpListener tcpListener;
/// <summary>
/// 接続してきたクライアントの情報
/// </summary>
List<ClientContext> clients;
/// <summary>
/// 待ち受けスレッド
/// ブロッキングモードで動作するので、
/// 別スレッドで起動
/// </summary>
Thread listenerThread;
/// <summary>
/// サーバ停止フラグ
/// </summary>
bool StopFlag;
/// <summary>
/// メッセージ通知用デリゲート
/// </summary>
TcpServerEvent tcpServerEvent;
// --------------------------------------------------------------------
/// <summary>
/// コンストラクタ
/// </summary>
public PolicyFileServer()
{
clients = new List<ClientContext>();
}
/// <summary>
/// サーバのメッセージを伝えるイベントを呼び出す
/// </summary>
/// <param name="message"></param>
private void invokeServerEvent(string message)
{
if (tcpServerEvent != null)
tcpServerEvent(message);
}
/// <summary>
/// ポリシーファイルの内容を取得する
/// </summary>
private void GetPolicyFile()
{
string path = "crossdomain.xml";
if (!File.Exists(path))
{
Policy = "";
return;
}
Policy = File.ReadAllText(path);
}
/// <summary>
/// クライアントを切断する
/// </summary>
/// <param name="context"></param>
private void CloseClient(ClientContext context)
{
context.tcpClient.Close();
clients.Remove(context);
invokeServerEvent("クライアントの接続を閉じました。");
invokeServerEvent("現在の接続数: " + clients.Count);
}
/// <summary>
/// サーバ開始
/// </summary>
public void Start()
{
if (listenerThread != null)
return;
StopFlag = false;
// ポリシーファイルの内容を取得する
GetPolicyFile();
if (Policy == "")
{
invokeServerEvent("crossdomain.xml ファイルの読み取りに失敗しました。" +
"Exe と同じディレクトリに、 crossdomain.xml ファイルを" +
"配置してください。");
}
// サーバ待ち受けスレッドを開始する
listenerThread = new Thread(new ThreadStart(Listen));
listenerThread.Start();
invokeServerEvent("PolicyFileServer : サーバが開始されました。");
}
/// <summary>
/// TCP サーバのメッセージを受け取るイベントハンドラの登録
/// </summary>
/// <param name="method"></param>
public void RegisterTcpServerEvent(TcpServerEvent method)
{
tcpServerEvent = method;
}
/// <summary>
/// サーバ停止
/// </summary>
public void Stop()
{
StopFlag = true;
// クラアントとの接続を全て閉じる
foreach (ClientContext context in clients)
{
context.tcpClient.Close();
context.StopFlag = true;
context.thread.Join();
}
// 待ち受けを停止する
if (tcpListener != null)
tcpListener.Stop();
// 待ち受けスレッドを停止する
if (listenerThread != null)
{
listenerThread.Join();
listenerThread = null;
}
invokeServerEvent("PolicyFileServer : サーバが停止されました。");
}
/// <summary>
/// クライアントに文字列のメッセージを送信する
/// </summary>
/// <param name="stream"></param>
/// <param name="message"></param>
private void SendMessage(ClientContext context, string message)
{
Stream stream = context.tcpClient.GetStream();
BinaryWriter writer = new BinaryWriter(stream);
byte[] byteArray = Encoding.UTF8.GetBytes(message);
try
{
writer.Write(byteArray);
writer.Flush();
}
catch (IOException e)
{
invokeServerEvent("SendMessage() で例外が発生しました。クライアントを切断します。");
invokeServerEvent(e.ToString());
CloseClient(context);
}
}
/// <summary>
/// サーバ待ち受けループ
/// </summary>
private void Listen()
{
try
{
tcpListener = new TcpListener(IPAddress.Any, Port);
tcpListener.Start();
invokeServerEvent("接続を待っています。");
while (!StopFlag)
{
if (tcpListener.Pending())
{
Thread thread = new Thread(new ParameterizedThreadStart(ClientLoop));
TcpClient client = tcpListener.AcceptTcpClient();
ClientContext context = new ClientContext(client, thread);
clients.Add(context);
thread.Start(context);
if (client.Client.Connected == true)
invokeServerEvent("クライアントが接続しました。 : " + client.Client.RemoteEndPoint.ToString());
invokeServerEvent("現在の接続数: " + clients.Count);
continue;
}
Thread.Sleep(10);
}
}
catch (SocketException e)
{
invokeServerEvent("エラー (SocketException) : " + e.ToString());
}
finally
{
tcpListener.Stop();
}
}
/// <summary>
/// クライアントからのデータを待つループ
/// </summary>
/// <param name="param"></param>
private void ClientLoop(Object param)
{
ClientContext context = (ClientContext)param;
TcpClient client = context.tcpClient;
//Byte[] bytes = new Byte[256];
NetworkStream stream = client.GetStream();
while (!context.StopFlag)
{
// 接続チェック
if (!client.Client.Connected)
{
CloseClient(context);
break;
}
if (stream.DataAvailable)
{
ReadStream(context, stream);
}
// 10 ミリ秒待つ
Thread.Sleep(10);
}
client.Close();
}
/// <summary>
/// データ受信
/// </summary>
/// <param name="context"></param>
/// <param name="stream"></param>
private void ReadStream(ClientContext context, NetworkStream stream)
{
BinaryReader reader = new BinaryReader(stream);
// 受信タイムアウト時間の設定
//stream.ReadTimeout = 1000;
// ブロッキングモードでデータを受信
while (stream.DataAvailable)
{
List<byte> byteList = new List<byte>();
// 1 文字ずつ、 \0 が出現するまで読み取る
// 例外が出たときも読み取り終了
while (true)
{
byte buffer;
try
{
buffer = (byte)stream.ReadByte();
}
catch (IOException)
{
break;
}
if (buffer == 0)
break;
byteList.Add(buffer);
}
// 読み取ったデータをストリング型に変換
string message = Encoding.UTF8.GetString(byteList.ToArray());
byteList.Clear();
try
{
// 受信したデータが、<policy-file-request/> だった時
if (message == "<policy-file-request/>")
{
invokeServerEvent("PolicyFileServer : ポリシーファイルを送信します。");
SendMessage(context, Policy);
CloseClient(context);
break;
}
}
catch (EndOfStreamException)
{
invokeServerEvent("PolicyFileServer : ReadStream() で例外が発生しました。クライアントを切断します。");
context.StopFlag = true;
CloseClient(context);
break;
}
catch (IOException e)
{
// タイムアウト
invokeServerEvent("PolicyFileServer : ReadStream() で例外が発生しました。クライアントを切断します。");
invokeServerEvent(e.ToString());
context.StopFlag = true;
CloseClient(context);
break;
}
}
}
}
}