-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.cs
342 lines (322 loc) · 9.36 KB
/
Client.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace Xasteroids
{
public class Client
{
public IPAddress IPAddress { get { return Dns.GetHostEntry(string.Empty).AddressList.Where(address => address.AddressFamily == AddressFamily.InterNetwork).First(); } }
public bool IsShutDown { get; private set; }
public event Action Disconnected;
public event Action<IPAddress, IConfigurable> ObjectReceived;
public Client()
{
IPEndPoint myEndPoint = new IPEndPoint(IPAddress, 8307);
_udpClient = new UdpClient(myEndPoint);
List<object> state = new List<object> { myEndPoint, _udpClient };
_udpClient.BeginReceive(OnUdpDataReceived, state);
}
private IPAddress _serverIPAddress;
public IPAddress ServerIPAddress
{
get { return _serverIPAddress; }
set
{
if (_serverIPAddress == value)
{
return;
}
if (_tcpClient != null && _tcpClient.Connected)
{
_tcpClient.GetStream().Close();
_tcpClient.Close();
}
_serverIPAddress = value;
if (_serverIPAddress != null)
{
ResetTcpClient();
}
}
}
private string _tcpDataFromServer;
public void ReceiveData(IPAddress senderIPAddress, string data)
{
if (data == null)
{
throw new ArgumentException();
}
string portionReceived = _tcpDataFromServer;
if (portionReceived == null)
{
Match objectStartMatch = ObjectStringConverter.ObjectStartRegex.Match(data);
if (!objectStartMatch.Success)
{
//our object string was malformed
return;
}
if (objectStartMatch.Index > 0)
{
string fragmentOnFront = data.Substring(0, objectStartMatch.Index);
//our object string was malformed
data = data.Substring(objectStartMatch.Index);
}
portionReceived = data;
}
else
{
portionReceived += data;
}
/* Given the asynchronous nature of our network transfers, it is possible
* that we've got multiple objects in our data string. Unlikely, but possible.
*/
string[] parts = ObjectStringConverter.ObjectEndRegex.Split(portionReceived);
if (parts.Length > 1)
{
int index = 0;
string completeString = parts[index];
ReceiveObjectString(senderIPAddress, completeString);
++index;
int indexOfLast = parts.Length - 1;
while (index < indexOfLast)
{
/* A closed object string is one ending with an object closing character.
* It might not be a valid object string, but if it were, the object
* would be correctly closed.
*/
string closedObjectString = parts[index];
Match objectStartMatch = ObjectStringConverter.ObjectStartRegex.Match(closedObjectString);
if (objectStartMatch.Success)
{
if (objectStartMatch.Index > 0)
{
string fragmentOnFront = closedObjectString.Substring(0, objectStartMatch.Index);
//our object string was malformed
closedObjectString = closedObjectString.Substring(objectStartMatch.Index);
}
ReceiveObjectString(senderIPAddress, closedObjectString);
}
else
{
//our object string was malformed
}
++index;
}
/* The last part must be either an empty string or the starting piece of
* another object.
*/
string lastPart = parts[indexOfLast];
if (lastPart.Length == 0)
{
_tcpDataFromServer = null;
return;
}
Match objStartMatch = ObjectStringConverter.ObjectStartRegex.Match(lastPart);
if (objStartMatch.Success)
{
if (objStartMatch.Index > 0)
{
string fragmentOnFront = lastPart.Substring(0, objStartMatch.Index);
//our object string was malformed
_tcpDataFromServer = lastPart.Substring(objStartMatch.Index);
}
}
else
{
//our object string was malformed
_tcpDataFromServer = null;
}
}
else
{
_tcpDataFromServer = portionReceived;
}
}
public void ReceiveObjectString(IPAddress senderIPAddress, string objectString)
{
if (_objectStringConverter.ObjectStringMalformed(objectString))
{
//our object string was malformed
}
else if (ObjectReceived != null)
{
IConfigurable theObject = _objectStringConverter.StringToObject(objectString);
ObjectReceived(senderIPAddress, theObject);
}
}
public void ResetTcpClient()
{
try
{
_tcpClient = new TcpClient(_serverIPAddress.ToString(), Host.TCP_PORT);
}
catch (SocketException e)
{
ServerIPAddress = null;
if (e.SocketErrorCode == SocketError.TimedOut)
{
MessageBox.Show("The host computer did not respond to your connection request. " +
"You might double check the IP address you entered. " +
"It is also possible that the firewall on the host computer would not let your connection request through."
);
}
else if (e.SocketErrorCode == SocketError.NetworkUnreachable)
{
MessageBox.Show("Your computer cannot reach the IP address you have specified. " +
"You might want to double check what you have entered."
);
}
else
{
MessageBox.Show("You were unable to connect to the host computer. Here is what .Net says about the error: " +
e.Message
);
}
}
if (_tcpClient == null)
{
return;
}
NetworkStream stream = _tcpClient.GetStream();
byte[] bytes = new byte[1024];
_streamAndBytesRead[0] = stream;
_streamAndBytesRead[1] = bytes;
stream.BeginRead(bytes, 0, bytes.Length, ReadCallback, _streamAndBytesRead);
SendObjectTcp(new NetworkMessage { Content = "IP Address:" + IPAddress });
}
public void SendObjectTcp(IConfigurable obj, bool tryAgainOnFailure = true)
{
string data = _objectStringConverter.ObjectToString(obj);
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(data);
NetworkStream stream = _tcpClient.GetStream();
try
{
stream.BeginWrite(bytes, 0, bytes.Length, OnTcpDataSent, _tcpClient);
}
catch (IOException exception)
{
if (tryAgainOnFailure)
{
ResetTcpClient();
SendObjectTcp(obj, false);
return;
}
SocketException innerException = exception.InnerException as SocketException;
if (innerException != null && innerException.SocketErrorCode == SocketError.ConnectionReset)
{
MessageBox.Show("Your connection with the host computer appears to have been reset. " +
"An attempt was made to fix the problem, but it failed. You are currently unable to send data to the host computer"
);
}
else
{
MessageBox.Show("You were unable to send data to the host computer. Here is what .Net says about the error: " +
exception.Message
);
}
}
}
public void ShutDown()
{
_udpClient.Close();
if (_tcpClient != null && _tcpClient.Connected)
{
_tcpClient.GetStream().Close();
_tcpClient.Close();
}
IsShutDown = true;
}
private TcpClient _tcpClient;
private UdpClient _udpClient;
private void OnTcpDataSent(IAsyncResult asyncResult)
{
TcpClient tcpClient = (TcpClient)asyncResult.AsyncState;
NetworkStream stream = tcpClient.GetStream();
stream.EndWrite(asyncResult);
}
private List<object> _streamAndBytesRead = new List<object> {
null,
null
};
// Called when we bet data over TCP connection
private void ReadCallback(IAsyncResult asyncResult)
{
NetworkStream stream = (NetworkStream)_streamAndBytesRead[0];
byte[] bytes = (byte[])_streamAndBytesRead[1];
int numberOfBytesRead = 0;
if (!stream.CanRead)
{
ServerIPAddress = null;
if (Disconnected != null)
{
Disconnected();
}
return;
}
try
{
numberOfBytesRead = stream.EndRead(asyncResult);
}
catch (IOException)
{
ServerIPAddress = null;
if (Disconnected != null)
{
Disconnected();
}
return;
}
if (numberOfBytesRead == 0)
{
ServerIPAddress = null;
if (Disconnected != null)
{
Disconnected();
}
return;
}
string data = System.Text.Encoding.ASCII.GetString(bytes, 0, numberOfBytesRead);
ReceiveData(ServerIPAddress, data);
stream.BeginRead(bytes, 0, bytes.Length, ReadCallback, _streamAndBytesRead);
}
private void OnUdpDataReceived(IAsyncResult asyncResult)
{
if (IsShutDown)
{
return;
}
List<object> container = (List<object>)asyncResult.AsyncState;
IPEndPoint myEndPoint = (IPEndPoint)container[0];
byte[] bytes = null;
try
{
bytes = _udpClient.EndReceive(asyncResult, ref myEndPoint);
}
catch (ObjectDisposedException)
{
//Another Microsoft problem I'm just going to eat
}
catch (NullReferenceException)
{
//Another Microsoft problem I'm just going to eat
}
if (bytes == null)
{
return;
}
_udpClient.BeginReceive(OnUdpDataReceived, container);
if (ObjectReceived != null)
{
string data = System.Text.Encoding.ASCII.GetString(bytes);
IConfigurable obj = _objectStringConverter.StringToObject(data);
ObjectReceived(ServerIPAddress, obj);
}
}
private ObjectStringConverter _objectStringConverter = new ObjectStringConverter();
}
}