-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Ping.c
47 lines (40 loc) · 1.93 KB
/
Ping.c
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
#include "Server.h"
#include <enet/enet.h>
#include <json-c/json.h>
#include <json-c/json_object.h>
#include <stdio.h>
#include <string.h>
int rawUdpInterceptCallback(ENetHost* host, ENetEvent* event)
{
(void) event;
if (!strncmp((char*) host->receivedData, "HELLO", host->receivedDataLength)) {
enet_socket_send(host->socket, &host->receivedAddress, &(ENetBuffer){.data = "HI", .dataLength = 2}, 1);
return 1;
} else if (!strncmp((char*) host->receivedData, "HELLOLAN", host->receivedDataLength)) {
Server* server = getServer();
if (strlen(server->mapName) == 0) {
return 1;
}
json_object * jobj = json_object_new_object();
json_object *serverNameObject = json_object_new_string(server->serverName);
json_object *mapNameObject = json_object_new_string(server->mapName);
json_object *gamemodeObject = json_object_new_string(server->gamemodeName);
json_object *protocolVersionObject = json_object_new_string("0.75");
json_object *currentPlayersObject = json_object_new_int(server->protocol.countOfUsers);
json_object *maxPlayersObject = json_object_new_int(server->protocol.maxPlayers);
json_object_object_add(jobj,"name", serverNameObject);
json_object_object_add(jobj,"players_current", currentPlayersObject);
json_object_object_add(jobj,"players_max", maxPlayersObject);
json_object_object_add(jobj,"map", mapNameObject);
json_object_object_add(jobj,"game_mode", gamemodeObject);
json_object_object_add(jobj,"game_version", protocolVersionObject);
const char *json = json_object_to_json_string(jobj);
enet_socket_send(
host->socket, &host->receivedAddress, &(ENetBuffer){.data = (char*)json, .dataLength = strlen(json)}, 1);
while (json_object_put(jobj) != 1) {
// Free the memory from the JSON object
}
return 1;
}
return 0;
}