-
Notifications
You must be signed in to change notification settings - Fork 3
/
TAServerClient.cpp
202 lines (165 loc) · 6.69 KB
/
TAServerClient.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
#include "TAServerClient.h"
TAServer::Client g_TAServerClient;
namespace TAServer {
bool Client::connect(std::string host, int port) {
tcpClient = std::make_shared<TCP::Client<short> >(ios);
// Run handlers for reading etc. in a new thread
iosThread = std::make_shared<std::thread>([&, host, port] {
boost::asio::io_service::work work(ios);
attachHandlers();
tcpClient->start(host, port, std::bind(&Client::handler_OnConnect, this));
ios.run();
});
// Start the thread used to poll game information to send
{
std::unique_lock<std::mutex> lock(pollingThreadMutex);
killPollingThreadFlag = false;
}
gameInfoPollingThread = std::make_shared<std::thread>([&]() {
this->performGamePolling();
});
return true;
}
bool Client::disconnect() {
tcpClient->stop();
ios.stop();
iosThread->join();
killPollingThread();
return true;
}
bool Client::isConnected() {
return !tcpClient->is_stopped();
}
void Client::killPollingThread() {
{
std::unique_lock<std::mutex> lock(pollingThreadMutex);
killPollingThreadFlag = true;
}
pollingThreadCV.notify_all();
if (gameInfoPollingThread->joinable()) {
gameInfoPollingThread->join();
}
}
void Client::performGamePolling() {
while (true) {
pollForGameInfoChanges();
bool shouldDie = false;
{
std::unique_lock<std::mutex> lock(pollingThreadMutex);
shouldDie = pollingThreadCV.wait_for(lock, std::chrono::seconds(2), [&] {return killPollingThreadFlag; });
}
if (shouldDie) {
break;
}
std::this_thread::sleep_for(std::chrono::seconds(2));
}
}
void Client::attachHandlers() {
tcpClient->add_handler(TASRV_MSG_KIND_LAUNCHER_2_GAME_LOADOUT_MESSAGE, [this](const json& j) {
handler_Launcher2GameLoadoutMessage(j);
});
tcpClient->add_handler(TASRV_MSG_KIND_LAUNCHER_2_GAME_NEXT_MAP_MESSAGE, [this](const json& j) {
handler_Launcher2GameNextMapMessage(j);
});
tcpClient->add_handler(TASRV_MSG_KIND_LAUNCHER_2_GAME_PINGS_MESSAGE, [this](const json& j) {
handler_Launcher2GamePingsMessage(j);
});
tcpClient->add_handler(TASRV_MSG_KIND_LAUNCHER_2_GAME_INIT_MESSAGE, [this](const json& j) {
handler_Launcher2GameInitMessage(j);
});
tcpClient->add_handler(TASRV_MSG_KIND_LAUNCHER_2_GAME_PLAYER_INFO, [this](const json& j) {
handler_Launcher2GamePlayerInfoMessage(j);
});
}
void Client::sendProtocolVersion() {
Game2LauncherProtocolVersionMessage msg;
json j;
msg.toJson(j);
Logger::debug("SendProtocolVersion json: %s", j.dump().c_str());
tcpClient->send(msg.getMessageKind(), j);
}
bool Client::retrieveLoadout(FUniqueNetId uniquePlayerId, int classId, int slot, std::map<int, int>& resultEquipMap) {
Game2LauncherLoadoutRequest req;
req.uniquePlayerId = uniquePlayerId;
req.classId = classId;
req.slot = slot;
json j;
req.toJson(j);
tcpClient->send(req.getMessageKind(), j);
// Block until a loadout is received or a timeout of 5 seconds occurs
auto startTime = std::chrono::system_clock::now();
auto& it = receivedLoadouts.find(Utils::netIdToLong(uniquePlayerId));
while (it == receivedLoadouts.end()) {
if (std::chrono::system_clock::now() > startTime + std::chrono::seconds(5)) return false;
std::this_thread::sleep_for(std::chrono::milliseconds(100));
it = receivedLoadouts.find(Utils::netIdToLong(uniquePlayerId));
}
Launcher2GameLoadoutMessage response = it->second;
// Delete the entry
{
std::lock_guard<std::mutex> lock(receivedLoadoutsMutex);
receivedLoadouts.erase(Utils::netIdToLong(uniquePlayerId));
}
response.toEquipPointMap(resultEquipMap);
return true;
}
void Client::sendServerInfo(std::string description, std::string motd, std::vector<unsigned char> password_hash, std::string game_setting_mode) {
Game2LauncherServerInfoMessage msg;
msg.description = description;
msg.motd = motd;
msg.password_hash = password_hash;
msg.game_setting_mode = game_setting_mode;
json j;
msg.toJson(j);
tcpClient->send(msg.getMessageKind(), j);
}
void Client::sendTeamInfo(const std::map<long long, int>& playerToTeamId) {
Game2LauncherTeamInfoMessage msg;
for (auto& elem : playerToTeamId) {
msg.playerToTeamId[elem.first] = elem.second;
}
json j;
msg.toJson(j);
Logger::debug("SendTeamInfo json: %s", j.dump().c_str());
tcpClient->send(msg.getMessageKind(), j);
}
void Client::sendScoreInfo(int beScore, int dsScore) {
Game2LauncherScoreInfoMessage msg;
msg.beScore = beScore;
msg.dsScore = dsScore;
json j;
msg.toJson(j);
Logger::debug("SendScoreInfo json: %s", j.dump().c_str());
tcpClient->send(msg.getMessageKind(), j);
}
void Client::sendMapInfo(int mapId) {
Game2LauncherMapInfoMessage msg;
msg.mapId = mapId;
json j;
msg.toJson(j);
Logger::debug("SendMapInfo json: %s", j.dump().c_str());
tcpClient->send(msg.getMessageKind(), j);
}
void Client::sendMatchTime(long long matchSecondsLeft, bool counting) {
Game2LauncherMatchTimeMessage msg;
msg.secondsRemaining = matchSecondsLeft;
msg.counting = counting;
json j;
msg.toJson(j);
Logger::debug("SendMatchTime json: %s", j.dump().c_str());
tcpClient->send(msg.getMessageKind(), j);
}
void Client::sendMatchEnded(int nextMatchIdx, const std::vector<std::string> &votableMaps, std::string nextMapOverride, int waitTime, std::map<long long, PlayerTimePlayedRecord> playerTimePlayed) {
Game2LauncherMatchEndMessage msg;
msg.controllerContext.hasContext = true;
msg.controllerContext.nextMapIndex = nextMatchIdx;
msg.controllerContext.nextMapOverride = nextMapOverride;
msg.votableMaps = votableMaps;
msg.playersTimePlayed = playerTimePlayed;
msg.nextMapWaitTime = waitTime;
json j = json::object();
msg.toJson(j);
Logger::debug("SendMatchEnd json: %s", j.dump().c_str());
tcpClient->send(msg.getMessageKind(), j);
}
}