-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenetsqrat.cpp
649 lines (603 loc) · 25.2 KB
/
enetsqrat.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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
////////////////////////////////////////////////////////////
//
// Copyright (c) 2013-2015 Brandon Haffen - bhaffen97@gmail.com
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <wizzardsrealms.hpp>
#include <enet/enetsqrat.hpp>
#include <cstdlib>
#include <iostream>
////////////////////////////////////////////////////////////
static SQInteger parse_address(HSQUIRRELVM v, const std::string& addr_str, ENetAddress& address)
{
size_t colonPos = addr_str.find(':');
if (colonPos == std::string::npos) return sq_throwerror(v, _SC("Failed to parse address (missing port in address?)"));
std::string host_str = addr_str.substr(0, colonPos);
std::string port_str = addr_str.substr(colonPos + 1);
if (host_str.empty()) return sq_throwerror(v, _SC("Failed to parse address"));
if (port_str.empty()) return sq_throwerror(v, _SC("Missing port in address"));
if (host_str == "*") {
address.host = ENET_HOST_ANY;
} else {
if (enet_address_set_host(&address, host_str.c_str()) != 0) return sq_throwerror(v, _SC("Failed to resolve host name"));
}
if (port_str == "*") {
address.port = ENET_PORT_ANY;
} else {
address.port = std::atoi(port_str.c_str());
}
return 0;
}
////////////////////////////////////////////////////////////
static void push_event(HSQUIRRELVM v, const ENetEvent& event)
{
sq_newtable(v);
if (event.peer) {
Sqrat::PushVar(v, "peer");
Sqrat::PushVar(v, event.peer);
sq_newslot(v, -3, false);
}
switch (event.type) {
case ENET_EVENT_TYPE_CONNECT :
Sqrat::PushVar(v, "data");
Sqrat::PushVar(v, event.data);
sq_newslot(v, -3, false);
Sqrat::PushVar(v, "type");
Sqrat::PushVar(v, ENET_EVENT_TYPE_CONNECT);
break;
case ENET_EVENT_TYPE_DISCONNECT :
Sqrat::PushVar(v, "data");
Sqrat::PushVar(v, event.data);
sq_newslot(v, -3, false);
Sqrat::PushVar(v, "type");
Sqrat::PushVar(v, ENET_EVENT_TYPE_DISCONNECT);
break;
case ENET_EVENT_TYPE_RECEIVE :
Sqrat::PushVar(v, "data");
Sqrat::PushVar(v, std::string((const char*) event.packet->data, event.packet->dataLength));
sq_newslot(v, -3, false);
Sqrat::PushVar(v, "channel");
Sqrat::PushVar(v, event.channelID);
sq_newslot(v, -3, false);
Sqrat::PushVar(v, "type");
Sqrat::PushVar(v, ENET_EVENT_TYPE_RECEIVE);
enet_packet_destroy(event.packet);
break;
case ENET_EVENT_TYPE_NONE :
Sqrat::PushVar(v, "type");
Sqrat::PushVar(v, ENET_EVENT_TYPE_NONE);
break;
}
sq_newslot(v, -3, false);
}
////////////////////////////////////////////////////////////
static SQInteger read_packet(HSQUIRRELVM v, ENetPacket*& packet, const std::string& data, enet_uint32 flag)
{
if (flag != ENET_PACKET_FLAG_RELIABLE && flag != ENET_PACKET_FLAG_UNSEQUENCED && flag != 0) {
return sq_throwerror(v, _SC("Unknown packet flag"));
}
packet = enet_packet_create(data.c_str(), data.size(), flag);
if (packet == NULL) {
return sq_throwerror(v, _SC("Failed to create packet"));
}
return 0;
}
////////////////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////////////////
static SQInteger enetPeer_constructor(HSQUIRRELVM v)
{
return sq_throwerror(v, _SC("constructor is protected"));
}
////////////////////////////////////////////////////////////
// Requests a disconnection from a peer
////////////////////////////////////////////////////////////
static SQInteger enetPeer_disconnect(HSQUIRRELVM v)
{
if (sq_gettop(v) == 1) {
Sqrat::Var<ENetPeer*> left(v, 1);
if (!Sqrat::Error::Occurred(v)) {
enet_peer_disconnect(left.value, 0);
return 0;
}
return sq_throwerror(v, Sqrat::Error::Message(v).c_str());
} else if (sq_gettop(v) == 2) {
Sqrat::Var<ENetPeer*> left(v, 1);
Sqrat::Var<enet_uint32> data(v, 2);
if (!Sqrat::Error::Occurred(v)) {
enet_peer_disconnect(left.value, data.value);
return 0;
}
return sq_throwerror(v, Sqrat::Error::Message(v).c_str());
}
return sq_throwerror(v, _SC("wrong number of parameters"));
}
////////////////////////////////////////////////////////////
// Requests a disconnection from a peer, but only after all queued outgoing packets are sent
////////////////////////////////////////////////////////////
static SQInteger enetPeer_disconnect_later(HSQUIRRELVM v)
{
if (sq_gettop(v) == 1) {
Sqrat::Var<ENetPeer*> left(v, 1);
if (!Sqrat::Error::Occurred(v)) {
enet_peer_disconnect_later(left.value, 0);
return 0;
}
return sq_throwerror(v, Sqrat::Error::Message(v).c_str());
} else if (sq_gettop(v) == 2) {
Sqrat::Var<ENetPeer*> left(v, 1);
Sqrat::Var<enet_uint32> data(v, 2);
if (!Sqrat::Error::Occurred(v)) {
enet_peer_disconnect_later(left.value, data.value);
return 0;
}
return sq_throwerror(v, Sqrat::Error::Message(v).c_str());
}
return sq_throwerror(v, _SC("wrong number of parameters"));
}
////////////////////////////////////////////////////////////
// Force an immediate disconnection from a peer
////////////////////////////////////////////////////////////
static SQInteger enetPeer_disconnect_now(HSQUIRRELVM v)
{
if (sq_gettop(v) == 1) {
Sqrat::Var<ENetPeer*> left(v, 1);
if (!Sqrat::Error::Occurred(v)) {
enet_peer_disconnect_now(left.value, 0);
return 0;
}
return sq_throwerror(v, Sqrat::Error::Message(v).c_str());
} else if (sq_gettop(v) == 2) {
Sqrat::Var<ENetPeer*> left(v, 1);
Sqrat::Var<enet_uint32> data(v, 2);
if (!Sqrat::Error::Occurred(v)) {
enet_peer_disconnect_now(left.value, data.value);
return 0;
}
return sq_throwerror(v, Sqrat::Error::Message(v).c_str());
}
return sq_throwerror(v, _SC("wrong number of parameters"));
}
////////////////////////////////////////////////////////////
// Returns the index of the peer
////////////////////////////////////////////////////////////
static SQInteger enetPeer_index(HSQUIRRELVM v)
{
if (sq_gettop(v) == 1) {
Sqrat::Var<ENetPeer*> left(v, 1);
if (!Sqrat::Error::Occurred(v)) {
std::size_t peer_index;
for (peer_index = 0; peer_index < left.value->host->peerCount; peer_index++) {
if (left.value == &(left.value->host->peers[peer_index])) {
Sqrat::PushVar(v, peer_index + 1);
return 1;
}
}
return sq_throwerror(v, _SC("Could not find peer index"));
}
return sq_throwerror(v, Sqrat::Error::Message(v).c_str());
}
return sq_throwerror(v, _SC("wrong number of parameters"));
}
////////////////////////////////////////////////////////////
// Queues a packet to be sent
////////////////////////////////////////////////////////////
static SQInteger enetPeer_send(HSQUIRRELVM v)
{
ENetPacket* packet;
if (sq_gettop(v) == 2) {
Sqrat::Var<ENetPeer*> left(v, 1);
Sqrat::Var<const std::string&> data(v, 2);
if (!Sqrat::Error::Occurred(v)) {
SQInteger result = read_packet(v, packet, data.value, ENET_PACKET_FLAG_RELIABLE);
if (SQ_FAILED(result)) {
return result;
}
enet_peer_send(left.value, 0, packet);
return 0;
}
return sq_throwerror(v, Sqrat::Error::Message(v).c_str());
} else if (sq_gettop(v) == 3) {
Sqrat::Var<ENetPeer*> left(v, 1);
Sqrat::Var<const std::string&> data(v, 2);
Sqrat::Var<enet_uint8> channel_id(v, 3);
if (!Sqrat::Error::Occurred(v)) {
SQInteger result = read_packet(v, packet, data.value, ENET_PACKET_FLAG_RELIABLE);
if (SQ_FAILED(result)) {
return result;
}
enet_peer_send(left.value, channel_id.value, packet);
return 0;
}
return sq_throwerror(v, Sqrat::Error::Message(v).c_str());
} else if (sq_gettop(v) == 4) {
Sqrat::Var<ENetPeer*> left(v, 1);
Sqrat::Var<const std::string&> data(v, 2);
Sqrat::Var<enet_uint8> channel_id(v, 3);
Sqrat::Var<enet_uint32> flag(v, 4);
if (!Sqrat::Error::Occurred(v)) {
SQInteger result = read_packet(v, packet, data.value, flag.value);
if (SQ_FAILED(result)) {
return result;
}
enet_peer_send(left.value, channel_id.value, packet);
return 0;
}
return sq_throwerror(v, Sqrat::Error::Message(v).c_str());
}
return sq_throwerror(v, _SC("wrong number of parameters"));
}
////////////////////////////////////////////////////////////
// Returns the downstream bandwidth of the client in bytes/second
////////////////////////////////////////////////////////////
static enet_uint32 enetPeer_incoming_bandwidth(ENetPeer* left)
{
return left->incomingBandwidth;
}
////////////////////////////////////////////////////////////
// Returns the upstream bandwidth of the client in bytes/second
////////////////////////////////////////////////////////////
static enet_uint32 enetPeer_outgoing_bandwidth(ENetPeer* left)
{
return left->outgoingBandwidth;
}
////////////////////////////////////////////////////////////
// Returns the mean packet loss of reliable packets as a percentage
////////////////////////////////////////////////////////////
static enet_uint32 enetPeer_packet_loss(ENetPeer* left)
{
return left->packetLoss / (float) ENET_PEER_PACKET_LOSS_SCALE;
}
////////////////////////////////////////////////////////////
// Returns the current round trip time (i.e. ping)
////////////////////////////////////////////////////////////
static enet_uint32 enetPeer_round_trip_time(ENetPeer* left)
{
return left->roundTripTime;
}
////////////////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////////////////
static SQInteger enetHost_constructor(HSQUIRRELVM v)
{
return sq_throwerror(v, _SC("constructor is protected"));
}
////////////////////////////////////////////////////////////
// Queues a packet to be sent to all peers associated with the host
////////////////////////////////////////////////////////////
static SQInteger enetHost_broadcast(HSQUIRRELVM v)
{
ENetPacket* packet;
if (sq_gettop(v) == 2) {
Sqrat::Var<ENetHost*> left(v, 1);
Sqrat::Var<const std::string&> data(v, 2);
if (!Sqrat::Error::Occurred(v)) {
SQInteger result = read_packet(v, packet, data.value, ENET_PACKET_FLAG_RELIABLE);
if (SQ_FAILED(result)) {
return result;
}
enet_host_broadcast(left.value, 0, packet);
return 0;
}
return sq_throwerror(v, Sqrat::Error::Message(v).c_str());
} else if (sq_gettop(v) == 3) {
Sqrat::Var<ENetHost*> left(v, 1);
Sqrat::Var<const std::string&> data(v, 2);
Sqrat::Var<enet_uint8> channel_id(v, 3);
if (!Sqrat::Error::Occurred(v)) {
SQInteger result = read_packet(v, packet, data.value, ENET_PACKET_FLAG_RELIABLE);
if (SQ_FAILED(result)) {
return result;
}
enet_host_broadcast(left.value, channel_id.value, packet);
return 0;
}
return sq_throwerror(v, Sqrat::Error::Message(v).c_str());
} else if (sq_gettop(v) == 4) {
Sqrat::Var<ENetHost*> left(v, 1);
Sqrat::Var<const std::string&> data(v, 2);
Sqrat::Var<enet_uint8> channel_id(v, 3);
Sqrat::Var<enet_uint32> flag(v, 4);
if (!Sqrat::Error::Occurred(v)) {
SQInteger result = read_packet(v, packet, data.value, flag.value);
if (SQ_FAILED(result)) {
return result;
}
enet_host_broadcast(left.value, channel_id.value, packet);
return 0;
}
return sq_throwerror(v, Sqrat::Error::Message(v).c_str());
}
return sq_throwerror(v, _SC("wrong number of parameters"));
}
////////////////////////////////////////////////////////////
// Checks for any queued events on the host and dispatches one if available
////////////////////////////////////////////////////////////
static SQInteger enetHost_check_events(HSQUIRRELVM v)
{
if (sq_gettop(v) == 1) {
Sqrat::Var<ENetHost*> left(v, 1);
if (!Sqrat::Error::Occurred(v)) {
ENetEvent event;
int out = enet_host_check_events(left.value, &event);
if (out == 0) {
sq_pushnull(v);
return 1;
}
if (out < 0) return sq_throwerror(v, _SC("Error checking network event"));
push_event(v, event);
return 1;
}
return sq_throwerror(v, Sqrat::Error::Message(v).c_str());
}
return sq_throwerror(v, _SC("wrong number of parameters"));
}
////////////////////////////////////////////////////////////
// Initiates a connection to a foreign host
////////////////////////////////////////////////////////////
static SQInteger enetHost_connect(HSQUIRRELVM v)
{
ENetAddress address;
ENetPeer* peer;
if (sq_gettop(v) == 2) {
Sqrat::Var<ENetHost*> left(v, 1);
Sqrat::Var<const std::string&> addr_str(v, 2);
if (!Sqrat::Error::Occurred(v)) {
SQInteger result = parse_address(v, addr_str.value, address);
if (SQ_FAILED(result)) {
return result;
}
peer = enet_host_connect(left.value, &address, 1, 0);
if (peer == NULL) {
return sq_throwerror(v, _SC("Failed to create peer"));
}
Sqrat::PushVar(v, peer);
return 1;
}
return sq_throwerror(v, Sqrat::Error::Message(v).c_str());
} else if (sq_gettop(v) == 3) {
Sqrat::Var<ENetHost*> left(v, 1);
Sqrat::Var<const std::string&> addr_str(v, 2);
Sqrat::Var<size_t> channelCount(v, 3);
if (!Sqrat::Error::Occurred(v)) {
SQInteger result = parse_address(v, addr_str.value, address);
if (SQ_FAILED(result)) {
return result;
}
peer = enet_host_connect(left.value, &address, channelCount.value, 0);
if (peer == NULL) {
return sq_throwerror(v, _SC("Failed to create peer"));
}
Sqrat::PushVar(v, peer);
return 1;
}
return sq_throwerror(v, Sqrat::Error::Message(v).c_str());
} else if (sq_gettop(v) == 4) {
Sqrat::Var<ENetHost*> left(v, 1);
Sqrat::Var<const std::string&> addr_str(v, 2);
Sqrat::Var<size_t> channelCount(v, 3);
Sqrat::Var<enet_uint32> data(v, 4);
if (!Sqrat::Error::Occurred(v)) {
SQInteger result = parse_address(v, addr_str.value, address);
if (SQ_FAILED(result)) {
return result;
}
peer = enet_host_connect(left.value, &address, channelCount.value, data.value);
if (peer == NULL) {
return sq_throwerror(v, _SC("Failed to create peer"));
}
Sqrat::PushVar(v, peer);
return 1;
}
return sq_throwerror(v, Sqrat::Error::Message(v).c_str());
}
return sq_throwerror(v, _SC("wrong number of parameters"));
}
////////////////////////////////////////////////////////////
// Waits for events on the host specified and shuttles packets between the host and its peers
////////////////////////////////////////////////////////////
static SQInteger enetHost_service(HSQUIRRELVM v)
{
if (sq_gettop(v) == 1) {
Sqrat::Var<ENetHost*> left(v, 1);
if (!Sqrat::Error::Occurred(v)) {
ENetEvent event;
int out = enet_host_service(left.value, &event, 0);
if (out == 0) {
sq_pushnull(v);
return 1;
}
if (out < 0) return sq_throwerror(v, _SC("Error during network servicing"));
push_event(v, event);
return 1;
}
return sq_throwerror(v, Sqrat::Error::Message(v).c_str());
}
return sq_throwerror(v, _SC("wrong number of parameters"));
}
////////////////////////////////////////////////////////////
// Enables an adaptive order-2 PPM range coder for the transmitted data of all peers
////////////////////////////////////////////////////////////
static bool enetHost_compress_with_range_coder(ENetHost* left)
{
if (enet_host_compress_with_range_coder(left) == 0) {
return true;
}
return false;
}
////////////////////////////////////////////////////////////
// Returns a new host for communicating to peers
////////////////////////////////////////////////////////////
static SQInteger host_create(HSQUIRRELVM v)
{
ENetAddress address;
ENetHost* host;
if (sq_gettop(v) == 2) {
Sqrat::Var<Sqrat::SharedPtr<std::string> > addr_str(v, 2);
if (!Sqrat::Error::Occurred(v)) {
if (addr_str.value.Get() != NULL) {
SQInteger result = parse_address(v, *addr_str.value, address);
if (SQ_FAILED(result)) {
return result;
}
}
host = enet_host_create(addr_str.value.Get() != NULL ? &address : NULL, 64, 1, 0, 0);
if (host == NULL) {
wr::Print(v, "Failed to create host (port already in use?)\n");
sq_pushnull(v);
return 1;
}
Sqrat::PushVar(v, host);
return 1;
}
return sq_throwerror(v, Sqrat::Error::Message(v).c_str());
} else if (sq_gettop(v) == 3) {
Sqrat::Var<Sqrat::SharedPtr<std::string> > addr_str(v, 2);
Sqrat::Var<size_t> peerCount(v, 3);
if (!Sqrat::Error::Occurred(v)) {
if (addr_str.value.Get() != NULL) {
SQInteger result = parse_address(v, *addr_str.value, address);
if (SQ_FAILED(result)) {
return result;
}
}
host = enet_host_create(addr_str.value.Get() != NULL ? &address : NULL, peerCount.value, 1, 0, 0);
if (host == NULL) {
wr::Print(v, "Failed to create host (port already in use?)\n");
sq_pushnull(v);
return 1;
}
Sqrat::PushVar(v, host);
return 1;
}
return sq_throwerror(v, Sqrat::Error::Message(v).c_str());
} else if (sq_gettop(v) == 4) {
Sqrat::Var<Sqrat::SharedPtr<std::string> > addr_str(v, 2);
Sqrat::Var<size_t> peerCount(v, 3);
Sqrat::Var<size_t> channelLimit(v, 4);
if (!Sqrat::Error::Occurred(v)) {
if (addr_str.value.Get() != NULL) {
SQInteger result = parse_address(v, *addr_str.value, address);
if (SQ_FAILED(result)) {
return result;
}
}
host = enet_host_create(addr_str.value.Get() != NULL ? &address : NULL, peerCount.value, channelLimit.value, 0, 0);
if (host == NULL) {
wr::Print(v, "Failed to create host (port already in use?)\n");
sq_pushnull(v);
return 1;
}
Sqrat::PushVar(v, host);
return 1;
}
return sq_throwerror(v, Sqrat::Error::Message(v).c_str());
} else if (sq_gettop(v) == 5) {
Sqrat::Var<Sqrat::SharedPtr<std::string> > addr_str(v, 2);
Sqrat::Var<size_t> peerCount(v, 3);
Sqrat::Var<size_t> channelLimit(v, 4);
Sqrat::Var<enet_uint32> incomingBandwidth(v, 5);
if (!Sqrat::Error::Occurred(v)) {
if (addr_str.value.Get() != NULL) {
SQInteger result = parse_address(v, *addr_str.value, address);
if (SQ_FAILED(result)) {
return result;
}
}
host = enet_host_create(addr_str.value.Get() != NULL ? &address : NULL, peerCount.value, channelLimit.value, incomingBandwidth.value, 0);
if (host == NULL) {
wr::Print(v, "Failed to create host (port already in use?)\n");
sq_pushnull(v);
return 1;
}
Sqrat::PushVar(v, host);
return 1;
}
return sq_throwerror(v, Sqrat::Error::Message(v).c_str());
} else if (sq_gettop(v) == 6) {
Sqrat::Var<Sqrat::SharedPtr<std::string> > addr_str(v, 2);
Sqrat::Var<size_t> peerCount(v, 3);
Sqrat::Var<size_t> channelLimit(v, 4);
Sqrat::Var<enet_uint32> incomingBandwidth(v, 5);
Sqrat::Var<enet_uint32> outgoingBandwidth(v, 6);
if (!Sqrat::Error::Occurred(v)) {
if (addr_str.value.Get() != NULL) {
SQInteger result = parse_address(v, *addr_str.value, address);
if (SQ_FAILED(result)) {
return result;
}
}
host = enet_host_create(addr_str.value.Get() != NULL ? &address : NULL, peerCount.value, channelLimit.value, incomingBandwidth.value, outgoingBandwidth.value);
if (host == NULL) {
wr::Print(v, "Failed to create host (port already in use?)\n");
sq_pushnull(v);
return 1;
}
Sqrat::PushVar(v, host);
return 1;
}
return sq_throwerror(v, Sqrat::Error::Message(v).c_str());
}
return sq_throwerror(v, _SC("wrong number of parameters"));
}
////////////////////////////////////////////////////////////
// Initializes and registers the ENet library in the given VM
////////////////////////////////////////////////////////////
void RegisterEnetLib(HSQUIRRELVM v, Sqrat::Table& namespaceTable)
{
Sqrat::ConstTable constTable(v);
constTable.Const(_SC("ENET_EVENT_TYPE_CONNECT"), static_cast<int>(ENET_EVENT_TYPE_CONNECT));
constTable.Const(_SC("ENET_EVENT_TYPE_DISCONNECT"), static_cast<int>(ENET_EVENT_TYPE_DISCONNECT));
constTable.Const(_SC("ENET_EVENT_TYPE_RECEIVE"), static_cast<int>(ENET_EVENT_TYPE_RECEIVE));
constTable.Const(_SC("ENET_EVENT_TYPE_NONE"), static_cast<int>(ENET_EVENT_TYPE_NONE));
constTable.Const(_SC("ENET_PACKET_FLAG_RELIABLE"), static_cast<int>(ENET_PACKET_FLAG_RELIABLE));
constTable.Const(_SC("ENET_PACKET_FLAG_UNSEQUENCED"), static_cast<int>(ENET_PACKET_FLAG_UNSEQUENCED));
constTable.Const(_SC("ENET_PACKET_FLAG_UNRELIABLE"), 0);
Sqrat::Class<ENetPeer, Sqrat::NoConstructor<ENetPeer> > enetPeer(v, _SC("enet.Peer"));
enetPeer.SquirrelFunc(_SC("constructor"), &enetPeer_constructor);
enetPeer.SquirrelFunc(_SC("disconnect"), &enetPeer_disconnect);
enetPeer.SquirrelFunc(_SC("disconnect_later"), &enetPeer_disconnect_later);
enetPeer.SquirrelFunc(_SC("disconnect_now"), &enetPeer_disconnect_now);
enetPeer.SquirrelFunc(_SC("index"), &enetPeer_index);
enetPeer.SquirrelFunc(_SC("send"), &enetPeer_send);
enetPeer.GlobalFunc(_SC("destroy"), &enet_host_destroy);
enetPeer.GlobalFunc(_SC("incoming_bandwidth"), &enetPeer_incoming_bandwidth);
enetPeer.GlobalFunc(_SC("outgoing_bandwidth"), &enetPeer_outgoing_bandwidth);
enetPeer.GlobalFunc(_SC("packet_loss"), &enetPeer_packet_loss);
enetPeer.GlobalFunc(_SC("ping"), &enet_peer_ping);
enetPeer.GlobalFunc(_SC("ping_interval"), &enet_peer_ping_interval);
enetPeer.GlobalFunc(_SC("reset"), &enet_peer_reset);
enetPeer.GlobalFunc(_SC("round_trip_time"), &enetPeer_round_trip_time);
enetPeer.GlobalFunc(_SC("throttle_configure"), &enet_peer_throttle_configure);
enetPeer.GlobalFunc(_SC("timeout"), &enet_peer_timeout);
Sqrat::Class<ENetHost, Sqrat::NoConstructor<ENetHost> > enetHost(v, _SC("enet.Host"));
enetHost.SquirrelFunc(_SC("constructor"), &enetHost_constructor);
enetHost.SquirrelFunc(_SC("broadcast"), &enetHost_broadcast);
enetHost.SquirrelFunc(_SC("check_events"), &enetHost_check_events);
enetHost.SquirrelFunc(_SC("connect"), &enetHost_connect);
enetHost.SquirrelFunc(_SC("service"), &enetHost_service);
enetHost.GlobalFunc(_SC("bandwidth_limit"), &enet_host_bandwidth_limit);
enetHost.GlobalFunc(_SC("compress_with_range_coder"), &enetHost_compress_with_range_coder);
namespaceTable.Bind(_SC("Host"), enetHost);
namespaceTable.SquirrelFunc(_SC("host_create"), &host_create);
}