Skip to content

Commit

Permalink
added fallback and failsaves for incorrect listen addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
99oblivius committed Nov 16, 2024
1 parent 9ade30a commit d583996
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 65 deletions.
2 changes: 1 addition & 1 deletion buildspec.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
},
"name": "vrbro",
"displayName": "VRBro Plugin",
"version": "1.0.4",
"version": "1.0.5",
"author": "99oblivius",
"website": "https://github.com/99oblivius",
"email": "projects@oblivius.dev",
Expand Down
63 changes: 36 additions & 27 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <obs-frontend-api.h>
#include <obs-module.h>
#include <util/config-file.h>
#include <asio.hpp>

#include "plugin-support.h"
#include "vrbro.hpp"
Expand Down Expand Up @@ -47,48 +48,56 @@ Config::Config() {
void Config::Load() {
config_t* obs_config = GetConfigStore();
if (obs_config) {
ListenAddrString = config_get_string(obs_config, TITLE, LISTEN_ADDR_STRING);
ListenAddrString = QString::fromUtf8(config_get_string(obs_config, TITLE, LISTEN_ADDR_STRING));
ListenPortNumber = QString::number(config_get_uint(obs_config, TITLE, LISTEN_PORT_NUMBER));
AutoBufferBool = config_get_bool(obs_config, TITLE, AUTO_BUFFER);
}
}

void Config::Save() {
config_t* obs_config = GetConfigStore();
if (!obs_config) {
return;
}
if (!obs_config) return;

// Validate and save address
// Skip test if already using default address
ListenAddrString.remove(QRegularExpression("\\s+"));
if (!isValidAddr(ListenAddrString)) {
QMessageBox alertbox;
alertbox.setWindowTitle("VRBro.Address.Error");
alertbox.setText("x.x.x.x\n\nChoose a valid listening address\ne.g 0.0.0.0");
alertbox.exec();
return;
if (ListenAddrString != ConfigConstants::DEFAULT_ADDRESS) {
try {
asio::io_context test_context;
asio::ip::tcp::acceptor test_acceptor(test_context);
auto endpoint = asio::ip::tcp::endpoint(
asio::ip::make_address(ListenAddrString.toStdString()),
33390
);
test_acceptor.open(endpoint.protocol());
test_acceptor.bind(endpoint);
test_acceptor.close();
test_context.stop();
} catch (...) {
QMessageBox::StandardButton reply = QMessageBox::question(nullptr, "VRBro.Address.Error",
"Address cannot be used. Use default (127.0.0.1)?",
QMessageBox::Yes | QMessageBox::No);

if (reply == QMessageBox::Yes) {
ListenAddrString = ConfigConstants::DEFAULT_ADDRESS;
} else {
return;
}
}
}
config_set_string(obs_config, TITLE, LISTEN_ADDR_STRING,
ListenAddrString.toUtf8().constData());

// Validate and save port
if (!isValidPort(ListenPortNumber)) {
QMessageBox alertbox;
alertbox.setWindowTitle("VRBro.Port.Error");
alertbox.setText("1024 <-> 65535\n\nChoose a valid port number\ne.g 33390");
alertbox.exec();

// Port validation
bool ok;
uint32_t port = ListenPortNumber.toUInt(&ok);
if (!ok || port < 1024 || port > 65535) {
QMessageBox::warning(nullptr, "VRBro.Port.Error", "Invalid port (use 1024-65535)");
return;
}
config_set_uint(obs_config, TITLE, LISTEN_PORT_NUMBER,
ListenPortNumber.toULongLong());

// Save auto buffer setting
// Save settings
config_set_string(obs_config, TITLE, LISTEN_ADDR_STRING, ListenAddrString.toUtf8().constData());
config_set_uint(obs_config, TITLE, LISTEN_PORT_NUMBER, port);
config_set_bool(obs_config, TITLE, AUTO_BUFFER, AutoBufferBool);

obs_log(LOG_WARNING, "[VRBro::Config] listen: %s port: %d",
ListenAddrString.toUtf8().constData(),
ListenPortNumber.toULongLong());

config_save(obs_config);
vrbro_restart_server();
}
Expand Down
2 changes: 1 addition & 1 deletion src/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Config {
static config_t* GetConfigStore();
static void OBSSaveCallback(obs_data_t* config_data, bool save, void* data);

QString ListenAddrString{ConfigConstants::DEFAULT_ADDRESS};
QString ListenAddrString{QString::fromUtf8(ConfigConstants::DEFAULT_ADDRESS)};
QString ListenPortNumber{QString::number(ConfigConstants::DEFAULT_PORT)};
std::atomic<bool> AutoBufferBool{false};
};
2 changes: 1 addition & 1 deletion src/network/network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ class Network {

// ASIO components
asio::io_context io_context_;
asio::ip::tcp::acceptor acceptor_;
asio::ip::tcp::acceptor acceptor_{io_context_};
std::thread io_thread_;
};
81 changes: 46 additions & 35 deletions src/vrbro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <obs-frontend-api.h>
#include <obs-module.h>
#include <string>

#include "config.hpp"
#include "forms/server-settings.hpp"
Expand Down Expand Up @@ -54,47 +55,57 @@ void vrbro_restart_server() {

bool obs_module_load(void) {
QMainWindow* main_window = static_cast<QMainWindow*>(obs_frontend_get_main_window());
if (!main_window) return true;

if (main_window) {
_conf = std::make_shared<Config>();
_conf->Load();
_conf = std::make_shared<Config>();
_conf->Load();

// Try configured address, fallback to default if needed
obs_log(LOG_WARNING, "[vrbro-server] Attempting network init: %s:%d",
_conf->ListenAddrString.toUtf8().constData(),
_conf->ListenPortNumber.toInt());

try {
_net = std::make_shared<Network>(
_conf->ListenAddrString.toStdString(),
_conf->ListenPortNumber.toInt(),
3
);

QAction* menu_action = static_cast<QAction*>(
obs_frontend_add_tools_menu_qaction(obs_module_text("VRBro Server Settings"))
);

obs_frontend_push_ui_translation(obs_module_get_string);
server_settings = new ServerSettings(main_window);
obs_frontend_pop_ui_translation();

auto menu_cb = [] {
server_settings->ToggleShowHide();
};
menu_action->connect(menu_action, &QAction::triggered, menu_cb);

obs_frontend_add_event_callback(
[](enum obs_frontend_event event, void*) {
if (event == OBS_FRONTEND_EVENT_FINISHED_LOADING) {
config_t* obs_conf = obs_frontend_get_global_config();
if (config_get_bool(obs_conf, "VRBroServerPlugin", "AutoBufferBool") &&
!obs_frontend_replay_buffer_active()) {
obs_frontend_replay_buffer_start();
}
vrbro_start_server();
} else if (event == OBS_FRONTEND_EVENT_EXIT) {
vrbro_stop_server();
}
},
static_cast<void*>(&_conf)
);
_conf->ListenPortNumber.toInt(), 3);
} catch (const std::exception& e) {
obs_log(LOG_ERROR, "[vrbro-server] Network init failed: %s - Trying default", e.what());
try {
_net = std::make_shared<Network>(ConfigConstants::DEFAULT_ADDRESS,
_conf->ListenPortNumber.toInt(), 3);
_conf->ListenAddrString = ConfigConstants::DEFAULT_ADDRESS;
} catch (const std::exception& e) {
obs_log(LOG_ERROR, "[vrbro-server] Default address also failed: %s", e.what());
}
}

auto menu_action = static_cast<QAction*>(
obs_frontend_add_tools_menu_qaction(obs_module_text("VRBro Server Settings")));

obs_frontend_push_ui_translation(obs_module_get_string);
server_settings = new ServerSettings(main_window);
obs_frontend_pop_ui_translation();

menu_action->connect(menu_action, &QAction::triggered,
[] { server_settings->ToggleShowHide(); });

obs_frontend_add_event_callback(
[](enum obs_frontend_event event, void*) {
if (event == OBS_FRONTEND_EVENT_FINISHED_LOADING) {
config_t* obs_conf = obs_frontend_get_global_config();
if (config_get_bool(obs_conf, "VRBroServerPlugin", "AutoBufferBool") &&
!obs_frontend_replay_buffer_active()) {
obs_frontend_replay_buffer_start();
}
vrbro_start_server();
} else if (event == OBS_FRONTEND_EVENT_EXIT) {
vrbro_stop_server();
}
},
static_cast<void*>(&_conf)
);

obs_log(LOG_INFO, "[vrbro-server] VRBro Server version: %s", PLUGIN_VERSION);
return true;
}
Expand Down

0 comments on commit d583996

Please sign in to comment.