From 90b7f0d0c064bc29369577663d360138316f37b5 Mon Sep 17 00:00:00 2001 From: Martin Michelsen Date: Fri, 10 Nov 2023 17:58:45 -0800 Subject: [PATCH] add $ping command --- README.md | 1 + src/ChatCommands.cc | 6 ++++++ src/Client.cc | 1 + src/Client.hh | 1 + src/ReceiveCommands.cc | 11 ++++++++++- 5 files changed, 19 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 37af523e..4eb1e1fd 100644 --- a/README.md +++ b/README.md @@ -271,6 +271,7 @@ Some commands only work on the game server and not on the proxy server. The chat * Information commands * `$li`: Shows basic information about the lobby or game you're in. If you're on the proxy server, shows information about your connection instead (remote Guild Card number, client ID, etc.). + * `$ping` (game server only): Shows round-trip ping time from the server to you. * `$what` (game server only): Shows the type, name, and stats of the nearest item on the ground. * `$matcount` (game server only): Shows how many of each type of material you've used. diff --git a/src/ChatCommands.cc b/src/ChatCommands.cc index ab59920d..720324a9 100644 --- a/src/ChatCommands.cc +++ b/src/ChatCommands.cc @@ -159,6 +159,11 @@ static void server_command_lobby_info(shared_ptr c, const std::string&) send_text_message(c, join(lines, "\n")); } +static void server_command_ping(shared_ptr c, const std::string&) { + c->ping_start_time = now(); + send_command(c, 0x1D, 0x00); +} + static void proxy_command_lobby_info(shared_ptr ses, const std::string&) { string msg; // On non-masked-GC sessions (BB), there is no remote Guild Card number, so we @@ -1598,6 +1603,7 @@ static const unordered_map chat_commands({ {"$password", {server_command_password, nullptr}}, {"$patch", {server_command_patch, proxy_command_patch}}, {"$persist", {server_command_persist, nullptr}}, + {"$ping", {server_command_ping, nullptr}}, {"$playrec", {server_command_playrec, nullptr}}, {"$rand", {server_command_rand, proxy_command_rand}}, {"$saverec", {server_command_saverec, nullptr}}, diff --git a/src/Client.cc b/src/Client.cc index 472ee442..c62b7109 100644 --- a/src/Client.cc +++ b/src/Client.cc @@ -147,6 +147,7 @@ Client::Client( should_send_to_lobby_server(false), should_send_to_proxy_server(false), bb_connection_phase(0xFF), + ping_start_time(0), sub_version(-1), x(0.0f), z(0.0f), diff --git a/src/Client.hh b/src/Client.hh index df3a3ef6..44d662c6 100644 --- a/src/Client.hh +++ b/src/Client.hh @@ -166,6 +166,7 @@ struct Client : public std::enable_shared_from_this { std::shared_ptr xb_netloc; parray xb_9E_unknown_a1a; uint8_t bb_connection_phase; + uint64_t ping_start_time; // Patch server std::vector patch_file_checksum_requests; diff --git a/src/ReceiveCommands.cc b/src/ReceiveCommands.cc index 79a442b3..2e2e3169 100644 --- a/src/ReceiveCommands.cc +++ b/src/ReceiveCommands.cc @@ -322,6 +322,15 @@ void on_disconnect(shared_ptr c) { //////////////////////////////////////////////////////////////////////////////// +static void on_1D(shared_ptr c, uint16_t, uint32_t, string&) { + if (c->ping_start_time) { + uint64_t ping_usecs = now() - c->ping_start_time; + c->ping_start_time = 0; + double ping_ms = static_cast(ping_usecs) / 1000.0; + send_text_message_printf(c, "To server: %gms", ping_ms); + } +} + static void on_05_XB(shared_ptr c, uint16_t, uint32_t, string&) { // The Xbox Live service doesn't close the TCP connection when the player // chooses Quit Game, so we manually disconnect the client when they send this @@ -4360,7 +4369,7 @@ static on_command_t handlers[0x100][6] = { /* 1A */ {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr}, /* 1B */ {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr}, /* 1C */ {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr}, - /* 1D */ {nullptr, on_ignored, on_ignored, on_ignored, on_ignored, on_ignored}, + /* 1D */ {nullptr, on_1D, on_1D, on_1D, on_1D, on_1D}, /* 1E */ {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr}, /* 1F */ {nullptr, on_1F, on_1F, nullptr, nullptr, nullptr}, // PATCH DC PC GC XB BB