This repository was archived by the owner on Sep 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTSourceEngineQuery.cpp
More file actions
290 lines (261 loc) · 13.6 KB
/
Copy pathTSourceEngineQuery.cpp
File metadata and controls
290 lines (261 loc) · 13.6 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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include <iostream>
#include <memory>
#include <boost/asio.hpp>
#include <future>
#include "TSourceEngineQuery.h"
#include "GlobalContext.h"
#include "parsemsg.h"
using namespace std::chrono_literals;
using boost::asio::ip::udp;
struct TSourceEngineQuery::impl_t {
std::shared_ptr<boost::asio::io_context> ioc = GlobalContextSingleton();
};
TSourceEngineQuery::TSourceEngineQuery() : pimpl(std::make_shared<impl_t>())
{
}
TSourceEngineQuery::~TSourceEngineQuery()
{
}
std::string UTF8_To_ANSI(const std::string &str)
{
return str;
}
auto TSourceEngineQuery::MakeServerInfoQueryResultFromBuffer(const char *reply, std::size_t reply_length, std::string address, uint16_t port) -> ServerInfoQueryResult
{
BufferReader buf(reply, reply_length);
ServerInfoQueryResult result{};
result.header1 = buf.ReadLong(); // header -1
result.header2 = buf.ReadByte(); // header ('I')
if (result.header1 != -1)
throw std::runtime_error("错误的返回数据头部(-1)");
if (result.header2 == 'I')
{
// Steam版
result.Protocol = buf.ReadByte();
result.ServerName = UTF8_To_ANSI(buf.ReadString());
result.Map = UTF8_To_ANSI(buf.ReadString());
result.Folder = UTF8_To_ANSI(buf.ReadString());
result.Game = UTF8_To_ANSI(buf.ReadString());
result.SteamID = buf.ReadShort();
result.PlayerCount = buf.ReadByte();
result.MaxPlayers = buf.ReadByte();
result.BotCount = buf.ReadByte();
result.ServerType = static_cast<ServerType_e>(buf.ReadByte());
result.Environment = static_cast<Environment_e>(buf.ReadByte());
result.Visibility = static_cast<Visibility_e>(buf.ReadByte());
result.VAC = buf.ReadByte();
result.GameVersion = UTF8_To_ANSI(buf.ReadString());
int EDF = buf.ReadByte();
if (EDF & 0x80)
result.Port = buf.ReadShort();
if (EDF & 0x10)
result.SteamIDExtended = { buf.ReadLong(), buf.ReadLong() };
if (EDF & 0x40)
result.SourceTVData = { buf.ReadShort(), UTF8_To_ANSI(buf.ReadString()) };
if (EDF & 0x20)
result.Keywords = UTF8_To_ANSI(buf.ReadString());
if (EDF & 0x01)
result.GameID = { buf.ReadLong(), buf.ReadLong() };
}
else if (result.header2 == 'm')
{
// 非Steam版
result.LocalAddress = UTF8_To_ANSI(buf.ReadString());
result.ServerName = UTF8_To_ANSI(buf.ReadString());
result.Map = UTF8_To_ANSI(buf.ReadString());
result.Folder = UTF8_To_ANSI(buf.ReadString());
result.Game = UTF8_To_ANSI(buf.ReadString());
result.PlayerCount = buf.ReadByte();
result.MaxPlayers = buf.ReadByte();
result.Protocol = buf.ReadByte();
result.ServerType = static_cast<ServerType_e>(buf.ReadByte());
result.Environment = static_cast<Environment_e>(buf.ReadByte());
result.Visibility = static_cast<Visibility_e>(buf.ReadByte());
if ((result.Mod = buf.ReadByte()) == true)
{
result.ModData = {
UTF8_To_ANSI(buf.ReadString()),
UTF8_To_ANSI(buf.ReadString()),
buf.ReadByte(),
buf.ReadLong(),
buf.ReadLong(),
static_cast<ServerInfoQueryResult::ModData_s::ModType_e>(buf.ReadByte()),
static_cast<bool>(buf.ReadByte())
};
}
result.VAC = buf.ReadByte();
result.BotCount = buf.ReadByte();
}
else
{
throw std::runtime_error("不支持的服务器信息协议格式");
}
result.FromAddress = std::move(address);
result.FromPort = port;
return result;
}
auto TSourceEngineQuery::MakePlayerListQueryResultFromBuffer(const char *reply, std::size_t reply_length, std::string address, uint16_t port) -> PlayerListQueryResult
{
PlayerListQueryResult result;
BufferReader buf(reply, reply_length);
result.header1 = buf.ReadLong();
if (result.header1 != -1)
throw std::runtime_error("错误的返回数据头部(-1)");
result.header2 = buf.ReadByte();
if (result.header2 == 'A')
{
result.Results.emplace<0>(buf.ReadLong());
}
else if (result.header2 == 'D')
{
volatile auto Players = buf.ReadByte();
std::vector<PlayerListQueryResult::PlayerInfo_s> infos;
while (!buf.Eof())
{
auto Index = buf.ReadByte();
auto Name = UTF8_To_ANSI(buf.ReadString());
auto Score = buf.ReadLong();
float Duration = buf.ReadFloat();
// 不能换成emplace_back因为要求大括号里面求值顺序从左到右
infos.push_back({ Index, std::move(Name), Score, Duration });
}
result.Results.emplace<1>(std::move(infos));
}
else
{
throw std::runtime_error("不支持的玩家列表协议格式");
}
return result;
}
template<class T>
void try_set_exception(std::promise<T>& pro, std::exception_ptr exc)
{
try {
pro.set_exception(exc);
}
catch (std::future_error&) {
// ...
}
}
template<class Ret, class Handler, class NewRet = typename std::invoke_result<Handler, Ret>::type>
std::future<NewRet> then(boost::asio::io_context & ioc, std::future<Ret> &fut, Handler fn)
{
struct Context {
std::future<Ret> fut;
std::promise<NewRet> pro;
};
std::shared_ptr<Context> con = std::make_shared<Context>();
con->fut = std::move(fut);
std::future<NewRet> ret = con->pro.get_future();
ioc.dispatch([fn, con]{
try {
if constexpr (std::is_void_v<NewRet>)
con->pro.set_value();
else
con->pro.set_value(fn(con->fut.get()));
}
catch (...) {
con->pro.set_exception(std::current_exception());
}
});
return ret;
}
// Reference: https://developer.valvesoftware.com/wiki/Server_queries#A2S_INFO
auto TSourceEngineQuery::GetServerInfoDataAsync(const char *host, const char *port, std::chrono::seconds timeout) -> std::future<ServerInfoQueryResult>
{
// 发送查询包
std::shared_ptr<std::promise<ServerInfoQueryResult>> pro = std::make_shared<std::promise<ServerInfoQueryResult>>();
std::shared_ptr<boost::asio::io_context> ioc = pimpl->ioc;
std::shared_ptr<udp::resolver> resolver = std::make_shared<udp::resolver>(*ioc);
resolver->async_resolve(udp::v4(), host, port, [resolver, ioc, pro, timeout](boost::system::error_code ec, udp::resolver::results_type endpoints) {
if(ec)
return try_set_exception(*pro, std::make_exception_ptr(boost::system::system_error(ec, "解析域名时发生错误"))), void();
std::shared_ptr<udp::socket> socket = std::make_shared<udp::socket>(*ioc, udp::endpoint(udp::v4(), 0));
for(auto &&endpoint : endpoints)
{
static constexpr char request1[] = "\xFF\xFF\xFF\xFF" "TSource Engine Query"; // Source / GoldSrc Steam
//static constexpr char request2[] = "\xFF\xFF\xFF\xFF" "details"; // GoldSrc WON
//static constexpr char request3[] = "\xFF\xFF\xFF\xFF" "info"; // Xash3D
socket->async_send_to(boost::asio::buffer(request1, sizeof(request1)), endpoint, [socket, endpoint, pro](boost::system::error_code ec, std::size_t bytes_transferred) {
if(ec)
return try_set_exception(*pro, std::make_exception_ptr(boost::system::system_error(ec, "发送服务器信息查询包时发生错误"))), void();
std::shared_ptr<char> buffer(new char[8192], std::default_delete<char[]>());
std::shared_ptr<udp::endpoint> sender_endpoint = std::make_shared<udp::endpoint>(udp::v4(), 0);
socket->async_receive_from(boost::asio::buffer(buffer.get(), 8192), *sender_endpoint, [socket, buffer, sender_endpoint, pro](boost::system::error_code ec, std::size_t reply_length) {
if (ec)
return try_set_exception(*pro, std::make_exception_ptr(boost::system::system_error(ec, "接收服务器信息查询包时发生错误"))), void();
try{
return pro->set_value(MakeServerInfoQueryResultFromBuffer(buffer.get(), reply_length, sender_endpoint->address().to_string(), sender_endpoint->port())), void();
} catch(...) {
return try_set_exception(*pro, std::current_exception()), void();
}
});
});
}
std::shared_ptr<boost::asio::system_timer> ddl = std::make_shared<boost::asio::system_timer>(*ioc);
ddl->expires_from_now(timeout);
ddl->async_wait([ioc, socket, pro, ddl](boost::system::error_code ec){
socket->close(ec);
return try_set_exception(*pro, std::make_exception_ptr(boost::system::system_error(boost::asio::error::make_error_code(boost::asio::error::timed_out), "查询服务器超时,可能是服务器挂了或者IP不正确。"))), void();
});
});
return pro->get_future();
}
auto TSourceEngineQuery::GetPlayerListDataAsync(const char *host, const char *port, std::chrono::seconds timeout) -> std::future<PlayerListQueryResult>
{
std::shared_ptr<std::promise<PlayerListQueryResult>> pro = std::make_shared<std::promise<PlayerListQueryResult>>();
std::shared_ptr<boost::asio::io_context> ioc = pimpl->ioc;
std::shared_ptr<udp::resolver> resolver = std::make_shared<udp::resolver>(*ioc);
resolver->async_resolve(udp::v4(), host, port, [resolver, ioc, pro, timeout](boost::system::error_code ec, udp::resolver::results_type endpoints) {
if(ec)
return try_set_exception(*pro, std::make_exception_ptr(boost::system::system_error(ec, "解析域名时发生错误"))), void();
std::shared_ptr<udp::socket> socket = std::make_shared<udp::socket>(*ioc, udp::endpoint(udp::v4(), 0));
for(auto &&endpoint : endpoints)
{
// first attempt
constexpr char request_challenge[] = "\xFF\xFF\xFF\xFF" "U" "\xFF\xFF\xFF\xFF";
socket->async_send_to(boost::asio::buffer(request_challenge, sizeof(request_challenge)), endpoint, [socket, endpoint, pro](boost::system::error_code ec, std::size_t bytes_transferred) {
if(ec)
return try_set_exception(*pro, std::make_exception_ptr(boost::system::system_error(ec, "发送玩家查询包时发生错误"))), void();
std::shared_ptr<char> buffer(new char[4096], std::default_delete<char[]>());
std::shared_ptr<udp::endpoint> sender_endpoint = std::make_shared<udp::endpoint>(udp::v4(), 0);
socket->async_receive_from(boost::asio::buffer(buffer.get(), 4096), *sender_endpoint, [socket, endpoint, buffer, sender_endpoint, pro](boost::system::error_code ec, std::size_t reply_length) {
if (ec)
return try_set_exception(*pro, std::make_exception_ptr(boost::system::system_error(ec, "无法接收玩家查询包"))), void();
PlayerListQueryResult first_result;
try{
first_result = MakePlayerListQueryResultFromBuffer(buffer.get(), reply_length, sender_endpoint->address().to_string(), sender_endpoint->port());
if(first_result.Results.index() == 1)
return pro->set_value(first_result), void();
} catch(...) {
return try_set_exception(*pro, std::current_exception()), void();
}
// second attempt
const int32_t challenge = std::get<int32_t>(first_result.Results);
const char(&accessor)[4] = reinterpret_cast<const char(&)[4]>(challenge);
char request3[10] = { '\xFF', '\xFF', '\xFF', '\xFF', 'U', accessor[0], accessor[1], accessor[2], accessor[3], '\0' };
socket->async_send_to(boost::asio::buffer(request3, sizeof(request3)), endpoint, [socket, buffer, sender_endpoint, endpoint, pro](boost::system::error_code ec, std::size_t bytes_transferred) {
if (ec)
return try_set_exception(*pro, std::make_exception_ptr(boost::system::system_error(ec, "发送challenge玩家查询包时发生错误"))), void();
socket->async_receive_from(boost::asio::buffer(buffer.get(), 4096), *sender_endpoint, [socket, endpoint, buffer, sender_endpoint, pro](boost::system::error_code ec, std::size_t reply_length) {
if (ec)
return try_set_exception(*pro, std::make_exception_ptr(boost::system::system_error(ec, "无法接收challenge玩家查询包"))), void();
try{
return pro->set_value(MakePlayerListQueryResultFromBuffer(buffer.get(), reply_length, sender_endpoint->address().to_string(), sender_endpoint->port())), void();
} catch(...) {
return try_set_exception(*pro, std::current_exception()), void();
}
});
});
});
});
}
std::shared_ptr<boost::asio::system_timer> ddl = std::make_shared<boost::asio::system_timer>(*ioc);
ddl->expires_from_now(timeout);
ddl->async_wait([ioc, socket, pro, ddl](boost::system::error_code ec){
socket->close(ec);
return try_set_exception(*pro, std::make_exception_ptr(boost::system::system_error(boost::asio::error::make_error_code(boost::asio::error::timed_out), "查询服务器超时,可能是服务器挂了或者IP不正确。"))), void();
});
});
return pro->get_future();
}