forked from Sir-Odie/CS-ELBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TCPWrapper.cs
226 lines (196 loc) · 6.4 KB
/
TCPWrapper.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
// 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 cs_elbot
{
/// <summary>
/// description of TCPWrapper.
/// </summary>
public class TCPWrapper
{
private cs_elbot.Logger TheLogger;
private TCPClient.TCPClient TheTCPClient;
public System.Net.IPAddress HostIp;
public int Port;
private int PMCounter = 0;
public bool AutoReconnect = true;
//private byte [] PartitialReceivedData = null;
//private System.Collections.ArrayList PartitialReceivedData = null;
//private System.Collections.Queue TxQueue = new System.Collections.Queue();
// OnGotDisconnected
public delegate void GotDisconnectedEventHandler(object sender, EventArgs e);
public event GotDisconnectedEventHandler GotDisconnected;
protected 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);
}
}
// OnSentCommand
public delegate void SentCommandEventHandler(object sender, SentCommandEventArgs e);
public class SentCommandEventArgs : EventArgs
{
public readonly byte[] CommandBuffer;
public SentCommandEventArgs(byte[] Buffer)
{
CommandBuffer = Buffer;
}
}
public event SentCommandEventHandler SentCommand;
private void OnSentCommand(SentCommandEventArgs e)
{
if(SentCommand!=null)
SentCommand(this,e);
}
// OnGotCommand
public delegate void GotCommandEventHandler(object sender, GotCommandEventArgs e);
public class GotCommandEventArgs : EventArgs
{
public readonly byte[] CommandBuffer;
public GotCommandEventArgs(byte[] Buffer)
{
CommandBuffer = Buffer;
}
}
public event GotCommandEventHandler GotCommand;
private void OnGotCommand(GotCommandEventArgs e)
{
if(GotCommand!=null)
GotCommand(this,e);
}
public TCPWrapper(TCPClient.TCPClient TheTCPClient,Logger MyLogger)
{
TheLogger = MyLogger;
this.TheTCPClient = TheTCPClient;
this.TheTCPClient.GotData += new TCPClient.TCPClient.GotDataEventHandler(GotData);
this.TheTCPClient.GotConnected += new TCPClient.TCPClient.GotConnectedEventHandler(GotServerConnected);
this.TheTCPClient.GotDisconnected += new TCPClient.TCPClient.GotDisconnectedEventHandler(GotServerDisconnected);
}
private bool ConnectToServer()
{
return TheTCPClient.Connect(HostIp, Port);
}
private void GotServerConnected(object Sender, System.EventArgs e)
{
OnGotConnected(new System.EventArgs());
}
public void ReconnectToServer()
{
bool GotConnected = false;
while (!GotConnected && !MainClass.tryingToConnect)
{
MainClass.tryingToConnect = true;
TheLogger.Log("Trying to connect to server...");
GotConnected = ConnectToServer();
if (GotConnected == false)
{
TheLogger.Log("... failed");
MainClass.tryingToConnect = false;
System.Threading.Thread.Sleep(10000);
}
else
{
TheLogger.Log("... successful");
MainClass.tryingToConnect = false;
}
}
}
public void DisconnectFromServer()
{
TheTCPClient.Disconnect();
}
public int Send(byte[] data)
{
//need to put a check in here for pms with no username...
int result;
if (data[0]==0x02)
{
// Check if we send a PM from console
string Message = System.Text.ASCIIEncoding.ASCII.GetString(data, 3, data.Length-3);
string[] PMArray = Message.Split(' ');
if (Message.StartsWith("Console:\\>"))
{
TheLogger.Log(Message);
return data.Length;
}
else
{
this.PMCounter++;
if (this.PMCounter>5)
{
// Do a sleep after sending 7 Sends
// Do a sleep after sending 5 Sends
System.Threading.Thread.Sleep(500);
this.PMCounter = 0;
}
}
if (data.Length > 159)
{
byte[] check = new byte[159];
Array.Copy(data, check, 159);
result = TheTCPClient.Send(check);
if (result > 0)
{
OnSentCommand(new SentCommandEventArgs(data));
}
return result;
}
else
{
byte[] check = new byte[data.Length];
Array.Copy(data, check, data.Length);
result = TheTCPClient.Send(check);
if (result > 0)
{
OnSentCommand(new SentCommandEventArgs(data));
}
return result;
}
}
result = TheTCPClient.Send(data);
if (result>0)
{
OnSentCommand(new SentCommandEventArgs(data));
}
return result;
}
private void GotServerDisconnected(object Sender, EventArgs e)
{
TheLogger.Log("TCPWrapper: GotServerDisconnected");
OnGotDisconnected(new EventArgs());
if (AutoReconnect==true && !MainClass.tryingToConnect)
{
ReconnectToServer();
}
}
private void GotData(object Sender, TCPClient.TCPClient.GotDataEventArgs e)
{
byte[] b = e.DataBuffer;
OnGotCommand(new GotCommandEventArgs(e.DataBuffer));
}
}
}