diff --git a/protocol/include/microsoft/net/remote/protocol/NetRemoteProtocol.hxx b/protocol/include/microsoft/net/remote/protocol/NetRemoteProtocol.hxx index 2b4b98e8..76a12b52 100644 --- a/protocol/include/microsoft/net/remote/protocol/NetRemoteProtocol.hxx +++ b/protocol/include/microsoft/net/remote/protocol/NetRemoteProtocol.hxx @@ -16,17 +16,20 @@ static constexpr std::string_view NetRemoteAddressHttpDefault = "localhost:5047" */ struct NetRemoteProtocol { -#define IP_DEFAULT "localhost" -#define PORT_DEFAULT 5047 -#define xstr(s) str(s) -#define str(s) #s +#define IP_DEFAULT "localhost" +#define PORT_DEFAULT 5047 +#define PORT_SEPARATOR "" +#define xstr(s) str(s) +#define str(s) #s static constexpr uint32_t PortDefault{ 5047 }; + static constexpr std::string_view PortSeparator{ ":" }; static constexpr std::string_view IpDefault{ "localhost" }; - static constexpr std::string_view AddressDefault{ IP_DEFAULT ":" xstr(PORT_DEFAULT) }; + static constexpr std::string_view AddressDefault{ IP_DEFAULT PORT_SEPARATOR xstr(PORT_DEFAULT) }; #undef IP_DEFAULT #undef PORT_DEFAULT +#undef PORT_SEPARATOR #undef xstr #undef str }; diff --git a/src/common/tools/cli/NetRemoteCli.cxx b/src/common/tools/cli/NetRemoteCli.cxx index 1c1e52a2..4e637ccf 100644 --- a/src/common/tools/cli/NetRemoteCli.cxx +++ b/src/common/tools/cli/NetRemoteCli.cxx @@ -1,7 +1,9 @@ +#include #include #include +#include #include #include @@ -60,7 +62,7 @@ NetRemoteCli::CreateParser() noexcept auto optionServer = app->add_option_function("-s,--server", [this](const std::string& serverAddress) { OnServerAddressChanged(serverAddress); }); - optionServer->description("The address of the netremote server to connect to"); + optionServer->description("The address of the netremote server to connect to, with format '[:port]"); m_cliAppServerAddress = optionServer; m_cliAppWifi = AddSubcommandWifi(app.get()); @@ -94,11 +96,19 @@ NetRemoteCli::AddSubcommandWifiEnumerateAccessPoints(CLI::App* parent) } void -NetRemoteCli::OnServerAddressChanged(const std::string& serverAddress) +NetRemoteCli::OnServerAddressChanged(const std::string& serverAddressArg) { - m_cliData->ServerAddress = serverAddress; + using Protocol::NetRemoteProtocol; - auto connection = NetRemoteServerConnection::TryEstablishConnection(serverAddress); + // Append the default port if not specified in command-line argument. + auto serverAddress = serverAddressArg; + if (serverAddress.find(':') == serverAddress.npos) { + serverAddress += std::format("{}{}", NetRemoteProtocol::PortSeparator, NetRemoteProtocol::PortDefault); + } + + m_cliData->ServerAddress = std::move(serverAddress); + + auto connection = NetRemoteServerConnection::TryEstablishConnection(m_cliData->ServerAddress); if (connection == nullptr) { LOGE << "Failed to create server connection"; return;