Skip to content

Commit c07d672

Browse files
committed
Close and reuse websocket port correctly
1 parent 2164c6f commit c07d672

File tree

4 files changed

+24
-7
lines changed

4 files changed

+24
-7
lines changed

deploy/lib/systemd/system/dbus2http.service

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Requires=dbus.service
99
[Service]
1010
User=dbus2http
1111
WorkingDirectory=/opt/cosmos/bin
12-
ExecStart=/opt/cosmos/bin/dbus2http --system --port 10059 --websocket_port 10058 --service_prefix com org.freedesktop.DBus
12+
ExecStart=/opt/cosmos/bin/dbus2http --system --port 10059 --websocket_port 10058 --service_prefix com
1313
Type=simple
1414
EnvironmentFile=/etc/environment
1515
Restart=always

include/dbus2http/SignalSocket.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,26 @@ class SignalSocket {
5151
SignalSocket(const InterfaceContext& context, int port, Config config);
5252

5353
void start() {
54-
ws_server_thread_ = std::thread([&]() { ws_server_.run(); });
54+
ws_server_thread_ = std::thread([this]() { ws_server_.run(); });
5555
dbus_session_thread_ =
5656
std::thread([&]() { dbus_connection_->enterEventLoop(); });
5757
}
5858

5959
void stop() {
6060
ws_server_.stop_listening();
61+
62+
for (const auto& [conn_hdl, slot] : conn2slot_) {
63+
websocketpp::lib::error_code ec;
64+
ws_server_.close(conn_hdl, websocketpp::close::status::going_away,
65+
"server stopping", ec);
66+
if (ec)
67+
PLOGE << "Error closing connection: " << ec.message();
68+
}
69+
conn2slot_.clear();
6170
ws_server_.stop();
71+
6272
if (ws_server_thread_.joinable()) ws_server_thread_.join();
73+
6374
dbus_connection_->leaveEventLoop();
6475
if (dbus_session_thread_.joinable()) dbus_session_thread_.join();
6576
}

src/SignalSocket.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ namespace dbus2http {
88

99
using websocketpp::log::alevel;
1010

11-
SignalSocket::SignalSocket(const InterfaceContext& context, int port, Config config)
11+
SignalSocket::SignalSocket(const InterfaceContext& context, int port,
12+
Config config)
1213
: context_(context), config_(config) {
1314
ws_server_.init_asio();
15+
ws_server_.set_reuse_addr(true);
1416
ws_server_.set_open_handler([&](auto conn_hdl) {
1517
websocketpp::lib::error_code ec;
16-
server::connection_ptr ws_conn = ws_server_.get_con_from_hdl(conn_hdl, ec);
18+
const server::connection_ptr ws_conn =
19+
ws_server_.get_con_from_hdl(conn_hdl, ec);
1720
if (ec) {
1821
PLOGE << "get_con_from_hdl error: " << ec.message();
1922
return;
@@ -32,15 +35,14 @@ SignalSocket::SignalSocket(const InterfaceContext& context, int port, Config con
3235
match = replaceAll(match, "%20", " ");
3336
match = replaceAll(match, "%27", "'");
3437
match = replaceAll(match, "%2C", ",");
35-
match = replaceAll(match, "%3D", "=");
3638
match = replaceAll(match, "%2F", "/");
3739
match = replaceAll(match, "%3A", ":");
3840
match = replaceAll(match, "%3D", "=");
3941
PLOGI << "match : " << match;
4042
try {
4143
conn2slot_[conn_hdl] = dbus_connection_->addMatch(
4244
match,
43-
[&, conn_hdl](sdbus::Message msg) {
45+
[this, &conn_hdl](sdbus::Message msg) {
4446
PLOGD << "get message from service: " << msg.getInterfaceName()
4547
<< " member: " << msg.getMemberName();
4648
std::vector<Argument> args;
@@ -59,6 +61,7 @@ SignalSocket::SignalSocket(const InterfaceContext& context, int port, Config con
5961

6062
nlohmann::json j = Message2Json::WrapHeader(
6163
msg, message2json.ExtractMessage(msg, args));
64+
websocketpp::lib::error_code ec;
6265
server::connection_ptr conn =
6366
ws_server_.get_con_from_hdl(conn_hdl, ec);
6467
if (conn) {
@@ -71,6 +74,8 @@ SignalSocket::SignalSocket(const InterfaceContext& context, int port, Config con
7174
},
7275
sdbus::return_slot_t());
7376
} catch (const std::exception& e) {
77+
ws_server_.close(conn_hdl, websocketpp::close::status::policy_violation,
78+
"add match failed: " + std::string(e.what()));
7479
PLOGE << "add match failed: " << e.what();
7580
}
7681
});
@@ -102,7 +107,7 @@ SignalSocket::SignalSocket(const InterfaceContext& context, int port, Config con
102107
ws_server_.set_access_channels(alevel::all);
103108
ws_server_.set_error_channels(alevel::all);
104109
#endif
105-
dbus_connection_ = DbusUtils::createConnection(system);
110+
dbus_connection_ = DbusUtils::createConnection(config.system_bus);
106111
}
107112

108113
void SignalSocket::on_message(server* s, websocketpp::connection_hdl conn_hdl,

src/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ int main(int argc, char* argv[]) {
111111
PLOGI << "revision: " << REVISION;
112112
PLOGI << "build time: " << std::string(__DATE__ " " __TIME__);
113113
PLOGI << "toolchain: " << std::string(TOOLCHAIN);
114+
PLOGI << "verbose: " << (program.get<bool>("--verbose") ? "true" : "false");
114115

115116
#ifdef NDEBUG
116117
PLOGI << "build mode: release";

0 commit comments

Comments
 (0)