Skip to content

Commit

Permalink
fix(tray): wait for tray exit
Browse files Browse the repository at this point in the history
  • Loading branch information
ArcticLampyrid committed Oct 2, 2024
1 parent b9d877b commit eec0568
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/Infrastructure/IPC/SocketClient.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include <Infrastructure/IPC/SocketClient.h>
#include <boost/asio.hpp>
#include <boost/dll.hpp>
#include <boost/process.hpp>
#include <boost/system.hpp>
#include <spdlog/spdlog.h>

Expand Down Expand Up @@ -33,7 +32,7 @@ void SocketClient::startTray() {
#endif

std::error_code ec;
bp::child tray(trayPath, bp::std_out > pipe, bp::std_err > bp::null, ec);
bp::child tray(trayPath, bp::std_in<bp::close, bp::std_out> pipe, bp::std_err > bp::null, ec);
if (ec) {
spdlog::error("Failed to start tray: file = {}, reason = {}",
trayPath.string(),
Expand All @@ -43,22 +42,28 @@ void SocketClient::startTray() {

std::string line;
if (!pipe || !std::getline(pipe, line) || line.empty()) {
spdlog::error("Failed to start tray: file = {}, reason = {}", trayPath.string(), "no port");
return;
}
_tray = std::move(tray);

spdlog::info("Tray started at: {}", line);

net::co_spawn(executor()->getIoContext(),
connect(std::strtol(line.c_str(), nullptr, 10)),
net::detached);

tray.detach();
}

void SocketClient::exitTray() {
std::error_code ec;
if (_socket) {
net::co_spawn(_socket->get_executor(), send("EXIT"), net::detached);
_tray.wait(ec);
} else {
_tray.terminate(ec);
}
auto exit_code = _tray.exit_code();
spdlog::info("Tray exited with code: {}", exit_code);
}

void SocketClient::showOrUpdateMessage(int messageId,
Expand Down
3 changes: 3 additions & 0 deletions src/Infrastructure/IPC/SocketClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <boost/asio/awaitable.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/process.hpp>
#include <chrono>
#include <functional>
#include <unordered_map>
Expand Down Expand Up @@ -34,6 +35,8 @@ class SocketClient {
inline static SocketClient* _instance = nullptr;

private:
boost::process::child _tray;

net::awaitable<void> handleReceive(std::string const& message);

net::awaitable<void> connect(std::uint16_t port);
Expand Down

0 comments on commit eec0568

Please sign in to comment.