-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNetworkHandler.cpp
633 lines (527 loc) · 19.4 KB
/
NetworkHandler.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
#include "NetworkHandler.h"
#include <thread>
NetworkHandler::NetworkHandler() {}
void NetworkHandler::setVerbose(bool enable) {
verbose = enable;
}
std::unordered_map<enet_uint16, ENetPeer*>& NetworkHandler::getPeerMap() {
return peerMap;
}
bool NetworkHandler::initialize() {
if (initialized) {
if (verbose) dConsole.sendMsg("Networking WARNING: ENet is already initialized", MESSAGE_TYPE::NETWORK_VERBOSE);
return true;
}
if (enet_initialize() == 0) {
initialized = true;
if (verbose) dConsole.sendMsg("ENet initialized successfully", MESSAGE_TYPE::NETWORK_VERBOSE);
return true;
}
if (verbose) dConsole.sendMsg("Networking WARNING: Failed to initialize ENet", MESSAGE_TYPE::NETWORK_VERBOSE);
return false;
}
bool NetworkHandler::shutdown() {
if (!initialized) {
if (verbose) dConsole.sendMsg("Networking WARNING: ENet cannot be shutdown; ENet is not initialized", MESSAGE_TYPE::NETWORK_VERBOSE);
return false;
}
initialized = false;
finished = true;
netServerThread.join();
netClientThread.join();
if (server) {
enet_host_flush(server);
enet_host_destroy(server);
}
if (client) {
enet_host_flush(client);
enet_host_destroy(client);
}
enet_deinitialize();
return true;
}
void NetworkHandler::handle(IrrHandling* m) {
netServerThread = std::thread(netBodyServer, this, m);
netClientThread = std::thread(netBodyClient, this, m);
}
ENetHost* NetworkHandler::getHost() {
return server;
}
ENetHost* NetworkHandler::getClient() {
return client;
}
ENetPeer* NetworkHandler::getPeer() {
return peer;
}
int NetworkHandler::getPeerState(int peerID) {
if (!server) {
if (verbose) dConsole.sendMsg("Networking WARNING: Could not fetch peer state; server is not being hosted", MESSAGE_TYPE::NETWORK_VERBOSE);
return -1;
}
ENetPeer* p = peerMap[peerID];
if (!p) {
if (verbose) {
std::string msg = "Networking WARNING: Peer with ID ";
msg += std::to_string(peerID);
msg += " could not be fetched";
dConsole.sendMsg(msg.c_str(), MESSAGE_TYPE::NETWORK_VERBOSE);
}
return -1;
}
return (int)p->state;
}
int NetworkHandler::getPeerPing(int peerID) {
if (!server && !client) {
if (verbose) dConsole.sendMsg("Networking WARNING: Could not fetch peer ping; client is not connected to a server", MESSAGE_TYPE::NETWORK_VERBOSE);
return -1;
}
ENetPeer* p = peerMap[peerID];
if (!p) {
if (verbose) {
std::string msg = "Networking WARNING: Peer with ID ";
msg += std::to_string(peerID);
msg += "'s ping could not be fetched";
dConsole.sendMsg(msg.c_str(), MESSAGE_TYPE::NETWORK_VERBOSE);
}
return -1;
}
return (int)p->roundTripTime;
}
void NetworkHandler::forceDisconnectClient(int peerID, int reason) {
if (!server) {
if (verbose) dConsole.sendMsg("Networking WARNING: Could not kick client; server is not being hosted", MESSAGE_TYPE::NETWORK_VERBOSE);
return;
}
ENetPeer* p = peerMap[peerID];
if (!p) {
if (verbose) {
std::string msg = "Networking WARNING: Peer with ID ";
msg += std::to_string(peerID);
msg += " could not be disconnected";
dConsole.sendMsg(msg.c_str(), MESSAGE_TYPE::NETWORK_VERBOSE);
}
return;
}
if (verbose) {
std::string msg = "Peer with ID ";
msg += std::to_string(peerID);
msg += " disconnected for reason code ";
msg += std::to_string(reason);
dConsole.sendMsg(msg.c_str(), MESSAGE_TYPE::NETWORK_VERBOSE);
}
enet_peer_disconnect(p,reason);
}
void netBodyServer(NetworkHandler* n, IrrHandling* m) {
// Server Network Loop
ENetEvent event;
sol::protected_function onPeerConnect = (*lua)["NetworkServer"]["OnClientConnect"];
sol::protected_function onPeerDisconnect = (*lua)["NetworkServer"]["OnClientDisconnect"];
sol::protected_function onPacketReceived = (*lua)["NetworkServer"]["OnPacketReceived"];
while (!n->finished) {
if (!n->initialized || !(n->getHost())) {
std::this_thread::yield();
continue;
}
int out = enet_host_service(n->getHost(), &event, 1);
if (out <= 0) continue;
switch (event.type) {
case ENET_EVENT_TYPE_CONNECT:
if (onPeerConnect.valid())
m->addLuaTask(onPeerConnect, sol::table());
else {
if (n->verbose) dConsole.sendMsg("Networking WARNING: A peer connected but NetworkServer.OnClientConnect is not declared", MESSAGE_TYPE::NETWORK_VERBOSE);
}
n->getPeerMap()[event.peer->incomingPeerID] = event.peer;
if (n->verbose) {
std::string msg = "Client joined presuming ID ";
msg += std::to_string(event.peer->incomingPeerID);
msg += " from IP ";
msg += std::to_string(event.peer->address.host);
dConsole.sendMsg(msg.c_str(), MESSAGE_TYPE::NETWORK_VERBOSE);
}
break;
case ENET_EVENT_TYPE_DISCONNECT:
if (onPeerDisconnect.valid())
m->addLuaTask(onPeerDisconnect, sol::table());
else {
if (n->verbose) dConsole.sendMsg("Networking WARNING: A peer disconnected but NetworkServer.OnClientDisconnect is not declared", MESSAGE_TYPE::NETWORK_VERBOSE);
}
n->getPeerMap().erase(event.peer->incomingPeerID);
if (n->verbose) {
std::string msg = "Client disconnected abandoning ID ";
msg += std::to_string(event.peer->incomingPeerID);
msg += " from IP ";
msg += std::to_string(event.peer->address.host);
dConsole.sendMsg(msg.c_str(), MESSAGE_TYPE::NETWORK_VERBOSE);
}
break;
case ENET_EVENT_TYPE_RECEIVE:
if (onPacketReceived.valid()) {
sol::table t = lua->create_table(); // Order matters here. Pushing the packet first causes crashes!
t[1] = event.channelID;
t[2] = Packet(event.packet, event.peer->incomingPeerID);
m->addLuaTask(onPacketReceived, t);
} else {
if (n->verbose) dConsole.sendMsg("Networking WARNING: A packet was received but NetworkServer.OnPacketReceived is not declared", MESSAGE_TYPE::NETWORK_VERBOSE);
enet_packet_destroy(event.packet);
}
break;
}
}
}
void netBodyClient(NetworkHandler* n, IrrHandling* m) {
// Client Network Loop
ENetEvent event;
sol::protected_function onConnect = (*lua)["NetworkClient"]["OnConnect"];
sol::protected_function onDisconnect = (*lua)["NetworkClient"]["OnDisconnect"];
sol::protected_function onPacketReceived = (*lua)["NetworkClient"]["OnPacketReceived"];
while (!n->finished) {
if (!n->initialized || !(n->getClient())) {
std::this_thread::yield();
continue;
}
if (!n->getPeer()) continue;
int out = enet_host_service(n->getClient(), &event, 1);
if (out <= 0) continue;
switch (event.type) {
case ENET_EVENT_TYPE_CONNECT:
if (onConnect.valid())
m->addLuaTask(onConnect, sol::table());
else {
if (n->verbose) dConsole.sendMsg("Networking WARNING: Client connected but NetworkClient.OnConnect is not declared", MESSAGE_TYPE::NETWORK_VERBOSE);
}
//if (!n->getHost()) n->getPeerMap()[event.peer->incomingPeerID] = event.peer;
if (n->verbose) {
std::string msg = "Connected to server via client ";
dConsole.sendMsg(msg.c_str(), MESSAGE_TYPE::NETWORK_VERBOSE);
}
break;
case ENET_EVENT_TYPE_DISCONNECT:
if (onDisconnect.valid()) {
sol::table t = lua->create_table();
t[1] = event.data;
m->addLuaTask(onDisconnect, t);
}
else {
if (n->verbose) dConsole.sendMsg("Networking WARNING: Client disconnected but NetworkClient.OnDisconnect is not declared", MESSAGE_TYPE::NETWORK_VERBOSE);
}
//if (!n->getHost()) n->getPeerMap().erase(event.peer->incomingPeerID);
if (n->verbose) {
std::string msg = "Disconnected from server as client, reason code ";
msg += std::to_string(event.data);
dConsole.sendMsg(msg.c_str(), MESSAGE_TYPE::NETWORK_VERBOSE);
}
break;
case ENET_EVENT_TYPE_RECEIVE:
if (onPacketReceived.valid()) {
sol::table t = lua->create_table();
t[1] = event.channelID;
t[2] = Packet(event.packet, event.peer->incomingPeerID);
m->addLuaTask(onPacketReceived, t);
}
else {
if (n->verbose) dConsole.sendMsg("Networking WARNING: A packet was received but NetworkClient.OnPacketReceived is not declared", MESSAGE_TYPE::NETWORK_VERBOSE);
enet_packet_destroy(event.packet);
}
break;
}
}
}
void NetworkHandler::hostServer(std::string ip, int port, int maxClients, int maxChannels) {
if (!initialized) {
if (verbose) dConsole.sendMsg("Networking WARNING: Host cannot be created; ENet is not initialized", MESSAGE_TYPE::NETWORK_VERBOSE);
return;
}
if (server) {
if (verbose) dConsole.sendMsg("Networking WARNING: Host cannot be created; there is an ongoing host", MESSAGE_TYPE::NETWORK_VERBOSE);
return;
}
ENetAddress address;
address.host = ENET_HOST_ANY; // Hosts on localhost by default
enet_address_set_host(&address, ip.c_str());
address.port = port;
server = enet_host_create(&address, maxClients, maxChannels, 0, 0);
char ipString[64];
int out = enet_address_get_host_ip(&server->address, ipString, sizeof(ipString));
if (!server || out != 0) {
std::string ms = "";
ms = "Networking WARNING: Server could not be hosted on IP ";
ms += ipString;
if (verbose) dConsole.sendMsg(ms.c_str(), MESSAGE_TYPE::NETWORK_VERBOSE);
sol::protected_function onHostFail = (*lua)["NetworkServer"]["OnHostFail"];
if (onHostFail.valid()) {
sol::protected_function_result result = onHostFail();
}
else if (verbose) {
dConsole.sendMsg("Networking WARNING: The server failed to host but NetworkServer.OnHostFail is not declared", MESSAGE_TYPE::NETWORK_VERBOSE);
}
if (verbose) {
dConsole.sendMsg("Networking WARNING: The server is being hosted but NetworkServer.OnHosted is not declared", MESSAGE_TYPE::NETWORK_VERBOSE);
}
if (server)
enet_host_destroy(server);
server = nullptr;
return;
}
if (verbose) {
std::string msg = "Hosting server on ";
msg += ipString;
msg += " for ";
msg += std::to_string(maxClients);
msg += " clients with ";
msg += std::to_string(maxChannels);
msg += " channels";
dConsole.sendMsg(msg.c_str(), MESSAGE_TYPE::NETWORK_VERBOSE);
}
sol::protected_function onHosted = (*lua)["NetworkServer"]["OnHosted"];
if (onHosted.valid()) {
sol::protected_function_result result = onHosted();
}
else if (verbose) {
dConsole.sendMsg("Networking WARNING: The server is being hosted but NetworkServer.OnHosted is not declared", MESSAGE_TYPE::NETWORK_VERBOSE);
}
}
bool NetworkHandler::stopHosting() {
if (server) {
enet_host_flush(server);
enet_host_destroy(server);
if (verbose) dConsole.sendMsg("Server hosting closed", MESSAGE_TYPE::NETWORK_VERBOSE);
return true;
}
else {
if (verbose) dConsole.sendMsg("Networking WARNING: NetworkServer.StopHosting was called but there is no server to stop hosting", MESSAGE_TYPE::NETWORK_VERBOSE);
return false;
}
}
void NetworkHandler::setBandwidthLimit(int incoming, int outgoing) {
if (!server) {
if (verbose) dConsole.sendMsg("Networking WARNING: Setting bandwidth limits must be done after hosting a server", MESSAGE_TYPE::NETWORK_VERBOSE);
return;
}
enet_host_bandwidth_limit(server, incoming, outgoing); // in bytes per second
}
bool NetworkHandler::isHosting() {
return server;
}
int NetworkHandler::getServerIP() {
return server ? server->address.host : 0;
}
int NetworkHandler::getPort() {
return server ? server->address.port : 0;
}
void NetworkHandler::setUseRangeEncoder(bool enable) {
if (server && enable)
enet_host_compress_with_range_coder(server);
else if (server)
enet_host_compress(server, nullptr);
if (client && enable)
enet_host_compress_with_range_coder(client);
else if (client)
enet_host_compress(client, nullptr);
if (!server && !client && verbose) dConsole.sendMsg("Networking WARNING: Enabling/disabling compression must be done after being connected to a server", MESSAGE_TYPE::NETWORK_VERBOSE);
else if (verbose) {
if (enable) dConsole.sendMsg("Range-encoding compressor enabled", MESSAGE_TYPE::NETWORK_VERBOSE);
else dConsole.sendMsg("Range-encoding compressor disabled", MESSAGE_TYPE::NETWORK_VERBOSE);
}
}
// Client
bool NetworkHandler::createClient(int outgoing, int channels) {
if (!initialized) {
if (verbose) dConsole.sendMsg("Networking WARNING: A call to create a client was made but networking is not initialized", MESSAGE_TYPE::NETWORK_VERBOSE);
return false;
}
if (client) {
if (verbose) dConsole.sendMsg("Networking WARNING: Client could not be created; current client should be destroyed first", MESSAGE_TYPE::NETWORK_VERBOSE);
return false;
}
client = enet_host_create(NULL, outgoing, channels, 0, 0);
if (!client) {
if (verbose) dConsole.sendMsg("Networking WARNING: Client could not be created", MESSAGE_TYPE::NETWORK_VERBOSE);
return false;
}
if (verbose) {
std::string msg = "Created client with ";
msg += std::to_string(outgoing);
msg += " outgoing connection(s) and ";
msg += std::to_string(channels);
msg += " channel(s) accessible";
dConsole.sendMsg(msg.c_str(), MESSAGE_TYPE::NETWORK_VERBOSE);
}
return true;
}
void NetworkHandler::connectClient(std::string ad, int port, int channels) {
if (!initialized) {
if (verbose) dConsole.sendMsg("Networking WARNING: A call to connect a client was made but networking is not initialized", MESSAGE_TYPE::NETWORK_VERBOSE);
return;
}
if (!client) {
if (verbose) dConsole.sendMsg("Networking WARNING: Client could not connect to address; client is not created", MESSAGE_TYPE::NETWORK_VERBOSE);
return;
}
ENetAddress address;
enet_address_set_host(&address, ad.c_str());
address.port = port;
if (verbose) {
std::string msg = "Client attempting to connect to ";
msg += ad;
msg += ":";
msg += std::to_string(port);
dConsole.sendMsg(msg.c_str(), MESSAGE_TYPE::NETWORK_VERBOSE);
}
std::thread connectThread([this, address, channels]() {
peer = enet_host_connect(client, &address, channels, 0);
if (!peer) {
if (verbose) dConsole.sendMsg("Networking WARNING: Failed to create peer connection", MESSAGE_TYPE::NETWORK_VERBOSE);
sol::protected_function f = (*lua)["NetworkClient"]["OnConnectFail"];
if (f.valid()) {
sol::protected_function_result result = f();
}
return;
}
else {
ENetEvent event;
if (enet_host_service(client, &event, 5000) > 0 && event.type == ENET_EVENT_TYPE_CONNECT) {
if (verbose) dConsole.sendMsg("Client connected to server", MESSAGE_TYPE::NETWORK_VERBOSE);
sol::protected_function f = (*lua)["NetworkClient"]["OnConnect"];
if (f.valid()) {
sol::protected_function_result result = f();
}
}
else {
if (verbose) dConsole.sendMsg("Client failed to connect to server", MESSAGE_TYPE::NETWORK_VERBOSE);
sol::protected_function f = (*lua)["NetworkClient"]["OnConnectFail"];
if (f.valid()) {
sol::protected_function_result result = f();
}
}
}
});
connectThread.detach();
}
void NetworkHandler::disconnectClient() {
if (!initialized) {
if (verbose) dConsole.sendMsg("Networking WARNING: A call to disconnect the client was made but networking is not initialized", MESSAGE_TYPE::NETWORK_VERBOSE);
return;
}
if (!client) {
if (verbose) dConsole.sendMsg("Networking WARNING: A call to disconnect the client was made but there is no available client", MESSAGE_TYPE::NETWORK_VERBOSE);
return;
}
if (!peer) {
if (verbose) dConsole.sendMsg("Networking WARNING: A call to disconnect the client was made but the client is not connected to a server", MESSAGE_TYPE::NETWORK_VERBOSE);
return;
}
if (verbose) dConsole.sendMsg("Disconnecting client from server", MESSAGE_TYPE::NETWORK_VERBOSE);
enet_peer_disconnect(peer, 0);
}
bool NetworkHandler::destroyClient() {
if (!initialized) {
if (verbose) dConsole.sendMsg("Networking WARNING: A call to destroy the client was made but networking is not initialized", MESSAGE_TYPE::NETWORK_VERBOSE);
return false;
}
if (!client) {
if (verbose) dConsole.sendMsg("Networking WARNING: A call to destroy the client was made but there is no available client", MESSAGE_TYPE::NETWORK_VERBOSE);
return false;
}
enet_host_flush(client);
enet_host_destroy(client);
return true;
}
bool NetworkHandler::isClientConnected() {
return peer && client && peer->state == ENET_PEER_STATE_CONNECTED;
}
void NetworkHandler::sendPacketToServer(const Packet& p, int channel, bool tcp) {
if (!initialized) {
if (verbose) dConsole.sendMsg("Networking WARNING: A call to send a packet to the server was made but networking is not initialized", MESSAGE_TYPE::NETWORK_VERBOSE);
return;
}
if (!client) {
if (verbose) dConsole.sendMsg("Networking WARNING: Packet could not be sent to the server; not connected to a server", MESSAGE_TYPE::NETWORK_VERBOSE);
return;
}
if (server && !client) {
if (verbose) dConsole.sendMsg("Networking WARNING: Cannot send packet to self; server is this application instance", MESSAGE_TYPE::NETWORK_VERBOSE);
return;
}
if (!p.p) {
if (verbose) dConsole.sendMsg("Networking WARNING: Packet could not be sent to the server; packet is invalid", MESSAGE_TYPE::NETWORK_VERBOSE);
return;
}
p.p->flags = tcp ? ENET_PACKET_FLAG_RELIABLE : ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT;
enet_peer_send(peer, channel, p.p);
enet_host_flush(client);
if (verbose) {
std::string msg = "Packet of size ";
msg += std::to_string(p.p->dataLength);
msg += "B sent to server";
msg += " on channel ";
msg += std::to_string(channel);
msg += " via ";
msg += tcp ? "TCP" : "UDP";
dConsole.sendMsg(msg.c_str(), MESSAGE_TYPE::NETWORK_VERBOSE);
}
}
void NetworkHandler::sendPacketToPeer(int peerID, const Packet& p, int channel, bool tcp) {
if (!initialized) {
if (verbose) dConsole.sendMsg("Networking WARNING: A call to send a packet to a peer was made but networking is not initialized", MESSAGE_TYPE::NETWORK_VERBOSE);
return;
}
if (!client && !server) {
if (verbose) dConsole.sendMsg("Networking WARNING: Packet could not be sent to peer; not connected to a server", MESSAGE_TYPE::NETWORK_VERBOSE);
return;
}
if (!p.p) {
if (verbose) dConsole.sendMsg("Networking WARNING: Packet could not be sent to peer; packet is invalid", MESSAGE_TYPE::NETWORK_VERBOSE);
return;
}
ENetPeer* thisPeer = peerMap[peerID];
if (!thisPeer) {
if (verbose) {
std::string msg = "Networking WARNING: Failed to send packet to peer with ID ";
msg += std::to_string(peerID);
msg += "; peer does not exist";
dConsole.sendMsg(msg.c_str(), MESSAGE_TYPE::NETWORK_VERBOSE);
}
return;
}
p.p->flags = tcp ? ENET_PACKET_FLAG_RELIABLE : ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT;
enet_peer_send(thisPeer, channel, p.p);
enet_host_flush(server ? server : client);
if (verbose) {
std::string msg = "Packet of size ";
msg += std::to_string(p.p->dataLength);
msg += "B sent to peer with ID ";
msg += std::to_string(peerID);
msg += " on channel ";
msg += std::to_string(channel);
msg += " via ";
msg += tcp ? "TCP" : "UDP";
dConsole.sendMsg(msg.c_str(), MESSAGE_TYPE::NETWORK_VERBOSE);
}
}
void NetworkHandler::sendPacketToAll(const Packet& p, int channel, bool tcp) {
if (!initialized) {
if (verbose) dConsole.sendMsg("Networking WARNING: A call to send a packet to all was made but networking is not initialized", MESSAGE_TYPE::NETWORK_VERBOSE);
return;
}
if (!client && !server) {
if (verbose) dConsole.sendMsg("Networking WARNING: Packet could not be sent to all; not connected to a server", MESSAGE_TYPE::NETWORK_VERBOSE);
return;
}
if (!p.p) {
if (verbose) dConsole.sendMsg("Networking WARNING: Packet could not be sent to all; packet is invalid", MESSAGE_TYPE::NETWORK_VERBOSE);
return;
}
p.p->flags = tcp ? ENET_PACKET_FLAG_RELIABLE : ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT;
enet_host_broadcast(server ? server : client, channel, p.p);
if (verbose) {
std::string msg = "Packet of size ";
msg += std::to_string(p.p->dataLength);
msg += "B sent to all ";
msg += " on channel ";
msg += std::to_string(channel);
msg += " via ";
msg += tcp ? "TCP" : "UDP";
dConsole.sendMsg(msg.c_str(), MESSAGE_TYPE::NETWORK_VERBOSE);
}
}