-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathRconClient.cpp
More file actions
156 lines (129 loc) · 3.57 KB
/
Copy pathRconClient.cpp
File metadata and controls
156 lines (129 loc) · 3.57 KB
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
#include "RconClient.h"
#include <utility>
RconClient::RconClient(SOCKET & socket, std::function<void(RconClient*c)> disconnectCB)
{
this->socket = socket;
this->disconnectCB = std::move(disconnectCB);
}
RconClient::~RconClient() { }
void RconClient::stop()
{
connected = false;
}
void RconClient::start()
{
workThread = std::make_shared<std::thread>(&RconClient::handleConnection, this);
workThread->detach();
}
void RconClient::onChatInput(std::string const & msg)
{
std::vector<std::string> rows = std::vector<std::string>();
rows.push_back(msg);
send(rows);
}
bool RconClient::checkLogin()
{
char pwd[33], magic, res;
pwd[32] = 0x00;
if (recv(socket, pwd, 32, 0) != 32) return false;
if (recv(socket, &magic, 1, 0) != 1) return false;
if (magic != 0x64) return false;
std::string pwdHash = md5(bf2server_get_adminpwd());
if (pwdHash == pwd) {
Logger.log(LogLevel_VERBOSE, "Client logged in.", pwd);
res = 1;
}
else {
Logger.log(LogLevel_VERBOSE, "Client sent wrong password '%s'", pwd);
res = 0;
}
::send(socket, &res, 1, 0);
return (res == 1);
}
void RconClient::handleCommand(std::string const & command)
{
std::string res;
if (bf2server_idle() && bf2server_get_map_status() == MAP_IDLE) {
if(!dispatchInternal(command, res)) {
res = bf2server_command(MESSAGETYPE_COMMAND, SENDER_REMOTE, bf2server_s2ws(command).c_str(), OUTPUT_BUFFER);
Logger.log(LogLevel_VERBOSE, "Executed command '%s', result: '%s'", command.c_str(), res.c_str());
}
}
else {
Logger.log(LogLevel_VERBOSE, "Server is busy - telling the client...'");
res = RETURN_BUSY;
}
auto rows = std::vector<std::string>();
size_t op = 0;
size_t np = 0;
while ((np = res.find('\n', np)) != std::string::npos) {
std::string r = res.substr(op, np - op);
rows.emplace_back(r);
op = ++np;
}
send(rows);
}
void RconClient::send(std::vector<std::string> &response)
{
unsigned char rowLen = 0;
auto rows = static_cast<unsigned char>(response.size());
{
std::unique_lock<std::mutex> lg(mtx);
::send(socket, reinterpret_cast<char*>(&rows), 1, 0);
for (std::string &row : response) {
rowLen = static_cast<unsigned char>(row.length() + 1);
::send(socket, reinterpret_cast<char*>(&rowLen), 1, 0);
::send(socket, row.c_str(), rowLen, 0);
}
}
}
void RconClient::handleConnection()
{
unsigned char rows, sz, bytesRead, fragment;
bool err = false;
if (!(connected = checkLogin())) {
Logger.log(LogLevel_VERBOSE, "Client login failed.");
}
while (connected) {
bytesRead = 0;
if (recv(socket, (char*)&rows, 1, 0) != 1) break;
if (recv(socket, (char*)&sz, 1, 0) != 1) break;
auto buffer = std::make_unique<char[]>(sz);
while (sz > bytesRead) {
if ((fragment = recv(socket, buffer.get() + bytesRead, sz - bytesRead, 0)) == SOCKET_ERROR) {
err = true;
connected = false;
break;
}
buffer.get()[sz - 1] = 0;
bytesRead += static_cast<char>(fragment);
}
if (!err) {
Logger.log(LogLevel_VERBOSE, "Received command: %s", buffer.get());
handleCommand(std::string(buffer.get()));
}
}
Logger.log(LogLevel_VERBOSE, "Closing connection.");
closesocket(socket);
disconnectCB(this);
}
bool RconClient::dispatchInternal(std::string const & command, std::string &res)
{
if (command.rfind(COMMAND_LUA, 0) == 0) {
auto ll = strlen(COMMAND_LUA);
if(command.size() > ll) {
auto lr = bf2server_lua_dostring(command.substr(ll));
res = RETURN_OK;
}
else {
res = RETURN_EPARAM;
}
return true;
}
return false;
}
void RconClient::reportEndgame() {
auto v = std::vector<std::string>();
v.emplace_back("Game has ended");
send(v);
}