forked from openairlinetycoon/ATDMasterServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebView.cpp
137 lines (95 loc) · 3.33 KB
/
WebView.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
#include "stdafx.h"
#include "WebView.h"
#include <format>
#include <thread>
#include "ServerBrowser.h"
#include "NATServer.h"
#include "libs/mongoose/mongoose.h"
using namespace RakNet;
mg_mgr mgr;
void fn(mg_connection* c, int ev, void* ev_data, void* fn_data) {
WebView* viewer = static_cast<WebView*>(fn_data);
viewer->ServerMessageHandler(c, ev, ev_data);
}
void WebView::StartServer() {
V_LOG("- Starting WebView Server...\n");
mg_mgr_init(&mgr);
mg_http_listen(&mgr, "http://0.0.0.0:8000", fn, this);
}
void WebView::ProcessMessage() {
mg_mgr_poll(&mgr, 100);
}
bool WebView::GetNATUserCount(mg_connection* c, mg_http_message* hm) const {
if (!mg_http_match_uri(hm, "/api/v1/nat/users")) {
return false;
}
mg_http_reply(c, 200, "Content-Type: application/json\r\n", "{count:%d}\n", this->m_natServer->GetConnectedUser());
return true;
}
std::string TransformUsersToJson(DataStructures::List<RoomMember*> users) {
std::string userJson{};
for (unsigned int j = 0; j < users.Size(); j++) {
const RoomsParticipant* user = users[j]->roomsParticipant;
userJson = userJson.append(std::format("{{name:\"{0}\", guid:\"{1}\"}}\n", user->GetName().C_String(), user->GetGUID().ToString()));
}
return userJson;
}
bool WebView::GetBrowserUserCount(mg_connection* c, mg_http_message* hm) const {
if (!mg_http_match_uri(hm, "/api/v1/browser/users")) {
return false;
}
auto users = this->m_serverBrowser->GetUsers();
const std::string userJson = TransformUsersToJson(users);
mg_http_reply(c, 200, "Content-Type: application/json\r\n", "[%s]\n", userJson.c_str());
return true;
}
bool WebView::GetBrowserRooms(mg_connection* c, mg_http_message* hm) const {
if (!mg_http_match_uri(hm, "/api/v1/browser/rooms")) {
return false;
}
std::string json;
json = json.append("");
auto rooms = this->m_serverBrowser->GetRooms();
for (unsigned int i = 0; i < rooms.Size(); i++) {
Room* room = rooms[i];
const char* name = room->GetStringProperty(DefaultRoomColumns::TC_ROOM_NAME);
DataStructures::List<RoomMember*> users = room->roomMemberList;
const std::string userJson = TransformUsersToJson(users);
json.append(std::format("{{name:\"{0}\",user:[{1}]}}", name, userJson.c_str()));
}
mg_http_reply(c, 200, "Content-Type: application/json\r\n", "[%s]\n", json.c_str());
return true;
}
bool WebView::GetRoomCreationCount(mg_connection* c, mg_http_message* hm) const {
if (!mg_http_match_uri(hm, "/api/v1/browser/history/rooms")) {
return false;
}
mg_http_reply(c, 200, "Content-Type: application/json\r\n", "{count:%d}\n", this->m_serverBrowser->GetRoomCreationCount());
return true;
}
bool WebView::GetUserLoginCount(mg_connection* c, mg_http_message* hm) {
if (!mg_http_match_uri(hm, "/api/v1/browser/history/users")) {
return false;
}
mg_http_reply(c, 200, "Content-Type: application/json\r\n", "{count:%d}\n", this->m_serverBrowser->GetUserLoginCount());
return true;
}
void WebView::ServerMessageHandler(mg_connection* c, int ev, void* ev_data) {
if (ev == MG_EV_HTTP_MSG) {
mg_http_message* hm = static_cast<mg_http_message*>(ev_data);
//NAT:
if (GetNATUserCount(c, hm))
return;
//Browser:
if (GetBrowserUserCount(c, hm))
return;
if (GetBrowserRooms(c, hm))
return;
//History:
if (GetRoomCreationCount(c, hm))
return;
if (GetUserLoginCount(c, hm))
return;
mg_http_reply(c, 404, "", "");
}
}