Skip to content

Commit

Permalink
Allow supplying address without port.
Browse files Browse the repository at this point in the history
  • Loading branch information
abeltrano committed Jan 18, 2024
1 parent fc429c1 commit c520300
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
Expand Down
18 changes: 14 additions & 4 deletions src/common/tools/cli/NetRemoteCli.cxx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@

#include <format>
#include <string>

#include <microsoft/net/remote/NetRemoteCli.hxx>
#include <microsoft/net/remote/protocol/NetRemoteProtocol.hxx>
#include <notstd/Memory.hxx>
#include <plog/Log.h>

Expand Down Expand Up @@ -60,7 +62,7 @@ NetRemoteCli::CreateParser() noexcept
auto optionServer = app->add_option_function<std::string>("-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 '<hostname>[:port]");

m_cliAppServerAddress = optionServer;
m_cliAppWifi = AddSubcommandWifi(app.get());
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit c520300

Please sign in to comment.