-
Notifications
You must be signed in to change notification settings - Fork 2
/
NetworkState.cs
416 lines (346 loc) · 8.95 KB
/
NetworkState.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
using System;
using System.Net;
using System.Collections.Specialized;
using System.Text;
using System.Collections;
namespace Rails
{
public enum NetworkMode
{
Offline, Hosting, Joined,
}
public class HostChannel : Network.Channel
{
object sync = new object();
public HostChannel(object state)
{
}
public override void Disconnected()
{
NetworkState.LostClient(this);
}
[Network.ProxyMethod]
public void Hello(StringCollection users)
{
lock(sync)
NetworkState.RegisterUsers(users, this);
}
[Network.ProxyMethod]
public void Bye()
{
this.Stop();
}
[Network.ProxyMethod]
public void Say(string user, string message)
{
lock(sync)
Chat.Current.Heard(user, message);
}
[Network.ProxyMethod]
public void SyncGameDataFull(byte[] data)
{
NetworkState.TransmitExcept(this, "SyncGameDataFull", data);
Game.SyncGameDataFull(data);
}
[Network.ProxyMethod]
public void SyncGameData(ArrayList delta)
{
NetworkState.TransmitExcept(this, "SyncGameData", delta);
Game.SyncGameData(delta);
}
[Network.ProxyMethod]
public void NeedFullSync()
{
NetworkState.TransmitExcept(this, "NeedFullSync");
Game.NeedFullSync();
}
[Network.ProxyMethod]
public void PhantomMouse(int x, int y)
{
NetworkState.TransmitExcept(this, "PhantomMouse", x, y);
GameForm.UpdatePhantomMouse(x, y);
}
[Network.ProxyMethod]
public void Newspaper(string headline, string subhead)
{
NetworkState.TransmitExcept(this, "Newspaper", headline, subhead);
Game.ShowNewspaper(headline, subhead);
}
[Network.ProxyMethod]
public void Winner(string message)
{
NetworkState.TransmitExcept(this, "Winner", message);
System.Windows.Forms.MessageBox.Show(message, Resource.GetString("Rails"));
}
}
public delegate void UserAcceptanceEventHandler(bool accepted, StringCollection users);
public delegate void GainedClientEventHandler(HostChannel channel);
public delegate void UsersUpdatedDelegate(StringCollection users);
public class ClientChannel : Network.Channel
{
public event UserAcceptanceEventHandler OnUserAcceptance;
[Network.ProxyMethod]
public void UsersAccepted(StringCollection users)
{
if (OnUserAcceptance != null)
OnUserAcceptance(true, users);
}
[Network.ProxyMethod]
public void UsersRejected(StringCollection rejects)
{
if (OnUserAcceptance != null)
OnUserAcceptance(false, rejects);
}
[Network.ProxyMethod]
public void Say(string user, string message)
{
Chat.Current.Heard(user, message);
}
[Network.ProxyMethod]
public void UsersUpdated(StringCollection users)
{
NetworkState.InvokeUsersUpdated(users);
}
[Network.ProxyMethod]
public void OpenNewGameDialog(NewGameInfo info)
{
NewGame.Start(info);
}
[Network.ProxyMethod]
public void SetNewGameInfo(NewGameInfo info)
{
NewGame.SetInfo(info);
}
[Network.ProxyMethod]
public void CancelNewGame()
{
NewGame.CancelNewGame();
}
[Network.ProxyMethod]
public void StartGame(byte[] data)
{
NewGame.StartGame(data);
}
[Network.ProxyMethod]
public void SyncGameDataFull(byte[] data)
{
Game.SyncGameDataFull(data);
}
[Network.ProxyMethod]
public void SyncGameData(ArrayList delta)
{
Game.SyncGameData(delta);
}
[Network.ProxyMethod]
public void NeedFullSync()
{
Game.NeedFullSync();
}
[Network.ProxyMethod]
public void PhantomMouse(int x, int y)
{
GameForm.UpdatePhantomMouse(x, y);
}
[Network.ProxyMethod]
public void Newspaper(string headline, string subhead)
{
Game.ShowNewspaper(headline, subhead);
}
[Network.ProxyMethod]
public void Winner(string message)
{
System.Windows.Forms.MessageBox.Show(message, Resource.GetString("Rails"));
}
[Network.ProxyMethod]
public void ShowComputerIntent(string s)
{
Game.Current.ShowComputerIntent(s);
}
[Network.ProxyMethod]
public void HideComputerIntent(bool b)
{
Game.Current.HideComputerIntent(b);
}
[Network.ProxyMethod]
public void NeedSync()
{
Game.Current.Sync();
}
}
public delegate void UsersUpdatedEventHandler(StringCollection users);
public class NetworkState
{
const int BufferSize = 20000;
public static NetworkMode Mode = NetworkMode.Offline;
public static Network.Host Host;
public static ClientChannel Client;
public static bool IsMaster
{
get { return Mode != NetworkMode.Joined; }
}
public static bool IsHost
{
get { return Mode == NetworkMode.Hosting; }
}
public static bool IsOffline
{
get { return Mode == NetworkMode.Offline; }
}
public static bool StartHosting(IPAddress address, int port)
{
if (Mode != NetworkMode.Offline)
throw new InvalidOperationException();
try
{
Host = new Network.Host(address, port, typeof(HostChannel));
Host.BufferSize = NetworkState.BufferSize;
}
catch
{
Host = null;
return false;
}
Host.Start();
Mode = NetworkMode.Hosting;
return true;
}
public static void StopHosting()
{
if (Mode != NetworkMode.Hosting)
throw new InvalidOperationException();
Host.Stop();
Host = null;
Mode = NetworkMode.Offline;
userList.Clear();
UserChannels.Clear();
channelUsers.Clear();
}
public static bool JoinHost(IPAddress address, int port)
{
if (Mode != NetworkMode.Offline)
throw new InvalidOperationException();
Client = new ClientChannel();
Client.BufferSize = NetworkState.BufferSize;
if (!Client.ConnectTo(address, port))
return false;
Client.OnDisconnected += new EventHandler(ClientDisconnected);
Client.Start();
Mode = NetworkMode.Joined;
return true;
}
public static void LeaveHost()
{
if (Mode != NetworkMode.Joined)
throw new InvalidOperationException();
Client.Stop();
Mode = NetworkMode.Offline;
}
public static event EventHandler LostHost;
private static void ClientDisconnected(object sender, EventArgs e)
{
Mode = NetworkMode.Offline;
if (LostHost != null)
LostHost(sender, e);
}
public static void Stop()
{
if (Mode == NetworkMode.Hosting)
StopHosting();
if (Mode == NetworkMode.Joined)
LeaveHost();
}
public static void Transmit(string method, params object[] args)
{
if (IsHost)
{
foreach (HostChannel channel in Host.Clients)
channel.Invoke(method, args);
}
else if (!IsMaster)
{
Client.Invoke(method, args);
}
}
public static void TransmitExcept(HostChannel except, string method, params object[] args)
{
if (IsHost)
{
foreach (HostChannel channel in Host.Clients)
if (channel != except)
channel.Invoke(method, args);
}
}
static StringCollection userList = new StringCollection();
public static Hashtable UserChannels = new Hashtable();
static Hashtable channelUsers = new Hashtable();
public static event GainedClientEventHandler GainedClient;
public static void RegisterUsers(StringCollection users, HostChannel channel)
{
StringCollection cu = null;
if (channel != null) // remote users
{
StringCollection rejects = new StringCollection();
foreach (string user in users)
if (userList.Contains(user))
rejects.Add(user);
if (rejects.Count == 0)
{
channel.Invoke("UsersAccepted", users);
if (GainedClient != null)
GainedClient(channel);
}
else
{
channel.Invoke("UsersRejected", rejects);
return;
}
cu = (StringCollection) channelUsers[channel];
if (cu == null)
channelUsers[channel] = cu = new StringCollection();
}
foreach (string user in users)
{
userList.Add(user);
UserChannels[user] = channel;
if (cu != null)
cu.Add(user);
}
InvokeUsersUpdated(userList);
if (channel != null)
Transmit("UsersUpdated", userList);
}
public static bool UserIsLocal(string user)
{
return UserChannels[user] == null;
}
public static event UsersUpdatedEventHandler UsersUpdated;
public static void LostClient(HostChannel channel)
{
if (channelUsers[channel] == null)
return;
foreach (string user in (StringCollection) channelUsers[channel])
{
userList.Remove(user);
UserChannels.Remove(user);
}
channelUsers.Remove(channel);
InvokeUsersUpdated(userList);
Transmit("UsersUpdated", userList);
}
public static void InvokeUsersUpdated(StringCollection users)
{
if (UsersUpdated != null)
UsersUpdated(users);
}
public static StringCollection CurrentUsers
{
get { return userList; }
}
public static void KickUser(string user)
{
HostChannel channel = (HostChannel) UserChannels[user];
if (channel != null)
channel.Stop();
}
}
}