Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions src/protocol/util/AsyncEventServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,34 @@ struct AsyncEventServer::implementation : public spl::enable_shared_from_this<im

implementation(std::shared_ptr<boost::asio::io_context> io_context,
const protocol_strategy_factory<char>::ptr& protocol,
const std::string& host,
unsigned short port)
: io_context_(std::move(io_context))
, acceptor_(*io_context_, tcp::endpoint(tcp::v4(), port))
, acceptor_([&]() {
boost::asio::ip::tcp::endpoint endpoint;
if (host.empty()) {
endpoint = boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port);
} else {
try {
auto addr = boost::asio::ip::make_address(host);
// Only allow IPv4 to avoid crashes
if (addr.is_v6()) {
CASPAR_LOG(fatal) << "IPv6 addresses are not supported for host: " << host;
throw std::runtime_error("IPv6 not supported");
}
endpoint = boost::asio::ip::tcp::endpoint(addr, port);
} catch (const std::exception& e) {
CASPAR_LOG(fatal) << "Invalid host address: " << host << " - " << e.what();
throw;
}
}
tcp::acceptor a(*io_context_);
a.open(endpoint.protocol());
a.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
a.bind(endpoint);
a.listen();
return a;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a reason for this constructor being more verbose than before?
Previously it was doing acceptor_(*io_context_, tcp::endpoint(tcp::v4(), port)), so it looks like this could be done with something along the lines of return tcp::acceptor(*io_context_, endpoint); instead?

}())
, protocol_factory_(protocol)
{
}
Expand Down Expand Up @@ -353,7 +378,13 @@ struct AsyncEventServer::implementation : public spl::enable_shared_from_this<im
AsyncEventServer::AsyncEventServer(std::shared_ptr<boost::asio::io_context> io_context,
const protocol_strategy_factory<char>::ptr& protocol,
unsigned short port)
: impl_(new implementation(std::move(io_context), protocol, port))
: AsyncEventServer(io_context, protocol, "", port) {}

AsyncEventServer::AsyncEventServer(std::shared_ptr<boost::asio::io_context> io_context,
const protocol_strategy_factory<char>::ptr& protocol,
const std::string& host,
unsigned short port)
: impl_(new implementation(std::move(io_context), protocol, host, port))
{
impl_->start_accept();
}
Expand Down
6 changes: 6 additions & 0 deletions src/protocol/util/AsyncEventServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ class AsyncEventServer
public:
explicit AsyncEventServer(std::shared_ptr<boost::asio::io_context> io_context,
const protocol_strategy_factory<char>::ptr& protocol,
const std::string& host,
unsigned short port);

AsyncEventServer(std::shared_ptr<boost::asio::io_context> io_context,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think this constructor without the host is being used or is helpful, and could be removed

const protocol_strategy_factory<char>::ptr& protocol,
unsigned short port);

~AsyncEventServer();

void add_client_lifecycle_object_factory(const lifecycle_factory_t& lifecycle_factory);
Expand Down
3 changes: 3 additions & 0 deletions src/shell/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,11 +438,14 @@ struct server::impl

if (name == L"tcp") {
auto port = ptree_get<unsigned int>(xml_controller.second, L"port");
std::wstring host_w = xml_controller.second.get(L"host", L"");
auto host_utf8 = u8(host_w);

try {
auto asyncbootstrapper = spl::make_shared<IO::AsyncEventServer>(
io_context_,
create_protocol(protocol, L"TCP Port " + std::to_wstring(port)),
host_utf8,
static_cast<short>(port));
async_servers_.push_back(asyncbootstrapper);

Expand Down