-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSourceRcon.cs
343 lines (278 loc) · 10.9 KB
/
SourceRcon.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
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Collections;
namespace SourceRcon {
/// <summary>
/// Summary description for SourceRcon.
/// </summary>
public class SourceRcon : IDisposable {
private static RConControl.Language mLangMan = RConControl.Language.Instance;
private string ConnectionWrongPw = mLangMan.GetString("Error_WrongRconPW");
private string ConnectionClosed = mLangMan.GetString("Error_ConnectionClosed");
private string ConnectionFailedString = mLangMan.GetString("Error_ConnectionFailed");
private string UnknownResponseType = mLangMan.GetString("Error_UnknownResponse");
public event StringOutput ServerOutput;
public event StringOutput Errors;
public event BoolInfo ConnectionSuccess;
private Socket S;
public SourceRcon() {
S = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
PacketCount = 0;
#if DEBUG
TempPackets = new ArrayList();
#endif
}
public void Connect(IPEndPoint Server, string password) {
IAsyncResult result = S.BeginConnect(Server, null, null);
result.AsyncWaitHandle.WaitOne(RConControl.GlobalConstants.RCON_CONNECT_TIMEOUT, true);
if (!result.IsCompleted || !S.Connected) {
S.Close();
OnError(ConnectionFailedString);
} else {
RCONPacket SA = new RCONPacket();
SA.RequestId = 1;
SA.String1 = password;
SA.ServerDataSent = RCONPacket.SERVERDATA_sent.SERVERDATA_AUTH;
SendRCONPacket(SA);
// This is the first time we've sent, so we can start listening now!
StartGetNewPacket();
}
}
public void ServerCommand(string command) {
if (connected) {
RCONPacket PacketToSend = new RCONPacket();
PacketToSend.RequestId = 2;
PacketToSend.ServerDataSent = RCONPacket.SERVERDATA_sent.SERVERDATA_EXECCOMMAND;
PacketToSend.String1 = command;
SendRCONPacket(PacketToSend);
}
}
void SendRCONPacket(RCONPacket p) {
byte[] Packet = p.OutputAsBytes();
S.BeginSend(Packet, 0, Packet.Length, SocketFlags.None, new AsyncCallback(SendCallback), this);
}
bool connected;
public bool Connected {
get { return connected; }
}
void SendCallback(IAsyncResult ar) {
S.EndSend(ar);
}
int PacketCount;
void StartGetNewPacket() {
RecState state = new RecState();
state.IsPacketLength = true;
state.Data = new byte[4];
state.PacketCount = PacketCount;
PacketCount++;
#if DEBUG
TempPackets.Add(state);
#endif
S.BeginReceive(state.Data, 0, 4, SocketFlags.None, new AsyncCallback(ReceiveCallback), state);
}
#if DEBUG
public ArrayList TempPackets;
#endif
void ReceiveCallback(IAsyncResult ar) {
bool recsuccess = false;
RecState state = null;
try {
int bytesgotten = S.EndReceive(ar);
state = (RecState)ar.AsyncState;
state.BytesSoFar += bytesgotten;
recsuccess = true;
#if DEBUG
Console.WriteLine("Receive Callback. Packet: {0} First packet: {1}, Bytes so far: {2}", state.PacketCount, state.IsPacketLength, state.BytesSoFar);
#endif
} catch (SocketException) {
//OnConnectionSuccess(false);
OnError(ConnectionClosed);
}
if (recsuccess)
ProcessIncomingData(state);
}
void ProcessIncomingData(RecState state) {
if (state.IsPacketLength) {
// First 4 bytes of a new packet.
state.PacketLength = BitConverter.ToInt32(state.Data, 0);
state.IsPacketLength = false;
state.BytesSoFar = 0;
state.Data = new byte[state.PacketLength];
S.BeginReceive(state.Data, 0, state.PacketLength, SocketFlags.None, new AsyncCallback(ReceiveCallback), state);
} else {
// Do something with data...
if (state.BytesSoFar < state.PacketLength) {
// Missing data.
S.BeginReceive(state.Data, state.BytesSoFar, state.PacketLength - state.BytesSoFar, SocketFlags.None, new AsyncCallback(ReceiveCallback), state);
} else if (state.BytesSoFar == 0 && state.PacketLength == 0) {
// Timeout
OnError(ConnectionClosed);
//OnConnectionSuccess(false);
} else {
// Process data.
#if DEBUG
Console.WriteLine("Complete packet.");
#endif
RCONPacket RetPack = new RCONPacket();
RetPack.ParseFromBytes(state.Data, this);
ProcessResponse(RetPack);
// Wait for new packet.
StartGetNewPacket();
}
}
}
void ProcessResponse(RCONPacket P) {
switch (P.ServerDataReceived) {
case RCONPacket.SERVERDATA_rec.SERVERDATA_AUTH_RESPONSE:
if (P.RequestId != -1) {
// Connected.
connected = true;
OnConnectionSuccess(true);
} else {
// Failed!
//OnConnectionSuccess(false);
OnError(ConnectionWrongPw);
}
break;
case RCONPacket.SERVERDATA_rec.SERVERDATA_RESPONSE_VALUE:
if (hadjunkpacket) {
// Real packet!
OnServerOutput(P.String1);
} else {
hadjunkpacket = true;
}
break;
default:
OnError(UnknownResponseType);
break;
}
}
bool hadjunkpacket;
internal void OnServerOutput(string output) {
if (ServerOutput != null) {
ServerOutput(output);
}
}
internal void OnError(string error) {
if (Errors != null) {
Errors(error);
}
}
internal void OnConnectionSuccess(bool info) {
if (ConnectionSuccess != null) {
ConnectionSuccess(info);
}
}
public void Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing) {
if (disposing) {
if (S != null) S.Dispose();
}
}
}
public delegate void StringOutput(string output);
public delegate void BoolInfo(bool info);
internal class RecState {
internal RecState() {
PacketLength = -1;
BytesSoFar = 0;
IsPacketLength = false;
}
public int PacketCount;
public int PacketLength;
public int BytesSoFar;
public bool IsPacketLength;
public byte[] Data;
}
internal class RCONPacket {
internal RCONPacket() {
RequestId = 0;
String1 = "blah";
String2 = String.Empty;
ServerDataSent = SERVERDATA_sent.None;
ServerDataReceived = SERVERDATA_rec.None;
}
internal byte[] OutputAsBytes() {
byte[] packetsize;
byte[] reqid;
byte[] serverdata;
byte[] bstring1;
byte[] bstring2;
UTF8Encoding utf = new UTF8Encoding();
bstring1 = utf.GetBytes(String1);
bstring2 = utf.GetBytes(String2);
serverdata = BitConverter.GetBytes((int)ServerDataSent);
reqid = BitConverter.GetBytes(RequestId);
// Compose into one packet.
byte[] FinalPacket = new byte[4 + 4 + 4 + bstring1.Length + 1 + bstring2.Length + 1];
packetsize = BitConverter.GetBytes(FinalPacket.Length - 4);
int BPtr = 0;
packetsize.CopyTo(FinalPacket, BPtr);
BPtr += 4;
reqid.CopyTo(FinalPacket, BPtr);
BPtr += 4;
serverdata.CopyTo(FinalPacket, BPtr);
BPtr += 4;
bstring1.CopyTo(FinalPacket, BPtr);
BPtr += bstring1.Length;
FinalPacket[BPtr] = (byte)0;
BPtr++;
bstring2.CopyTo(FinalPacket, BPtr);
BPtr += bstring2.Length;
FinalPacket[BPtr] = (byte)0;
BPtr++;
return FinalPacket;
}
internal void ParseFromBytes(byte[] bytes, SourceRcon parent) {
int BPtr = 0;
ArrayList stringcache;
UTF8Encoding utf = new UTF8Encoding();
// First 4 bytes are ReqId.
RequestId = BitConverter.ToInt32(bytes, BPtr);
BPtr += 4;
// Next 4 are server data.
ServerDataReceived = (SERVERDATA_rec)BitConverter.ToInt32(bytes, BPtr);
BPtr += 4;
// string1 till /0
stringcache = new ArrayList();
while (bytes[BPtr] != 0) {
stringcache.Add(bytes[BPtr]);
BPtr++;
}
String1 = utf.GetString((byte[])stringcache.ToArray(typeof(byte)));
BPtr++;
// string2 till /0
stringcache = new ArrayList();
while (bytes[BPtr] != 0) {
stringcache.Add(bytes[BPtr]);
BPtr++;
}
String2 = utf.GetString((byte[])stringcache.ToArray(typeof(byte)));
BPtr++;
// Repeat if there's more data?
if (BPtr != bytes.Length) {
parent.OnError("Urk, extra data!");
}
}
public enum SERVERDATA_sent : int {
SERVERDATA_AUTH = 3,
SERVERDATA_EXECCOMMAND = 2,
None = 255
}
public enum SERVERDATA_rec : int {
SERVERDATA_RESPONSE_VALUE = 0,
SERVERDATA_AUTH_RESPONSE = 2,
None = 255
}
internal int RequestId;
internal string String1;
internal string String2;
internal RCONPacket.SERVERDATA_sent ServerDataSent;
internal RCONPacket.SERVERDATA_rec ServerDataReceived;
}
}