-
Notifications
You must be signed in to change notification settings - Fork 3
/
TCPClient.cs
391 lines (353 loc) · 14.4 KB
/
TCPClient.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
// Eternal Lands Bot
// Copyright (C) 2006 Artem Makhutov
// artem@makhutov.org
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
using System;
namespace TCPClient
{
/// <summary>
/// description of TCPClient.
/// </summary>
public class TCPClient
{
// OnGotDisconnected
public delegate void GotDisconnectedEventHandler(object sender, EventArgs e);
public event GotDisconnectedEventHandler GotDisconnected;
public void OnGotDisconnected(EventArgs e)
{
if (GotDisconnected != null)
GotDisconnected(this, e);
}
// OnGotConnected
public delegate void GotConnectedEventHandler(object sender, EventArgs e);
public event GotConnectedEventHandler GotConnected;
protected void OnGotConnected(EventArgs e)
{
if (GotConnected != null)
GotConnected(this, e);
}
// OnGotData
public delegate void GotDataEventHandler(object sender, GotDataEventArgs e);
public event GotDataEventHandler GotData;
public class GotDataEventArgs : EventArgs
{
public readonly byte[] DataBuffer;
public GotDataEventArgs(byte[] Buffer)
{
DataBuffer = Buffer;
}
}
private void OnGotData(GotDataEventArgs e)
{
if (GotData != null)
GotData(this, e);
}
System.Threading.Thread ReaderThread = null;
System.Net.Sockets.Socket TCPSocket = null;
public TCPClient()
{
}
public bool Connect(string Hostname, int Port)
{
return this.Connect(System.Net.Dns.GetHostEntry(Hostname).AddressList[0], Port);
}
public bool Connect(System.Net.IPAddress IPAddress, int Port)
{
if (this.TCPSocket != null)
{
if (this.TCPSocket.Connected == true)
{
Console.WriteLine("socket already connected, returning true....");
return true;
}
}
try
{
//new code
if (this.ReaderThread != null)
{
if (this.ReaderThread.IsAlive)
{
Console.WriteLine("Thread still alive.... Trying to kill it...");
this.ReaderThread.Abort();
this.ReaderThread.Join();
}
else
{
this.ReaderThread = null;
}
return false;
}
else
{
Console.WriteLine("Connecting to game and starting a new reader....");
this.TCPSocket = null;
this.TCPSocket = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
this.TCPSocket.ReceiveTimeout = 1000;
this.TCPSocket.SendTimeout = 1000;
this.TCPSocket.Connect(new System.Net.IPEndPoint(IPAddress, Port));
// Start the thread to pool received data
this.ReaderThread = new System.Threading.Thread(this.ReaderThreadLoop);
this.ReaderThread.Start();
this.OnGotConnected(new System.EventArgs());
return true;
}
}
catch (Exception ex)
{
System.Console.WriteLine(ex.TargetSite.Name);
System.Console.WriteLine(ex.StackTrace);
System.Console.WriteLine(ex.Source);
System.Console.WriteLine(ex.Message);
return false;
}
}
public void Disconnect()
{
//if (this.ReaderThread != null)
//{
// this.ReaderThread.Abort();
// this.ReaderThread.Join();
// this.ReaderThread = null;
//}
if (this.TCPSocket != null)
{
if (this.TCPSocket.Connected == true)
{
this.TCPSocket.Shutdown(System.Net.Sockets.SocketShutdown.Both);
}
this.TCPSocket.Close();
this.TCPSocket = null;
}
this.OnGotDisconnected(new System.EventArgs());
if (this.ReaderThread != null)
{
if (this.ReaderThread.IsAlive)
{
//this.ReaderThread.Abort();
this.ReaderThread.Join();
}
this.ReaderThread = null;
}
}
public int Send(byte[] Buffer)
{
if (this.TCPSocket == null)
{
this.Disconnect();
return 0;
}
if (this.TCPSocket.Connected == false)
{
this.Disconnect();
return 0;
}
int DataSent = 0;
int TotalDataSent = 0;
int DataLeft = Buffer.Length;
System.Threading.Thread.Sleep(20);
// Loop while we have send all data
while (DataLeft > 0 && !(this.TCPSocket == null || this.TCPSocket.Connected == false))
{
if (this.TCPSocket == null)
{
this.Disconnect();
return TotalDataSent;
}
if (this.TCPSocket.Connected == false)
{
this.Disconnect();
return TotalDataSent;
}
System.Threading.Thread.Sleep(5);
DataSent = this.TCPSocket.Send(Buffer, TotalDataSent, DataLeft, System.Net.Sockets.SocketFlags.None);
TotalDataSent += DataSent;
DataLeft -= DataSent;
}
return TotalDataSent;
}
private void ReaderThreadLoop()
{
ReaderThread.Name = "TCPClient.ReaderThreadLoop";
try
{
while (this.TCPSocket != null)
{
if (!this.TCPSocket.Connected)
{
this.TCPSocket = null;
break;
}
int RxLength = 0;
int PacketLength = 0;
int BytesAvailable = 0;
byte[] DataBufferA = new byte[3];
byte[] DataBufferB = new byte[0];
// We need the first 3 bytes to get the packet length
while (RxLength < 3)
{
// Do a loop until we receive the first 3 bytes of data
// reader loop here here
while (this.TCPSocket != null)
{
if (!this.TCPSocket.Connected)
{
this.TCPSocket = null;
break;
}
BytesAvailable = this.TCPSocket.Available;
//right here here
if (BytesAvailable < 3)
{
System.Threading.Thread.Sleep(40);//was 20ms, increased to see if it will fix an infrequent error
}
else
{
break;
}
}
if (this.TCPSocket == null)
{
break;
}
else if (!this.TCPSocket.Connected)
{
this.TCPSocket = null;
break;
}
RxLength += this.TCPSocket.Receive(DataBufferA, 3, System.Net.Sockets.SocketFlags.None);
}
if (this.TCPSocket == null)
{
break;
}
else if (!this.TCPSocket.Connected)
{
this.TCPSocket = null;
break;
}
// Get the packet length
PacketLength = System.BitConverter.ToUInt16(DataBufferA, 1) + 2;
// Check if we need to fetch more data to complete the packet
if (PacketLength > 3)
{
DataBufferB = new byte[PacketLength - 3];
while (RxLength < PacketLength)
{
if (this.TCPSocket == null)
{
break;
}
else if (!this.TCPSocket.Connected)
{
this.TCPSocket = null;
break;
}
// Do a loop until we receive enough data to fill the packet
BytesAvailable = this.TCPSocket.Available;
while (BytesAvailable < PacketLength - 3)
{
System.Threading.Thread.Sleep(40);
if (this.TCPSocket == null)
{
break;
}
else if (!this.TCPSocket.Connected)
{
this.TCPSocket = null;
break;
}
BytesAvailable = this.TCPSocket.Available;
}
// RxLength += this.TCPSocket.Receive(DataBufferB,PacketLength-3,System.Net.Sockets.SocketFlags.None);
//Check if we can get all data with one go, if not get only a part of it
if ((RxLength + BytesAvailable) < PacketLength)
{
if (this.TCPSocket == null)
{
break;
}
else if (!this.TCPSocket.Connected)
{
this.TCPSocket = null;
break;
}
RxLength += this.TCPSocket.Receive(DataBufferB, RxLength - 3, BytesAvailable, System.Net.Sockets.SocketFlags.None);
}
else
{
if (this.TCPSocket == null)
{
break;
}
else if (!this.TCPSocket.Connected)
{
this.TCPSocket = null;
break;
}
RxLength += this.TCPSocket.Receive(DataBufferB, RxLength - 3, PacketLength - RxLength, System.Net.Sockets.SocketFlags.None);
//RxLength += this.TCPSocket.Receive(DataBufferB, RxLength - 3, BytesAvailable, System.Net.Sockets.SocketFlags.None);
}
}
}
if (this.TCPSocket == null)
{
break;
}
else if (!this.TCPSocket.Connected)
{
break;
}
// Put the two byte arrays together
System.IO.MemoryStream OutputBuffer = new System.IO.MemoryStream();
OutputBuffer.Write(DataBufferA, 0, DataBufferA.Length);
OutputBuffer.Write(DataBufferB, 0, DataBufferB.Length);
this.OnGotData(new GotDataEventArgs(OutputBuffer.ToArray()));
}
Console.WriteLine("Allowing thread to die on its own, like it should!!!!");
this.ReaderThread = null;
//this.Disconnect();
}
catch (System.Threading.ThreadAbortException ex)
{
Console.WriteLine("Getting to thread abort");
System.Console.WriteLine(ex.TargetSite.Name);
System.Console.WriteLine(ex.StackTrace);
System.Console.WriteLine(ex.Source);
System.Console.WriteLine(ex.Message);
this.ReaderThread = null;
}
catch (System.Net.Sockets.SocketException ex)
{
System.Console.WriteLine(ex.TargetSite.Name);
System.Console.WriteLine(ex.StackTrace);
System.Console.WriteLine(ex.Source);
System.Console.WriteLine(ex.Message);
this.ReaderThread = null;
this.Disconnect();
}
catch (Exception ex)
{
System.Console.WriteLine(ex.TargetSite.Name);
System.Console.WriteLine(ex.StackTrace);
System.Console.WriteLine(ex.Source);
System.Console.WriteLine(ex.Message);
this.ReaderThread = null;
//this.Disconnect();
}
}
}
}