Skip to content

Commit

Permalink
refactor(main): move map_port to network
Browse files Browse the repository at this point in the history
  • Loading branch information
ReenigneArcher committed Feb 7, 2024
1 parent 0aa4f06 commit 69cef9c
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 28 deletions.
26 changes: 1 addition & 25 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "httpcommon.h"
#include "logging.h"
#include "main.h"
#include "network.h"
#include "nvhttp.h"
#include "platform/common.h"
#include "process.h"
Expand Down Expand Up @@ -811,28 +812,3 @@ write_file(const char *path, const std::string_view &contents) {

return 0;
}

/**
* @brief Map a specified port based on the base port.
* @param port The port to map as a difference from the base port.
* @return `std:uint16_t` : The mapped port number.
*
* EXAMPLES:
* ```cpp
* std::uint16_t mapped_port = map_port(1);
* ```
*/
std::uint16_t
map_port(int port) {
// calculate the port from the config port
auto mapped_port = (std::uint16_t)((int) config::sunshine.port + port);

// Ensure port is in the range of 1024-65535
if (mapped_port < 1024 || mapped_port > 65535) {
BOOST_LOG(warning) << "Port out of range: "sv << mapped_port;
}

// TODO: Ensure port is not already in use by another application

return mapped_port;
}
2 changes: 0 additions & 2 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ std::string
read_file(const char *path);
int
write_file(const char *path, const std::string_view &contents);
std::uint16_t
map_port(int port);
void
launch_ui();
void
Expand Down
28 changes: 27 additions & 1 deletion src/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,39 @@
* @file src/network.cpp
* @brief todo
*/
#include "network.h"
#include "config.h"
#include "logging.h"
#include "network.h"
#include "utility.h"
#include <algorithm>

using namespace std::literals;

/**
* @brief Map a specified port based on the base port.
* @param port The port to map as a difference from the base port.
* @return `std:uint16_t` : The mapped port number.
*
* EXAMPLES:
* ```cpp
* std::uint16_t mapped_port = map_port(1);
* ```
*/
std::uint16_t
map_port(int port) {
// calculate the port from the config port
auto mapped_port = (std::uint16_t)((int) config::sunshine.port + port);

// Ensure port is in the range of 1024-65535
if (mapped_port < 1024 || mapped_port > 65535) {
BOOST_LOG(warning) << "Port out of range: "sv << mapped_port;
}

// TODO: Ensure port is not already in use by another application

return mapped_port;
}

namespace ip = boost::asio::ip;

namespace net {
Expand Down
4 changes: 4 additions & 0 deletions src/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

#include "utility.h"

// functions
std::uint16_t
map_port(int port);

namespace net {
void
free_host(ENetHost *host);
Expand Down
1 change: 1 addition & 0 deletions src/platform/linux/publish.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "misc.h"
#include "src/logging.h"
#include "src/main.h"
#include "src/network.h"
#include "src/nvhttp.h"
#include "src/platform/common.h"
#include "src/utility.h"
Expand Down
1 change: 1 addition & 0 deletions src/platform/macos/publish.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "misc.h"
#include "src/logging.h"
#include "src/main.h"
#include "src/network.h"
#include "src/nvhttp.h"
#include "src/platform/common.h"
#include "src/utility.h"
Expand Down

0 comments on commit 69cef9c

Please sign in to comment.