This repository was archived by the owner on Dec 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathserver_main.cpp
More file actions
304 lines (257 loc) · 10.2 KB
/
server_main.cpp
File metadata and controls
304 lines (257 loc) · 10.2 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include <iostream>
#include <memory>
#include <string>
#include <thread>
#include <chrono>
#include <mutex>
#include <cstdio>
#include <csignal>
#include <asio.hpp>
#include <grpcpp/grpcpp.h>
#include "protos/connect_tool.grpc.pb.h"
#include "core/connect_tool_core.h"
#include "core/asio_event_loop.h"
#include "config/config_manager.h"
using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::Status;
using connecttool::ConnectToolService;
using connecttool::CreateLobbyRequest;
using connecttool::CreateLobbyResponse;
using connecttool::JoinLobbyRequest;
using connecttool::JoinLobbyResponse;
using connecttool::LeaveLobbyRequest;
using connecttool::LeaveLobbyResponse;
using connecttool::GetLobbyInfoRequest;
using connecttool::GetLobbyInfoResponse;
using connecttool::GetFriendLobbiesRequest;
using connecttool::GetFriendLobbiesResponse;
using connecttool::InviteFriendRequest;
using connecttool::InviteFriendResponse;
using connecttool::GetVPNStatusRequest;
using connecttool::GetVPNStatusResponse;
using connecttool::GetVPNRoutingTableRequest;
using connecttool::GetVPNRoutingTableResponse;
using connecttool::GetVersionRequest;
using connecttool::GetVersionResponse;
class ConnectToolServiceImpl final : public ConnectToolService::Service {
public:
ConnectToolServiceImpl(ConnectToolCore* core) : core_(core) {}
Status GetVersion(ServerContext* context, const GetVersionRequest* request, GetVersionResponse* reply) override {
reply->set_version(APP_VERSION_STRING);
return Status::OK;
}
Status CreateLobby(ServerContext* context, const CreateLobbyRequest* request, CreateLobbyResponse* reply) override {
std::lock_guard<std::mutex> lock(mutex_);
std::string lobbyId;
bool success = core_->createLobby(lobbyId);
reply->set_success(success);
reply->set_lobby_id(lobbyId);
return Status::OK;
}
Status JoinLobby(ServerContext* context, const JoinLobbyRequest* request, JoinLobbyResponse* reply) override {
std::lock_guard<std::mutex> lock(mutex_);
bool success = core_->joinLobby(request->lobby_id());
reply->set_success(success);
reply->set_message(success ? "Join request sent" : "Failed to join lobby");
return Status::OK;
}
Status LeaveLobby(ServerContext* context, const LeaveLobbyRequest* request, LeaveLobbyResponse* reply) override {
std::lock_guard<std::mutex> lock(mutex_);
core_->leaveLobby();
reply->set_success(true);
return Status::OK;
}
Status GetLobbyInfo(ServerContext* context, const GetLobbyInfoRequest* request, GetLobbyInfoResponse* reply) override {
std::lock_guard<std::mutex> lock(mutex_);
bool inLobby = core_->isInLobby();
reply->set_is_in_lobby(inLobby);
if (inLobby) {
reply->set_lobby_id(std::to_string(core_->getCurrentLobbyId().ConvertToUint64()));
auto members = core_->getLobbyMembers();
for (const auto& memberID : members) {
auto* member = reply->add_members();
member->set_steam_id(std::to_string(memberID.ConvertToUint64()));
member->set_name(SteamFriends()->GetFriendPersonaName(memberID));
auto connInfo = core_->getMemberConnectionInfo(memberID);
member->set_ping(connInfo.ping);
member->set_relay_info(connInfo.relayInfo);
}
}
return Status::OK;
}
Status GetFriendLobbies(ServerContext* context, const GetFriendLobbiesRequest* request, GetFriendLobbiesResponse* reply) override {
std::lock_guard<std::mutex> lock(mutex_);
auto lobbies = core_->getFriendLobbies();
for (const auto& lobby : lobbies) {
auto* friendLobby = reply->add_lobbies();
friendLobby->set_steam_id(std::to_string(lobby.friendID.ConvertToUint64()));
friendLobby->set_name(lobby.friendName);
friendLobby->set_lobby_id(std::to_string(lobby.lobbyID.ConvertToUint64()));
}
return Status::OK;
}
Status InviteFriend(ServerContext* context, const InviteFriendRequest* request, InviteFriendResponse* reply) override {
std::lock_guard<std::mutex> lock(mutex_);
bool success = core_->inviteFriend(request->friend_steam_id());
reply->set_success(success);
return Status::OK;
}
Status GetVPNStatus(ServerContext* context, const GetVPNStatusRequest* request, GetVPNStatusResponse* reply) override {
std::lock_guard<std::mutex> lock(mutex_);
reply->set_enabled(core_->isVPNEnabled());
reply->set_local_ip(core_->getLocalVPNIP());
reply->set_device_name(core_->getTunDeviceName());
auto stats = core_->getVPNStatistics();
auto* statsProto = reply->mutable_stats();
statsProto->set_packets_sent(stats.packetsSent);
statsProto->set_bytes_sent(stats.bytesSent);
statsProto->set_packets_received(stats.packetsReceived);
statsProto->set_bytes_received(stats.bytesReceived);
statsProto->set_packets_dropped(stats.packetsDropped);
return Status::OK;
}
Status GetVPNRoutingTable(ServerContext* context, const GetVPNRoutingTableRequest* request, GetVPNRoutingTableResponse* reply) override {
std::lock_guard<std::mutex> lock(mutex_);
auto table = core_->getVPNRoutingTable();
for (const auto& entry : table) {
auto* route = reply->add_routes();
route->set_ip(entry.first);
route->set_name(entry.second.name);
route->set_is_local(entry.second.isLocal);
}
return Status::OK;
}
private:
ConnectToolCore* core_;
std::mutex mutex_;
};
// 信号处理
std::unique_ptr<grpc::Server> g_server;
std::atomic<bool> g_running{true};
void signalHandler(int signal) {
std::cout << "\nReceived signal " << signal << ", shutting down..." << std::endl;
g_running = false;
AsioEventLoop::instance().stop();
if (g_server) {
g_server->Shutdown();
}
}
/**
* @brief 基于 Asio 的 Steam 回调定时器
*
* 使用 Asio 定时器替代传统的 sleep 循环,实现更高效的事件驱动
*/
class SteamCallbackTimer {
public:
SteamCallbackTimer(asio::io_context& io, ConnectToolCore* core, std::chrono::milliseconds interval)
: timer_(io)
, core_(core)
, interval_(interval)
, running_(false) {}
void start() {
running_ = true;
scheduleNext();
}
void stop() {
running_ = false;
timer_.cancel();
}
private:
void scheduleNext() {
if (!running_) return;
timer_.expires_after(interval_);
timer_.async_wait([this](const asio::error_code& ec) {
if (!ec && running_) {
core_->update();
scheduleNext();
}
});
}
asio::steady_timer timer_;
ConnectToolCore* core_;
std::chrono::milliseconds interval_;
std::atomic<bool> running_;
};
int main(int argc, char** argv) {
// 设置信号处理
#ifdef _WIN32
signal(SIGINT, signalHandler);
signal(SIGTERM, signalHandler);
#else
struct sigaction sa;
sa.sa_handler = signalHandler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sigaction(SIGINT, &sa, nullptr);
sigaction(SIGTERM, &sa, nullptr);
#endif
std::cout << "=== ConnectTool v" << APP_VERSION_STRING << " ===" << std::endl;
// 初始化配置管理器 - 从远程加载配置
auto& configManager = ConfigManager::instance();
std::cout << "Loading configuration from remote server..." << std::endl;
if (!configManager.loadFromRemote()) {
std::cerr << "ERROR: Failed to load configuration from all remote sources!" << std::endl;
std::cerr << "Please check your network connection and try again." << std::endl;
std::cerr << "Error: " << configManager.getLastError() << std::endl;
return 1;
}
// 检查版本要求
if (!configManager.checkVersion()) {
std::cerr << "ERROR: Application version is too old!" << std::endl;
std::cerr << "Current version: " << ConfigManager::getAppVersion() << std::endl;
std::cerr << "Minimum required version: " << configManager.getMinVersion() << std::endl;
std::cerr << "Please update to the latest version." << std::endl;
return 1;
}
std::cout << "Configuration loaded successfully. Min version: "
<< configManager.getMinVersion() << std::endl;
const auto& config = configManager.getConfig();
// Initialize Core
ConnectToolCore core;
if (!core.initSteam()) {
std::cerr << "Failed to initialize Steam. Exiting." << std::endl;
return 1;
}
// 获取 Asio 事件循环
auto& eventLoop = AsioEventLoop::instance();
auto& ioContext = eventLoop.getContext();
// 创建 Steam 回调定时器(使用配置的间隔)
SteamCallbackTimer steamTimer(ioContext, &core,
std::chrono::milliseconds(config.networking.steam_callback_interval_ms));
steamTimer.start();
// Define server address based on platform(使用配置的路径)
#ifdef _WIN32
std::string socket_path = config.server.unix_socket_path_windows;
#else
std::string socket_path = config.server.unix_socket_path_unix;
#endif
// Remove the socket file if it already exists
std::remove(socket_path.c_str());
std::string server_address("unix:" + socket_path);
ConnectToolServiceImpl service(&core);
ServerBuilder builder;
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
builder.RegisterService(&service);
g_server = std::unique_ptr<Server>(builder.BuildAndStart());
if (!g_server) {
std::cerr << "Failed to start gRPC server" << std::endl;
return 1;
}
std::cout << "Server listening on " << server_address << std::endl;
std::cout << "Press Ctrl+C to shutdown..." << std::endl;
// 在后台线程运行 gRPC 服务器
std::thread grpcThread([&]() {
g_server->Wait();
});
// 在主线程运行 Asio 事件循环
eventLoop.run();
// 清理
steamTimer.stop();
if (grpcThread.joinable()) {
grpcThread.join();
}
std::cout << "Server shutdown complete." << std::endl;
return 0;
}