Skip to content

Commit

Permalink
🎉 Reimplemented webserver with POCO
Browse files Browse the repository at this point in the history
  • Loading branch information
AnotherFoxGuy committed Jan 8, 2025
1 parent 59bef02 commit c49be64
Show file tree
Hide file tree
Showing 16 changed files with 503 additions and 4 deletions.
4 changes: 4 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
BasedOnStyle: Microsoft

...
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ set(CMAKE_PREFIX_PATH ${ROR_DEPENDENCY_DIR} ${CMAKE_PREFIX_PATH})

set(CMAKE_THREAD_PREFER_PTHREAD YES)
find_package(Threads REQUIRED)
find_package(Angelscript)
find_package(jsoncpp REQUIRED)
find_package(SocketW REQUIRED)

find_package(Angelscript)
cmake_dependent_option(RORSERVER_WITH_ANGELSCRIPT "Adds scripting support" ON "TARGET Angelscript::angelscript" OFF)

find_package(Poco)
cmake_dependent_option(RORSERVER_WITH_WEBSERVER "Adds the webserver" ON "TARGET Poco::Poco" OFF)

# setup paths
SET(RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/")
SET(LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib/")
Expand Down
20 changes: 20 additions & 0 deletions cmake/PageCompiler.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
find_program(PAGECOMPILER_EXE cpspc HINTS ${PAGECOMPILER_DIR} REQUIRED)



macro(PageCompiler file outfile)
get_filename_component(out ${outfile} DIRECTORY)

if (WIN32)
set(PC_COMMAND ${PAGECOMPILER_EXE} /output-dir ${out} ${file})
else ()
set(PC_COMMAND ${PAGECOMPILER_EXE} --output-dir ${out} ${file})
endif ()

add_custom_command(
OUTPUT ${outfile}
COMMENT "Compiling ${file}"
DEPENDS ${file}
COMMAND ${PC_COMMAND}
)
endmacro()
22 changes: 19 additions & 3 deletions conanfile.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
import os
from conan import ConanFile
from conan.tools.files import copy
from conan.tools.cmake import CMakeToolchain, CMakeDeps
from conan.tools.files import copy, save


class RoRServer(ConanFile):
name = "RoRServer"
settings = "os", "compiler", "build_type", "arch"
generators = "CMakeToolchain", "CMakeDeps"
default_options = {
"poco*:enable_pagecompiler": True,
"poco*:enable_data_mysql": False,
}

def layout(self):
self.folders.generators = os.path.join(self.folders.build, "generators")

def requirements(self):
self.requires("angelscript/2.37.0")
self.requires("jsoncpp/1.9.5")
self.requires("poco/1.13.3")
self.requires("openssl/3.3.2", override=True)
self.requires("socketw/3.11.0@anotherfoxguy/stable")
self.requires("socketw/3.11.0@anotherfoxguy/stable")

def generate(self):
pc_exe = self.dependencies["poco"].cpp_info.bindirs[0].replace("\\", "/")
tc = CMakeToolchain(self)
tc.variables["PAGECOMPILER_DIR"] = pc_exe
tc.generate()
deps = CMakeDeps(self)
deps.generate()
if self.settings.build_type == "Release":
deps.configuration = "RelWithDebInfo"
deps.generate()
30 changes: 30 additions & 0 deletions source/server/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
FILE(GLOB_RECURSE server_src CONFIGURE_DEPENDS *.cpp *.c *.h *.rc)

if (RORSERVER_WITH_WEBSERVER)
include(PageCompiler)

file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/web)

PageCompiler("${CMAKE_SOURCE_DIR}/source/web/index.cpsp" "${CMAKE_CURRENT_BINARY_DIR}/web/index.cpp")
PageCompiler("${CMAKE_SOURCE_DIR}/source/web/playerlist.cpsp" "${CMAKE_CURRENT_BINARY_DIR}/web/playerlist.cpp")
PageCompiler("${CMAKE_SOURCE_DIR}/source/web/playerlist_test.cpsp" "${CMAKE_CURRENT_BINARY_DIR}/web/playerlist_test.cpp")
set(
server_src ${server_src}
"${CMAKE_CURRENT_BINARY_DIR}/web/index.cpp"
"${CMAKE_CURRENT_BINARY_DIR}/web/playerlist.cpp"
"${CMAKE_CURRENT_BINARY_DIR}/web/playerlist_test.cpp"
)
endif (RORSERVER_WITH_WEBSERVER)

# the final lib
add_executable(${PROJECT_NAME} ${server_src})

Expand All @@ -17,6 +33,12 @@ endif ()

target_link_libraries(${PROJECT_NAME} PRIVATE Threads::Threads SocketW::SocketW jsoncpp_lib)

if (RORSERVER_WITH_WEBSERVER)
target_compile_definitions(${PROJECT_NAME} PRIVATE WITH_WEBSERVER)
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(${PROJECT_NAME} PRIVATE Poco::Poco)
endif (RORSERVER_WITH_WEBSERVER)

IF (WIN32)
target_compile_definitions(${PROJECT_NAME} PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
ELSEIF (UNIX)
Expand All @@ -37,6 +59,14 @@ ELSEIF (UNIX)
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
# install the files required for the runtime
INSTALL(
DIRECTORY ${CMAKE_SOURCE_DIR}/bin/resources/
DESTINATION share/rorserver/
FILES_MATCHING PATTERN "*"
PATTERN ".svn" EXCLUDE
PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ
)

# configure and install init script
CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/contrib/rorserver-initscript.in
Expand Down
14 changes: 14 additions & 0 deletions source/server/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ static std::string s_serverlist_host("api.rigsofrods.org");
static std::string s_serverlist_path("");
static std::string s_resourcedir(RESOURCE_DIR);

static unsigned int s_webserver_port(0);
static unsigned int s_listen_port(0);
static unsigned int s_max_clients(16);
static unsigned int s_heartbeat_retry_count(5);
Expand All @@ -73,6 +74,7 @@ static bool s_print_stats(false);
static bool s_foreground(false);
static bool s_show_version(false);
static bool s_show_help(false);
static bool s_webserver_enabled(false);

// Vehicle spawn limits
static size_t s_max_vehicles(20);
Expand Down Expand Up @@ -263,8 +265,10 @@ namespace Config {
HANDLE_ARG_VALUE("max-clients", { setMaxClients(atoi(value)); });
HANDLE_ARG_VALUE("vehicle-limit", { setMaxVehicles(atoi(value)); });
HANDLE_ARG_VALUE("port", { setListenPort(atoi(value)); });
HANDLE_ARG_VALUE("webserver-port", { setWebserverPort(atoi(value)); });

HANDLE_ARG_FLAG ("print-stats", { setPrintStats(true); });
HANDLE_ARG_FLAG ("use-webserver", { setWebserverEnabled(true); });
HANDLE_ARG_FLAG ("foreground", { setForeground(true); });
HANDLE_ARG_FLAG ("fg", { setForeground(true); });
HANDLE_ARG_FLAG ("inet", { setServerMode(SERVER_INET); });
Expand Down Expand Up @@ -310,6 +314,10 @@ namespace Config {

ServerType getServerMode() { return s_server_mode; }

bool getWebserverEnabled() { return s_webserver_enabled; }

unsigned int getWebserverPort() { return s_webserver_port; }

bool getPrintStats() { return s_print_stats; }

bool getForeground() { return s_foreground; }
Expand Down Expand Up @@ -407,6 +415,10 @@ namespace Config {
return true;
}

void setWebserverPort(unsigned int port) { s_webserver_port = port; }

void setWebserverEnabled(bool webserver) { s_webserver_enabled = webserver; }

void setPrintStats(bool value) { s_print_stats = value; }

void setAuthFile(const std::string &file) { s_authfile = file; }
Expand Down Expand Up @@ -494,6 +506,8 @@ namespace Config {
else if (strcmp(key, "port") == 0) { setListenPort(VAL_INT (value)); }
else if (strcmp(key, "mode") == 0) { SetConfServerMode(VAL_STR (value)); }
else if (strcmp(key, "printstats") == 0) { setPrintStats(VAL_BOOL(value)); }
else if (strcmp(key, "webserver") == 0) { setWebserverEnabled(VAL_BOOL(value)); }
else if (strcmp(key, "webserverport") == 0) { setWebserverPort(VAL_INT (value)); }
else if (strcmp(key, "foreground") == 0) { setForeground(VAL_BOOL(value)); }
else if (strcmp(key, "resdir") == 0) { setResourceDir(VAL_STR (value)); }
else if (strcmp(key, "logfilename") == 0) { Logger::SetOutputFile(VAL_STR (value)); }
Expand Down
8 changes: 8 additions & 0 deletions source/server/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ namespace Config {

bool getEnableScripting();

bool getWebserverEnabled();

unsigned int getWebserverPort();

bool getForeground();

const std::string &getResourceDir();
Expand Down Expand Up @@ -130,6 +134,10 @@ namespace Config {

bool setServerMode(ServerType mode);

void setWebserverEnabled(bool value);

void setWebserverPort(unsigned int port);

void setPrintStats(bool value);

void setHeartbeatIntervalSec(unsigned sec);
Expand Down
28 changes: 28 additions & 0 deletions source/server/macros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
This file is part of "Rigs of Rods Server" (Relay mode)
Copyright 2007 Pierre-Michel Ricordel
Copyright 2014+ Rigs of Rods Community
"Rigs of Rods Server" is free software: you can redistribute it
and/or modify it under the terms of the GNU General Public License
as published by the Free Software Foundation, either version 3
of the License, or (at your option) any later version.
"Rigs of Rods Server" is distributed in the hope that it will
be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Foobar. If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once


#define BEGIN_HTML_ITERATOR(_BEGIN, _END) for (auto it = _BEGIN; it != _END; it++){
#define END_HTML_ITERATOR }

#define BEGIN_HTML_LOOP(_max) for (int i = 0; i < _max; i++){
#define END_HTML_LOOP }
20 changes: 20 additions & 0 deletions source/server/rorserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@ void daemonize() {

#endif // ! _WIN32

#ifdef WITH_WEBSERVER
#include "webserver.h"
#endif

int main(int argc, char *argv[]) {
// set default verbose levels
Logger::SetLogLevel(LOGTYPE_DISPLAY, LOG_INFO);
Expand Down Expand Up @@ -331,6 +335,16 @@ int main(int argc, char *argv[]) {
}
}

#ifdef WITH_WEBSERVER
// start webserver if used
if (Config::getWebserverEnabled()) {
int port = Config::getWebserverPort();
Logger::Log(LOG_INFO, "starting webserver on port %d ...", port);
StartWebserver(port, &s_sequencer, s_master_server.IsRegistered(), s_master_server.GetTrustLevel());
Logger::Log(LOG_INFO, "Done");
}
#endif //WITH_WEBSERVER

// start the main program loop
// if we need to communiate to the master user the notifier routine
if (server_mode != SERVER_LAN) {
Expand Down Expand Up @@ -387,6 +401,12 @@ int main(int argc, char *argv[]) {
}

s_sequencer.Close();
#ifdef WITH_WEBSERVER
// start webserver if used
if (Config::getWebserverEnabled()) {
StopWebserver();
}
#endif //WITH_WEBSERVER
return 0;
}

Expand Down
Loading

0 comments on commit c49be64

Please sign in to comment.