Skip to content

Commit

Permalink
[CHORE] Code cleanup + proper unix socket cleanup on SIGINT
Browse files Browse the repository at this point in the history
  • Loading branch information
nots1dd committed Feb 25, 2025
1 parent a53f3de commit 48a9e39
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 deletions.
8 changes: 5 additions & 3 deletions include/macros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
#include <string>
#include <string_view>

#define TRANSPORT_STREAM_START_BYTE 0x47 // 0x47 (MPEG-TS sync byte)
#define WAVY_SERVER_PORT_NO 8080
#define WAVY_SERVER_PORT_NO_STR "8080"
#define TRANSPORT_STREAM_START_BYTE 0x47 // 0x47 (MPEG-TS sync byte)
#define WAVY_SERVER_PORT_NO 8080
#define WAVY_SERVER_AUDIO_SIZE_LIMIT 200 // in MiBs
#define WAVY_SERVER_PORT_NO_STR "8080"

#define STRING_CONSTANTS(X) \
X(PLAYLIST_EXT, ".m3u8") \
Expand All @@ -28,6 +29,7 @@
X(PLAYLIST_VARIANT_TAG, "#EXT-X-STREAM-INF:") \
X(SERVER_PATH_HLS_CLIENTS, "/hls/clients") \
X(SERVER_LOCK_FILE, "/tmp/hls_server.lock") \
X(NETWORK_TEXT_DELIM, "\r\n\r\n") \
X(SERVER_CERT, "server.crt") \
X(SERVER_PRIVATE_KEY, "server.key") \
X(SERVER_TEMP_STORAGE_DIR, "/tmp/hls_temp") \
Expand Down
50 changes: 33 additions & 17 deletions src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <archive.h>
#include <archive_entry.h>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/signal_set.hpp>
#include <boost/asio/ssl/stream.hpp>
#include <boost/beast.hpp>
#include <boost/filesystem.hpp>
Expand Down Expand Up @@ -358,8 +359,9 @@ class HLS_Session : public std::enable_shared_from_this<HLS_Session>
auto self(shared_from_this());

auto parser = std::make_shared<http::request_parser<http::string_body>>();
parser->body_limit(150 * 1024 * 1024); // for now 100MiB is alright, when lossless codecs come
// in the picture we will have to think about it.
parser->body_limit(WAVY_SERVER_AUDIO_SIZE_LIMIT * 1024 *
1024); // for now 200MiB is alright, when lossless codecs come
// in the picture we will have to think about it.

http::async_read(
socket_, buffer_, *parser,
Expand Down Expand Up @@ -449,12 +451,11 @@ class HLS_Session : public std::enable_shared_from_this<HLS_Session>
if (target == "/toml/upload")
{
// Remove padding text before parsing
std::string body = request_.body();
std::string delimiter = "\r\n\r\n";
auto pos = body.find(delimiter);
std::string body = request_.body();
auto pos = body.find(macros::NETWORK_TEXT_DELIM);
if (pos != std::string::npos)
{
body = body.substr(pos + delimiter.length());
body = body.substr(pos + macros::NETWORK_TEXT_DELIM.length());
}

// Remove bottom padding text
Expand All @@ -470,7 +471,7 @@ class HLS_Session : public std::enable_shared_from_this<HLS_Session>
if (toml_data_opt.path.empty())
{
LOG_ERROR << "[TOML] Failed to parse TOML data";
send_response("HTTP/1.1 400 Bad Request\r\n\r\nTOML parsing failed\r\n");
send_response(macros::to_string(macros::SERVER_ERROR_400));
return;
}

Expand Down Expand Up @@ -625,7 +626,7 @@ class HLS_Session : public std::enable_shared_from_this<HLS_Session>
auto response = std::make_shared<http::response<http::string_body>>();
response->version(request_.version());
response->result(http::status::ok);
response->set(http::field::server, "Wavy-Server");
response->set(http::field::server, "Wavy Server");
response->set(http::field::content_type, content_type);
response->body() = std::move(file_content);
response->prepare_payload();
Expand Down Expand Up @@ -692,25 +693,29 @@ class HLS_Server
public:
HLS_Server(boost::asio::io_context& io_context, boost::asio::ssl::context& ssl_context,
short port)
: acceptor_(io_context, tcp::endpoint(tcp::v4(), port)), ssl_context_(ssl_context)
: acceptor_(io_context, tcp::endpoint(tcp::v4(), port)), ssl_context_(ssl_context),
signals_(io_context, SIGINT, SIGTERM, SIGHUP)
{
ensure_single_instance();
LOG_INFO << "[Server] Starting HLS server on port " << port;

signals_.async_wait(
[this](boost::system::error_code /*ec*/, int /*signo*/)
{
LOG_INFO << "[Server] Termination signal received. Cleaning up...";
cleanup();
std::exit(0);
});

start_accept();
}

~HLS_Server()
{
if (lock_fd_ != -1)
{
close(lock_fd_);
unlink(macros::to_string(macros::SERVER_LOCK_FILE).c_str()); // Remove the lock file
}
}
~HLS_Server() { cleanup(); }

private:
tcp::acceptor acceptor_;
boost::asio::ssl::context& ssl_context_;
boost::asio::signal_set signals_;
int lock_fd_ = -1;

void start_accept()
Expand Down Expand Up @@ -755,6 +760,17 @@ class HLS_Server

LOG_INFO << "[Server] Lock acquired: " << macros::SERVER_LOCK_FILE;
}

void cleanup()
{
if (lock_fd_ != -1)
{
close(lock_fd_);
unlink(macros::to_string(macros::SERVER_LOCK_FILE).c_str());
LOG_INFO << "[Server] Lock file removed: " << macros::SERVER_LOCK_FILE;
lock_fd_ = -1;
}
}
};

auto main() -> int
Expand Down

0 comments on commit 48a9e39

Please sign in to comment.