forked from nefarius/sm-ext-socket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extension.cpp
464 lines (378 loc) · 15.5 KB
/
Extension.cpp
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#include "Extension.h"
#include <string>
#include <boost/asio.hpp>
#include "CallbackHandler.h"
#include "Callback.h"
#include "Socket.h"
using namespace boost::asio::ip;
Extension extension;
SMEXT_LINK(&extension);
void GameFrame(bool simulating) {
callbackHandler.ExecuteQueuedCallbacks();
}
extern const sp_nativeinfo_t smsock_natives[];
bool Extension::SDK_OnLoad(char *error, size_t err_max, bool late) {
smutils->AddGameFrameHook(&GameFrame);
sharesys->AddNatives(myself, smsock_natives);
socketHandleType = handlesys->CreateType("Socket", this, 0, NULL, NULL, myself->GetIdentity(), NULL);
//if (_debug) smutils->LogError(myself, "[Debug] Extension loaded");
socketHandler.StartProcessing();
return true;
}
void Extension::SDK_OnUnload() {
smutils->RemoveGameFrameHook(&GameFrame);
handlesys->RemoveType(socketHandleType, NULL);
socketHandler.Shutdown();
}
void Extension::OnHandleDestroy(HandleType_t type, void *object) {
if (type == socketHandleType && object != NULL) {
socketHandler.DestroySocket((SocketWrapper*) object);
}
}
SocketWrapper* Extension::GetSocketWrapperByHandle(Handle_t handle) {
HandleSecurity sec;
sec.pOwner = NULL;
sec.pIdentity = myself->GetIdentity();
SocketWrapper* sw;
if (handlesys->ReadHandle(handle, socketHandleType, &sec, (void**)&sw) != HandleError_None) return NULL;
return sw;
}
// native bool:SocketIsConnected(Handle:socket);
cell_t SocketIsConnected(IPluginContext *pContext, const cell_t *params) {
SocketWrapper* sw = extension.GetSocketWrapperByHandle(static_cast<Handle_t>(params[1]));
if (sw == NULL) return pContext->ThrowNativeError("Invalid handle: %i", params[1]);
switch (sw->socketType) {
case SM_SocketType_Tcp:
return ((Socket<tcp>*) sw->socket)->IsOpen();
case SM_SocketType_Udp:
return ((Socket<udp>*) sw->socket)->IsOpen();
default:
return false;
}
}
// native Handle:SocketCreate(SocketType:protocol=SOCKET_TCP, SocketErrorCB:efunc);
cell_t SocketCreate(IPluginContext *pContext, const cell_t *params) {
if (params[1] != SM_SocketType_Tcp && params[1] != SM_SocketType_Udp) return pContext->ThrowNativeError("Invalid protocol specified");
if (!pContext->GetFunctionById(params[2])) return pContext->ThrowNativeError("Invalid error callback specified");
cell_t handle = -1;
switch (params[1]) {
case SM_SocketType_Tcp: {
Socket<tcp>* socket = socketHandler.CreateSocket<tcp>(SM_SocketType_Tcp);
SocketWrapper* sw = socketHandler.GetSocketWrapper(socket);
handle = handlesys->CreateHandle(extension.socketHandleType, sw, pContext->GetIdentity(), myself->GetIdentity(), NULL);
socket->smHandle = handle;
socket->errorCallback = pContext->GetFunctionById(params[2]);
break;
}
case SM_SocketType_Udp: {
Socket<udp>* socket = socketHandler.CreateSocket<udp>(SM_SocketType_Udp);
SocketWrapper* sw = socketHandler.GetSocketWrapper(socket);
handle = handlesys->CreateHandle(extension.socketHandleType, sw, pContext->GetIdentity(), myself->GetIdentity(), NULL);
socket->smHandle = handle;
socket->errorCallback = pContext->GetFunctionById(params[2]);
break;
}
}
return handle;
}
// native SocketBind(Handle:socket, String:hostname[], port);
cell_t SocketBind(IPluginContext *pContext, const cell_t *params) {
SocketWrapper* sw = extension.GetSocketWrapperByHandle(static_cast<Handle_t>(params[1]));
if (sw == NULL) return pContext->ThrowNativeError("Invalid handle: %i", params[1]);
if (params[3] < 0 || params[3] > 65535) return pContext->ThrowNativeError("Invalid port specified");
char *hostname = NULL;
pContext->LocalToString(params[2], &hostname);
switch (sw->socketType) {
case SM_SocketType_Tcp:
return ((Socket<tcp>*) sw->socket)->Bind(hostname, params[3], false);
case SM_SocketType_Udp:
return ((Socket<udp>*) sw->socket)->Bind(hostname, params[3], false);
default:
return false;
}
}
// native SocketConnect(Handle:socket, SocketConnectCB:cfunc, SocketReceiveCB:rfunc, SocketDisconnectCB:dfunc, String:hostname[], port);
cell_t SocketConnect(IPluginContext *pContext, const cell_t *params) {
SocketWrapper* sw = extension.GetSocketWrapperByHandle(static_cast<Handle_t>(params[1]));
if (sw == NULL) return pContext->ThrowNativeError("Invalid handle: %i", params[1]);
//if (socket->shouldListen()) return pContext->ThrowNativeError("You can't connect a listening socket");
if (!pContext->GetFunctionById(params[2])) return pContext->ThrowNativeError("Invalid connect callback specified");
if (!pContext->GetFunctionById(params[3])) return pContext->ThrowNativeError("Invalid receive callback specified");
if (!pContext->GetFunctionById(params[4])) return pContext->ThrowNativeError("Invalid disconnect callback specified");
if (params[6] < 0 || params[6] > 65535) return pContext->ThrowNativeError("Invalid port specified");
char *hostname = NULL;
pContext->LocalToString(params[5], &hostname);
switch (sw->socketType) {
case SM_SocketType_Tcp: {
Socket<tcp>* socket = (Socket<tcp>*) sw->socket;
if (socket->IsOpen()) return pContext->ThrowNativeError("Socket is already connected");
socket->connectCallback = pContext->GetFunctionById(params[2]);
socket->receiveCallback = pContext->GetFunctionById(params[3]);
socket->disconnectCallback = pContext->GetFunctionById(params[4]);
return socket->Connect(hostname, params[6]);
}
case SM_SocketType_Udp: {
Socket<udp>* socket = (Socket<udp>*) sw->socket;
if (socket->IsOpen()) return pContext->ThrowNativeError("Socket is already connected");
socket->connectCallback = pContext->GetFunctionById(params[2]);
socket->receiveCallback = pContext->GetFunctionById(params[3]);
socket->disconnectCallback = pContext->GetFunctionById(params[4]);
return socket->Connect(hostname, params[6]);
}
default:
return false;
}
}
// native SocketDisconnect(Handle:socket);
cell_t SocketDisconnect(IPluginContext *pContext, const cell_t *params) {
SocketWrapper* sw = extension.GetSocketWrapperByHandle(static_cast<Handle_t>(params[1]));
if (sw == NULL) return pContext->ThrowNativeError("Invalid handle: %i", params[1]);
switch (sw->socketType) {
case SM_SocketType_Tcp: {
Socket<tcp>* socket = (Socket<tcp>*) sw->socket;
if (!socket->IsOpen()) return pContext->ThrowNativeError("Socket is not connected/listening");
return socket->Disconnect();
}
case SM_SocketType_Udp: {
Socket<udp>* socket = (Socket<udp>*) sw->socket;
if (!socket->IsOpen()) return pContext->ThrowNativeError("Socket is not connected/listening");
return socket->Disconnect();
}
default:
return false;
}
}
// native SocketListen(Handle:socket, SocketIncomingCB:ifunc);
cell_t SocketListen(IPluginContext *pContext, const cell_t *params) {
SocketWrapper* sw = extension.GetSocketWrapperByHandle(static_cast<Handle_t>(params[1]));
if (sw == NULL) return pContext->ThrowNativeError("Invalid handle: %i", params[1]);
if (sw->socketType != SM_SocketType_Tcp) return pContext->ThrowNativeError("The socket must use the TCP/SOCK_STREAM protocol");
if (!pContext->GetFunctionById(params[2])) return pContext->ThrowNativeError("Invalid incoming callback specified");
switch (sw->socketType) {
case SM_SocketType_Tcp: {
Socket<tcp>* socket = (Socket<tcp>*) sw->socket;
if (socket->IsOpen()) return pContext->ThrowNativeError("Socket is already open");
socket->incomingCallback = pContext->GetFunctionById(params[2]);
return socket->Listen();
}
case SM_SocketType_Udp: {
Socket<udp>* socket = (Socket<udp>*) sw->socket;
if (socket->IsOpen()) return pContext->ThrowNativeError("Socket is already open");
socket->incomingCallback = pContext->GetFunctionById(params[2]);
return socket->Listen();
}
default:
return false;
}
}
// native SocketSend(Handle:socket, String:command[], size);
cell_t SocketSend(IPluginContext *pContext, const cell_t *params) {
SocketWrapper* sw = extension.GetSocketWrapperByHandle(static_cast<Handle_t>(params[1]));
if (sw == NULL) return pContext->ThrowNativeError("Invalid handle: %i", params[1]);
char* dataTmp = NULL;
pContext->LocalToString(params[2], &dataTmp);
std::string data;
if (params[3] == -1) {
data.assign(dataTmp);
} else {
data.assign(dataTmp, params[3]);
}
switch (sw->socketType) {
case SM_SocketType_Tcp: {
Socket<tcp>* socket = (Socket<tcp>*) sw->socket;
if (!socket->IsOpen()) return pContext->ThrowNativeError("Can't send, socket is not connected");
return socket->Send(data);
}
case SM_SocketType_Udp: {
Socket<udp>* socket = (Socket<udp>*) sw->socket;
if (!socket->IsOpen()) return pContext->ThrowNativeError("Can't send, socket is not connected");
socket->incomingCallback = pContext->GetFunctionById(params[2]);
return socket->Send(data);
}
default:
return false;
}
}
// native SocketSendTo(Handle:socket, const String:data[], size=-1, const String:hostname[], port);
cell_t SocketSendTo(IPluginContext *pContext, const cell_t *params) {
SocketWrapper* sw = extension.GetSocketWrapperByHandle(static_cast<Handle_t>(params[1]));
if (sw == NULL) return pContext->ThrowNativeError("Invalid handle: %i", params[1]);
if (sw->socketType == SM_SocketType_Tcp) return pContext->ThrowNativeError("This native doesn't support connection orientated protocols");
char* dataTmp = NULL;
pContext->LocalToString(params[2], &dataTmp);
std::string data;
if (params[3] == -1) {
data.assign(dataTmp);
} else {
data.assign(dataTmp, params[3]);
}
char* hostname = NULL;
pContext->LocalToString(params[4], &hostname);
switch (sw->socketType) {
case SM_SocketType_Udp: {
Socket<udp>* socket = (Socket<udp>*) sw->socket;
//if (!socket->IsOpen()) return pContext->ThrowNativeError("Can't send, socket is not connected");
socket->incomingCallback = pContext->GetFunctionById(params[2]);
return socket->SendTo(data, hostname, params[5]);
}
default:
return false;
}
}
// native SocketSetOption(Handle:socket, SocketOption:option, value)
cell_t SocketSetOption(IPluginContext *pContext, const cell_t *params) {
SocketWrapper* sw = extension.GetSocketWrapperByHandle(static_cast<Handle_t>(params[1]));
if (params[2] != SM_SO_ConcatenateCallbacks &&
params[2] != SM_SO_ForceFrameLock &&
params[2] != SM_SO_CallbacksPerFrame &&
params[2] != SM_SO_DebugMode) {
if (sw == NULL) return pContext->ThrowNativeError("Invalid handle: %i", params[1]);
switch (sw->socketType) {
case SM_SocketType_Tcp: {
return ((Socket<tcp>*) sw->socket)->SetOption((SM_SocketOption) params[2], params[3]);
}
case SM_SocketType_Udp: {
return ((Socket<udp>*) sw->socket)->SetOption((SM_SocketOption) params[2], params[3]);
}
default:
return false;
}
} else {
return false;
}
#if 0
switch (params[2]) {
case ConcatenateCallbacks:
socket->setOption(ConcatenateCallbacks, value);
return 1;
case ForceFrameLock:
callbacks->setOption(ForceFrameLock, value);
return 1;
case CallbacksPerFrame:
if (value > 0) {
callbacks->setOption(CallbacksPerFrame, value);
return 1;
} else {
return 0;
}
case DebugMode:
sockets._debug = value != 0;
return 1;
...
}
#endif
}
// native SocketSetReceiveCallback(Handle:socket, SocketReceiveCB:rfunc);
cell_t SocketSetReceiveCallback(IPluginContext *pContext, const cell_t *params) {
SocketWrapper* sw = extension.GetSocketWrapperByHandle(static_cast<Handle_t>(params[1]));
if (sw == NULL) return pContext->ThrowNativeError("Invalid handle: %i", params[1]);
switch (sw->socketType) {
case SM_SocketType_Tcp:
((Socket<tcp>*) sw->socket)->receiveCallback = pContext->GetFunctionById((params[2]));
break;
case SM_SocketType_Udp:
((Socket<udp>*) sw->socket)->receiveCallback = pContext->GetFunctionById((params[2]));
default:
return false;
}
return true;
}
// native SocketSetSendqueueEmptyCallback(Handle:socket, SocketSendqueueEmptyCB:sfunc);
cell_t SocketSetSendqueueEmptyCallback(IPluginContext *pContext, const cell_t *params) {
SocketWrapper* sw = extension.GetSocketWrapperByHandle(static_cast<Handle_t>(params[1]));
if (sw == NULL) return pContext->ThrowNativeError("Invalid handle: %i", params[1]);
bool forceSendqueueEmptyCallback = false;
switch (sw->socketType) {
case SM_SocketType_Tcp:
((Socket<tcp>*) sw->socket)->sendqueueEmptyCallback = pContext->GetFunctionById((params[2]));
if (!((Socket<tcp>*) sw->socket)->sendQueueLength) forceSendqueueEmptyCallback = true;
break;
case SM_SocketType_Udp:
((Socket<udp>*) sw->socket)->sendqueueEmptyCallback = pContext->GetFunctionById((params[2]));
if (!((Socket<tcp>*) sw->socket)->sendQueueLength) forceSendqueueEmptyCallback = true;
default:
return false;
}
if (forceSendqueueEmptyCallback) {
callbackHandler.AddCallback(new Callback(CallbackEvent_SendQueueEmpty, sw->socket));
}
return true;
}
// native SocketSetDisconnectCallback(Handle:socket, SocketDisconnectCB:dfunc);
cell_t SocketSetDisconnectCallback(IPluginContext *pContext, const cell_t *params) {
SocketWrapper* sw = extension.GetSocketWrapperByHandle(static_cast<Handle_t>(params[1]));
if (sw == NULL) return pContext->ThrowNativeError("Invalid handle: %i", params[1]);
switch (sw->socketType) {
case SM_SocketType_Tcp:
((Socket<tcp>*) sw->socket)->disconnectCallback = pContext->GetFunctionById((params[2]));
break;
case SM_SocketType_Udp:
((Socket<udp>*) sw->socket)->disconnectCallback = pContext->GetFunctionById((params[2]));
default:
return false;
}
return true;
}
// native SocketSetErrorCallback(Handle:socket, SocketErrorCB:efunc);
cell_t SocketSetErrorCallback(IPluginContext *pContext, const cell_t *params) {
SocketWrapper* sw = extension.GetSocketWrapperByHandle(static_cast<Handle_t>(params[1]));
if (sw == NULL) return pContext->ThrowNativeError("Invalid handle: %i", params[1]);
switch (sw->socketType) {
case SM_SocketType_Tcp:
((Socket<tcp>*) sw->socket)->errorCallback = pContext->GetFunctionById((params[2]));
break;
case SM_SocketType_Udp:
((Socket<udp>*) sw->socket)->errorCallback = pContext->GetFunctionById((params[2]));
default:
return false;
}
return true;
}
// native SocketSetArg(Handle:socket, any:arg);
cell_t SocketSetArg(IPluginContext *pContext, const cell_t *params) {
SocketWrapper* sw = extension.GetSocketWrapperByHandle(static_cast<Handle_t>(params[1]));
if (sw == NULL) return pContext->ThrowNativeError("Invalid handle: %i", params[1]);
switch (sw->socketType) {
case SM_SocketType_Tcp:
((Socket<tcp>*) sw->socket)->smCallbackArg = params[2];
break;
case SM_SocketType_Udp:
((Socket<udp>*) sw->socket)->smCallbackArg = params[2];
default:
return false;
}
return true;
}
// native SocketGetHostName(String:dest[], destLen);
cell_t SocketGetHostName(IPluginContext *pContext, const cell_t *params) {
char* dest = NULL;
pContext->LocalToString(params[1], &dest);
boost::system::error_code errorCode;
std::string hostName = host_name(errorCode);
if (!errorCode) {
size_t len = hostName.copy(dest, params[2]-1);
dest[len] = '\0';
return true;
} else {
dest[0] = '\0';
return false;
}
}
const sp_nativeinfo_t smsock_natives[] = {
{"SocketIsConnected", SocketIsConnected},
{"SocketCreate", SocketCreate},
{"SocketBind", SocketBind},
{"SocketConnect", SocketConnect},
{"SocketDisconnect", SocketDisconnect},
{"SocketListen", SocketListen},
{"SocketSend", SocketSend},
{"SocketSendTo", SocketSendTo},
{"SocketSetOption", SocketSetOption},
{"SocketSetReceiveCallback", SocketSetReceiveCallback},
{"SocketSetSendqueueEmptyCallback", SocketSetSendqueueEmptyCallback},
{"SocketSetDisconnectCallback", SocketSetDisconnectCallback},
{"SocketSetErrorCallback", SocketSetErrorCallback},
{"SocketSetArg", SocketSetArg},
{"SocketGetHostName", SocketGetHostName},
{NULL, NULL},
};