Debian Codegraph @

src/cdogs/net_server.c

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4
5 Copyright (c) 2014-2017, 2021, 2023-2024 Cong Xu
6 All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10
11 Redistributions of source code must retain the above copyright notice, this
12 list of conditions and the following disclaimer.
13 Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 POSSIBILITY OF SUCH DAMAGE.
28 */
29 #include "net_server.h"
30
31 #include <string.h>
32
33 #include "proto/nanopb/pb_encode.h"
34
35 #include "actor_placement.h"
36 #include "ai_utils.h"
37 #include "campaign_entry.h"
38 #include "events.h"
39 #include "game_events.h"
40 #include "gamedata.h"
41 #include "handle_game_events.h"
42 #include "log.h"
43 #include "los.h"
44 #include "pickup.h"
45 #include "player.h"
46 #include "sys_config.h"
47 #include "utils.h"
48
49 NetServer gNetServer;
50
51 void NetServerInit(NetServer *n)
52 {
53 memset(n, 0, sizeof *n);
54 }
55 void NetServerTerminate(NetServer *n)
56 {
57 NetServerClose(n);
58 }
59 void NetServerReset(NetServer *n)
60 {
61 n->PrevCmd = n->Cmd = 0;
62 }
63
64 static ENetHost *HostOpen(void);
65 static bool ListenSocketTryOpen(ENetSocket *listen, const enet_uint16 port);
66 void NetServerOpen(NetServer *n, const uint16_t port)
67 {
68 if (n->server)
69 {
70 return;
71 }
72
73 n->server = HostOpen();
74 if (n->server == NULL)
75 {
76 return;
77 }
78
79 // Start listen socket, to respond to UDP scans
80 if (!ListenSocketTryOpen(&n->listen, port))
81 {
82 return;
83 }
84
85 // Save limited length hostname for LAN scanners
86 // This is because getting hostname locally is much faster
87 if (enet_address_get_host(
88 &n->server->address, n->hostname, sizeof n->hostname) != 0)
89 {
90 LOG(LM_NET, LL_WARN, "Failed to get hostname");
91 n->hostname[0] = '\0';
92 }
93 }
94 static ENetHost *HostOpen(void)
95 {
96 ENetAddress address;
97 address.host = ENET_HOST_ANY;
98 address.port = ENET_PORT_ANY;
99 ENetHost *host =
100 enet_host_create(&address, NET_SERVER_MAX_CLIENTS, 2, 0, 0);
101 if (host == NULL)
102 {
103 LOG(LM_NET, LL_ERROR, "cannot create server host");
104 return NULL;
105 }
106 else
107 {
108 char buf[256];
109 enet_address_get_host_ip(&host->address, buf, sizeof buf);
110 LOG(LM_NET, LL_INFO, "starting server on %s:%u", buf,
111 host->address.port);
112 }
113 return host;
114 }
115 static bool ListenSocketTryOpen(ENetSocket *listen, const enet_uint16 port)
116 {
117 *listen = enet_socket_create(ENET_SOCKET_TYPE_DATAGRAM);
118 if (*listen == ENET_SOCKET_NULL)
119 {
120 LOG(LM_NET, LL_ERROR, "Failed to create socket");
121 return false;
122 }
123 if (enet_socket_set_option(*listen, ENET_SOCKOPT_REUSEADDR, 1) != 0)
124 {
125 LOG(LM_NET, LL_ERROR, "Failed to enable reuse address");
126 return false;
127 }
128 ENetAddress addr;
129 addr.host = ENET_HOST_ANY;
130 addr.port = port;
131 if (enet_socket_bind(*listen, &addr) != 0)
132 {
133 LOG(LM_NET, LL_ERROR, "failed to bind listen socket");
134 return false;
135 }
136 if (enet_socket_get_address(*listen, &addr) != 0)
137 {
138 LOG(LM_NET, LL_ERROR, "cannot get listen socket address");
139 return false;
140 }
141 LOG(LM_NET, LL_INFO, "listening for scans on port %u", addr.port);
142 return true;
143 }
144
145 void NetServerClose(NetServer *n)
146 {
147 if (n->server)
148 {
149 for (int i = 0; i < (int)n->server->peerCount; i++)
150 {
151 ENetPeer *peer = n->server->peers + i;
152 enet_peer_disconnect_now(peer, 0);
153 }
154 enet_host_destroy(n->server);
155 }
156 n->server = NULL;
157 }
158
159 static void PollListener(NetServer *n);
160 static void OnReceive(NetServer *n, ENetEvent event);
161 static void OnDisconnect(const ENetEvent event);
162 void NetServerPoll(NetServer *n)
163 {
164 if (!n->server)
165 {
166 return;
167 }
168
169 // Check our listening socket for scanning clients
170 PollListener(n);
171
172 n->PrevCmd = n->Cmd;
173 n->Cmd = 0;
174 int check;
175 do
176 {
177 ENetEvent event;
178 check = enet_host_service(n->server, &event, 0);
179 if (check < 0)
180 {
181 fprintf(stderr, "Host check event failure\n");
182 enet_host_destroy(n->server);
183 n->server = NULL;
184 return;
185 }
186 else if (check > 0)
187 {
188 switch (event.type)
189 {
190 case ENET_EVENT_TYPE_CONNECT: {
191 char buf[256];
192 enet_address_get_host_ip(
193 &event.peer->address, buf, sizeof buf);
194 LOG(LM_NET, LL_INFO, "client connection from %s:%u", buf,
195 event.peer->address.port);
196 }
197 break;
198 case ENET_EVENT_TYPE_RECEIVE:
199 OnReceive(n, event);
200 break;
201 case ENET_EVENT_TYPE_DISCONNECT:
202 OnDisconnect(event);
203 break;
204 default:
205 CASSERT(false, "Unknown event");
206 break;
207 }
208 }
209 } while (check > 0);
210
211 NetServerFlush(n);
212 }
213 static void PollListener(NetServer *n)
214 {
215 // Check for data to recv
216 ENetSocketSet set;
217 ENET_SOCKETSET_EMPTY(set);
218 ENET_SOCKETSET_ADD(set, n->listen);
219 if (enet_socketset_select(n->listen, &set, NULL, 0) <= 0)
220 {
221 return;
222 }
223
224 ENetAddress addr;
225 char buf;
226 ENetBuffer recvbuf;
227 recvbuf.data = &buf;
228 recvbuf.dataLength = 1;
229 const int recvlen = enet_socket_receive(n->listen, &addr, &recvbuf, 1);
230 if (recvlen <= 0)
231 {
232 return;
233 }
234 char addrbuf[256];
235 enet_address_get_host_ip(&addr, addrbuf, sizeof addrbuf);
236 LOG(LM_NET, LL_DEBUG, "listener received from %s:%u", addrbuf, addr.port);
237 // Reply to scanner client with our server host/address
238 NServerInfo sinfo;
239 sinfo.ProtocolVersion = NET_PROTOCOL_VERSION;
240 sinfo.ENetPort = n->server->address.port;
241 strcpy(sinfo.Hostname, n->hostname);
242 sinfo.GameMode = gCampaign.Entry.Mode;
243 sinfo.CampaignName[0] = '\0';
244 strncat(
245 sinfo.CampaignName, gCampaign.Setting.Title,
246 sizeof sinfo.CampaignName - 1);
247 sinfo.MissionNumber = gCampaign.MissionIndex + 1;
248 sinfo.NumPlayers = GetNumPlayers(PLAYER_ANY, false, false);
249 sinfo.MaxPlayers = NET_SERVER_MAX_CLIENTS * MAX_LOCAL_PLAYERS +
250 GetNumPlayers(PLAYER_ANY, false, true);
251 // Encode our packet
252 uint8_t encbuf[1024];
253 pb_ostream_t stream = pb_ostream_from_buffer(encbuf, sizeof encbuf);
254 const bool status = pb_encode(&stream, NServerInfo_fields, &sinfo);
255 CASSERT(status, "Failed to encode pb");
256 recvbuf.data = encbuf;
257 recvbuf.dataLength = stream.bytes_written;
258 if (enet_socket_send(n->listen, &addr, &recvbuf, 1) !=
259 (int)recvbuf.dataLength)
260 {
261 LOG(LM_NET, LL_ERROR, "Failed to reply to scanner");
262 }
263 }
264 static void OnConnect(NetServer *n, ENetEvent event);
265 static void OnReceive(NetServer *n, ENetEvent event)
266 {
267 const GameEventType msg = (GameEventType) * (uint32_t *)event.packet->data;
268 int peerId = -1;
269 if (event.peer->data != NULL)
270 {
271 // We may not have assigned peer ID
272 peerId = ((NetPeerData *)event.peer->data)->Id;
273 LOG(LM_NET, LL_TRACE, "recv message from peerId(%d) msg(%d)", peerId,
274 (int)msg);
275 }
276 const GameEventEntry gee = GameEventGetEntry(msg);
277 if (gee.Enqueue)
278 {
279 // Game event message; decode and add to event queue
280 LOG(LM_NET, LL_TRACE, "recv gameEvent(%d)", (int)gee.Type);
281 GameEvent e = GameEventNew(gee.Type);
282 NetDecode(event.packet, &e.u, gee.Fields);
283 GameEventsEnqueue(&gGameEvents, e);
284 }
285 else
286 {
287 switch (gee.Type)
288 {
289 case GAME_EVENT_CLIENT_CONNECT:
290 OnConnect(n, event);
291 break;
292 case GAME_EVENT_CLIENT_READY:
293 CASSERT(peerId >= 0, "peer id unset");
294 // Flush game events to make sure we add the players
295 HandleGameEvents(&gGameEvents, NULL, NULL, NULL, NULL);
296 // Reset player data
297 for (int i = 0; i < MAX_LOCAL_PLAYERS; i++)
298 {
299 const int cid = (peerId + 1) * MAX_LOCAL_PLAYERS + i;
300 const PlayerData *pData = PlayerDataGetByUID(cid);
301 if (pData == NULL)
302 continue;
303 GameEvent e = GameEventNew(GAME_EVENT_PLAYER_DATA);
304 e.u.PlayerData = PlayerDataMissionReset(pData);
305 GameEventsEnqueue(&gGameEvents, e);
306 }
307 // Flush game events to make sure we reset player data
308 HandleGameEvents(&gGameEvents, NULL, NULL, NULL, NULL);
309
310 // Send game start messages if we've started already
311 if (gMission.HasStarted)
312 {
313 NetServerSendGameStartMessages(n, peerId);
314
315 // Find a current player to spawn next to
316 struct vec2 defaultSpawnPosition = svec2_zero();
317 const TActor *closestActor = AIGetClosestPlayer(svec2_zero());
318 if (closestActor != NULL)
319 {
320 defaultSpawnPosition = closestActor->Pos;
321 }
322
323 // Add the client's actors
324 for (int i = 0; i < MAX_LOCAL_PLAYERS; i++)
325 {
326 const int cid = (peerId + 1) * MAX_LOCAL_PLAYERS + i;
327 const PlayerData *pData = PlayerDataGetByUID(cid);
328 if (pData != NULL)
329 {
330 defaultSpawnPosition = PlacePlayer(
331 &gMap, pData, defaultSpawnPosition, true);
332 }
333 }
334 }
335 if (gMission.HasBegun)
336 {
337 GameEvent e = GameEventNew(GAME_EVENT_GAME_BEGIN);
338 e.u.GameBegin.MissionTime = gMission.time;
339 NetServerSendMsg(
340 n, peerId, GAME_EVENT_GAME_BEGIN, &e.u.GameBegin);
341 }
342
343 NetServerFlush(n);
344 break;
345 default:
346 CASSERT(false, "unexpected message type");
347 break;
348 }
349 }
350 enet_packet_destroy(event.packet);
351 }
352 static void OnConnect(NetServer *n, ENetEvent event)
353 {
354 char buf[256];
355 enet_address_get_host_ip(&event.peer->address, buf, sizeof buf);
356 LOG(LM_NET, LL_INFO, "new client connected from %s:%u", buf,
357 event.peer->address.port);
358 /* Store any relevant client information here. */
359 CMALLOC(event.peer->data, sizeof(NetPeerData));
360 const int peerId = n->peerId;
361 ((NetPeerData *)event.peer->data)->Id = peerId;
362 n->peerId++;
363
364 // Send the client ID
365 LOG(LM_NET, LL_DEBUG, "NetServer: sending client ID %d", peerId);
366 NClientId cid;
367 cid.Id = peerId;
368 cid.FirstPlayerUID = (peerId + 1) * MAX_LOCAL_PLAYERS;
369 NetServerSendMsg(n, peerId, GAME_EVENT_CLIENT_ID, &cid);
370
371 // Send the current campaign details over
372 LOG(LM_NET, LL_DEBUG, "NetServer: sending campaign entry");
373 NCampaignDef def = NMakeCampaignDef(&gCampaign);
374 NetServerSendMsg(n, peerId, GAME_EVENT_CAMPAIGN_DEF, &def);
375
376 SoundPlay(&gSoundDevice, StrSound("menu_start"));
377 LOG(LM_NET, LL_DEBUG, "NetServer: client connection complete");
378
379 NetServerFlush(n);
380 }
381 static void OnDisconnect(const ENetEvent event)
382 {
383 int peerId = -1;
384 if (event.peer->data != NULL)
385 {
386 peerId = ((NetPeerData *)event.peer->data)->Id;
387 CFREE(event.peer->data);
388 event.peer->data = NULL;
389 }
390 CASSERT(peerId >= 0, "Cannot find disconnected peer id");
391 char buf[256];
392 enet_address_get_host_ip(&event.peer->address, buf, sizeof buf);
393 LOG(LM_NET, LL_INFO, "peerId(%d) disconnected %s:%u", peerId, buf,
394 event.peer->address.port);
395 // Remove client's players
396 for (int i = 0; i < MAX_LOCAL_PLAYERS; i++)
397 {
398 GameEvent e = GameEventNew(GAME_EVENT_PLAYER_REMOVE);
399 e.u.PlayerRemove.UID = (peerId + 1) * MAX_LOCAL_PLAYERS + i;
400 GameEventsEnqueue(&gGameEvents, e);
401 }
402 }
403
404 void NetServerFlush(NetServer *n)
405 {
406 if (n->server == NULL)
407 return;
408 enet_host_flush(n->server);
409 }
410
411 static void SendConfig(
412 Config *config, const char *name, NetServer *n, const int peerId);
413 void NetServerSendGameStartMessages(NetServer *n, const int peerId)
414 {
415 if (!n->server)
416 return;
417 GameEvent e;
418 // Send details of all current players
419 CA_FOREACH(const PlayerData, pOther, gPlayerDatas)
420 NPlayerData pd = NMakePlayerData(pOther);
421 NetServerSendMsg(n, peerId, GAME_EVENT_PLAYER_DATA, &pd);
422 LOG(LM_NET, LL_DEBUG, "send player data uid(%d) maxHealth(%d) excessHealth(%d) HP(%d)",
423 (int)pd.UID, (int)pd.MaxHealth, (int)pd.ExcessHealth, (int)pd.HP);
424 CA_FOREACH_END()
425
426 // Send all game-specific config values
427 SendConfig(&gConfig, "Game.FriendlyFire", n, peerId);
428 SendConfig(&gConfig, "Game.FPS", n, peerId);
429 SendConfig(&gConfig, "Game.Fog", n, peerId);
430 SendConfig(&gConfig, "Game.SightRange", n, peerId);
431 SendConfig(&gConfig, "Game.AllyCollision", n, peerId);
432
433 NetServerSendMsg(n, peerId, GAME_EVENT_NET_GAME_START, NULL);
434
435 // Send all actors
436 CA_FOREACH(const TActor, a, gActors)
437 if (!a->isInUse)
438 {
439 continue;
440 }
441 e = GameEventNew(GAME_EVENT_ACTOR_ADD);
442 e.u.ActorAdd.UID = a->uid;
443 e.u.ActorAdd.PilotUID = a->pilotUID;
444 e.u.ActorAdd.VehicleUID = a->vehicleUID;
445 e.u.ActorAdd.CharId = a->charId;
446 e.u.ActorAdd.Health = a->health;
447 e.u.ActorAdd.Direction = (int32_t)a->direction;
448 e.u.ActorAdd.PlayerUID = a->PlayerUID;
449 e.u.ActorAdd.ThingFlags = a->thing.flags;
450 e.u.ActorAdd.Pos = Vec2ToNet(a->Pos);
451 LOG(LM_NET, LL_DEBUG, "send add actor UID(%d) playerUID(%d)",
452 (int)e.u.ActorAdd.UID, (int)e.u.ActorAdd.PlayerUID);
453 NetServerSendMsg(n, peerId, GAME_EVENT_ACTOR_ADD, &e.u.ActorAdd);
454 CA_FOREACH_END()
455
456 // Send key state
457 e = GameEventNew(GAME_EVENT_ADD_KEYS);
458 e.u.AddKeys.KeyFlags = gMission.KeyFlags;
459 NetServerSendMsg(n, peerId, GAME_EVENT_ADD_KEYS, &e.u.AddKeys);
460
461 // Send objective counts
462 CA_FOREACH(const Objective, o, gMission.missionData->Objectives)
463 e = GameEventNew(GAME_EVENT_OBJECTIVE_UPDATE);
464 e.u.ObjectiveUpdate.ObjectiveId = _ca_index;
465 e.u.ObjectiveUpdate.Count = o->done;
466 NetServerSendMsg(
467 n, peerId, GAME_EVENT_OBJECTIVE_UPDATE, &e.u.ObjectiveUpdate);
468 CA_FOREACH_END()
469
470 // Send all tiles, RLE
471 const Tile *tLast = NULL;
472 e = GameEventNew(GAME_EVENT_TILE_SET);
473 struct vec2i pos;
474 for (pos.y = 0; pos.y < gMap.Size.y; pos.y++)
475 {
476 for (pos.x = 0; pos.x < gMap.Size.x; pos.x++)
477 {
478 const Tile *t = MapGetTile(&gMap, pos);
479 // Use RLE, so check if the current tile is the same as the last
480 if (tLast != NULL && t->Class == tLast->Class)
481 {
482 e.u.TileSet.RunLength++;
483 }
484 else
485 {
486 // Send the last run
487 if (tLast != NULL)
488 {
489 NetServerSendMsg(
490 n, peerId, GAME_EVENT_TILE_SET, &e.u.TileSet);
491 }
492 // Begin the next run
493 e = GameEventNew(GAME_EVENT_TILE_SET);
494 e.u.TileSet.Pos = Vec2i2Net(pos);
495 if (t->Class != NULL)
496 {
497 TileClassGetName(
498 e.u.TileSet.ClassName, t->Class, t->Class->Style,
499 t->Class->StyleType, t->Class->Mask,
500 t->Class->MaskAlt);
501 }
502 if (t->Door.Class != NULL)
503 {
504 TileClassGetName(
505 e.u.TileSet.DoorClassName, t->Door.Class,
506 t->Door.Class->Style, t->Door.Class->StyleType,
507 t->Door.Class->Mask, t->Door.Class->MaskAlt);
508 }
509 if (t->Door.Class2 != NULL)
510 {
511 TileClassGetName(
512 e.u.TileSet.DoorClass2Name, t->Door.Class2,
513 t->Door.Class2->Style, t->Door.Class2->StyleType,
514 t->Door.Class2->Mask, t->Door.Class2->MaskAlt);
515 }
516 e.u.TileSet.RunLength = 0;
517 }
518 tLast = t;
519 }
520 }
521 NetServerSendMsg(n, peerId, GAME_EVENT_TILE_SET, &e.u.TileSet);
522
523 // Send all the tiles visited so far
524 e = GameEventNew(GAME_EVENT_EXPLORE_TILES);
525 bool run = false;
526 for (pos.y = 0; pos.y < gMap.Size.y; pos.y++)
527 {
528 for (pos.x = 0; pos.x < gMap.Size.x; pos.x++)
529 {
530 const Tile *t = MapGetTile(&gMap, pos);
531 if (LOSAddRun(&e.u.ExploreTiles, &run, pos, t->isVisited))
532 {
533 NetServerSendMsg(
534 n, peerId, GAME_EVENT_EXPLORE_TILES, &e.u.ExploreTiles);
535 e = GameEventNew(GAME_EVENT_EXPLORE_TILES);
536 run = false;
537 }
538 }
539 }
540 if (e.u.ExploreTiles.Runs_count > 0)
541 {
542 NetServerSendMsg(
543 n, peerId, GAME_EVENT_EXPLORE_TILES, &e.u.ExploreTiles);
544 }
545
546 // Send all pickups
547 CA_FOREACH(const Pickup, p, gPickups)
548 if (!p->isInUse)
549 continue;
550 e = GameEventNew(GAME_EVENT_ADD_PICKUP);
551 e.u.AddPickup.UID = p->UID;
552 strcpy(e.u.AddPickup.PickupClass, p->class->Name);
553 e.u.AddPickup.IsRandomSpawned = p->IsRandomSpawned;
554 e.u.AddPickup.SpawnerUID = p->SpawnerUID;
555 e.u.AddPickup.ThingFlags = p->thing.flags;
556 e.u.AddPickup.Pos = Vec2ToNet(p->thing.Pos);
557 NetServerSendMsg(n, peerId, GAME_EVENT_ADD_PICKUP, &e.u.AddPickup);
558 CA_FOREACH_END()
559
560 // Send all map objects
561 CA_FOREACH(const TObject, o, gObjs)
562 if (!o->isInUse)
563 continue;
564 e = GameEventNew(GAME_EVENT_MAP_OBJECT_ADD);
565 e.u.MapObjectAdd.UID = o->uid;
566 strcpy(e.u.MapObjectAdd.MapObjectClass, o->Class->Name);
567 e.u.MapObjectAdd.Mask = Color2Net(o->thing.CPic.Mask);
568 e.u.MapObjectAdd.Pos = Vec2ToNet(o->thing.Pos);
569 e.u.MapObjectAdd.ThingFlags = o->thing.flags;
570 e.u.MapObjectAdd.Health = o->Health;
571 LOG(LM_NET, LL_DEBUG,
572 "send add map object UID(%d) pos(%d, %d) flags(%x) health(%d)",
573 (int)e.u.MapObjectAdd.UID, e.u.MapObjectAdd.Pos.x,
574 e.u.MapObjectAdd.Pos.y, e.u.MapObjectAdd.ThingFlags,
575 e.u.MapObjectAdd.Health);
576 NetServerSendMsg(n, peerId, GAME_EVENT_MAP_OBJECT_ADD, &e.u.MapObjectAdd);
577 CA_FOREACH_END()
578
579 // If mission complete already, send message
580 if (CanCompleteMission(&gMission))
581 {
582 NMissionComplete mc = NMakeMissionComplete(&gMission);
583 NetServerSendMsg(n, peerId, GAME_EVENT_MISSION_COMPLETE, &mc);
584 }
585 }
586 static void SendConfig(
587 Config *config, const char *name, NetServer *n, const int peerId)
588 {
589 GameEvent e = GameEventNew(GAME_EVENT_CONFIG);
590 const Config *c = ConfigGet(config, name);
591 strcpy(e.u.Config.Name, name);
592 switch (c->Type)
593 {
594 case CONFIG_TYPE_STRING:
595 CASSERT(false, "unimplemented");
596 break;
597 case CONFIG_TYPE_INT:
598 sprintf(e.u.Config.Value, "%d", c->u.Int.Value);
599 break;
600 case CONFIG_TYPE_FLOAT:
601 sprintf(e.u.Config.Value, "%f", c->u.Float.Value);
602 break;
603 case CONFIG_TYPE_BOOL:
604 strcpy(e.u.Config.Value, c->u.Bool.Value ? "true" : "false");
605 break;
606 case CONFIG_TYPE_ENUM:
607 sprintf(e.u.Config.Value, "%d", (int)c->u.Enum.Value);
608 break;
609 case CONFIG_TYPE_GROUP:
610 CASSERT(false, "Cannot send groups over net");
611 break;
612 default:
613 CASSERT(false, "Unknown config type");
614 break;
615 }
616 NetServerSendMsg(n, peerId, GAME_EVENT_CONFIG, &e.u.Config);
617 }
618
619 void NetServerSendMsg(
620 NetServer *n, const int peerId, const GameEventType e, const void *data)
621 {
622 if (!n->server)
623 return;
624
625 if (peerId >= 0)
626 {
627 LOG(LM_NET, LL_TRACE, "send msg(%d) to peers(%d)", (int)e,
628 (int)n->server->connectedPeers);
629 // Find the peer and send
630 for (int i = 0; i < (int)n->server->peerCount; i++)
631 {
632 ENetPeer *peer = n->server->peers + i;
633 if (peer->data != NULL &&
634 ((NetPeerData *)peer->data)->Id == peerId)
635 {
636 enet_peer_send(peer, 0, NetEncode(e, data));
637 return;
638 }
639 }
640 CASSERT(false, "Cannot find peer by id");
641 }
642 else
643 {
644 LOG(LM_NET, LL_TRACE, "bcast msg(%d) to peers(%d)", (int)e,
645 (int)n->server->connectedPeers);
646 enet_host_broadcast(n->server, 0, NetEncode(e, data));
647 }
648 }