#include "mologram/RemoteService.hpp" #include #include #include #include #include #include "mologram/SocketUtil.hpp" #include "mologram/Wire.hpp" namespace mologram { RemoteService::RemoteService(std::string socketPath) : path_(std::move(socketPath)) {} RemoteService::~RemoteService() { stop_ = true; { std::lock_guard g(mutex_); if (fd_.valid()) ::shutdown(fd_.get(), SHUT_RDWR); } cv_.notify_all(); if (thread_.joinable()) thread_.join(); } void RemoteService::start() { if (!thread_.joinable()) thread_ = std::thread([this]() { loop(); }); } bool RemoteService::waitConnected(const std::function& giveUp) { std::unique_lock lk(mutex_); while (!connected_.load()) { if (giveUp && giveUp()) return false; cv_.wait_for(lk, std::chrono::milliseconds(20)); } return true; } void RemoteService::addSink(IEventSink* s) { std::lock_guard g(sinkMutex_); if (std::find(sinks_.begin(), sinks_.end(), s) == sinks_.end()) sinks_.push_back(s); } void RemoteService::removeSink(IEventSink* s) { std::lock_guard g(sinkMutex_); sinks_.erase(std::remove(sinks_.begin(), sinks_.end(), s), sinks_.end()); } void RemoteService::publish(const char* state) { std::vector copy; { std::lock_guard g(sinkMutex_); copy = sinks_; } js::Object o; o["state"] = js::Value(state); Event e{"connection.state", js::Value(std::move(o))}; for (auto* s : copy) s->onEvent(e); } bool RemoteService::sendFrame(const js::Value& f) { std::lock_guard g(mutex_); if (!connected_.load() || !fd_.valid()) return false; std::string bytes = FrameCodec::encode(f); return SocketUtil::writeAll(fd_.get(), bytes.data(), bytes.size()); } RequestId RemoteService::call(const std::string& method, const js::Value& params, Reply cb) { RequestId id = next_++; { std::lock_guard g(mutex_); pending_[id] = cb; } if (!sendFrame(Wire::request(id, method, params))) { Reply r; { std::lock_guard g(mutex_); auto it = pending_.find(id); if (it != pending_.end()) { r = it->second; pending_.erase(it); } } if (r) r(RpcResult::failure(ErrorCode::Disconnected, "not connected to the daemon")); } return id; } void RemoteService::cancel(RequestId id) { sendFrame(Wire::cancel(id)); } void RemoteService::failPending(const char* text) { std::map copy; { std::lock_guard g(mutex_); copy.swap(pending_); } for (auto& kv : copy) kv.second(RpcResult::failure(ErrorCode::Disconnected, text)); } void RemoteService::loop() { while (!stop_.load()) { int e = 0; Fd fd = SocketUtil::connectUnix(path_, e); if (!fd.valid()) { for (int i = 0; i < 50 && !stop_.load(); i++) std::this_thread::sleep_for(std::chrono::milliseconds(20)); // retry every 1 s continue; } int raw = fd.get(); { std::lock_guard g(mutex_); fd_ = std::move(fd); connected_ = true; } cv_.notify_all(); reconnects_++; publish("connected"); FrameReader reader; char buf[65536]; bool ok = true; while (ok && !stop_.load()) { pollfd p{raw, POLLIN, 0}; int rc = ::poll(&p, 1, 200); if (rc < 0 && errno != EINTR) break; if (rc <= 0) continue; ssize_t n = ::recv(raw, buf, sizeof(buf), 0); if (n <= 0) break; reader.feed(buf, (size_t)n); for (;;) { js::Value frame; auto st = reader.next(frame); if (st == FrameReader::Status::NeedMore) break; if (st == FrameReader::Status::BadFrame) { ok = false; break; } Wire::Incoming in; RpcError err; if (!Wire::parse(frame, in, err)) { ok = false; break; } if (in.kind == Wire::Kind::Response) { Reply r; { std::lock_guard g(mutex_); auto it = pending_.find(in.id); if (it != pending_.end()) { r = it->second; pending_.erase(it); } } if (r) r(in.result); } else if (in.kind == Wire::Kind::Event) { std::vector copy; { std::lock_guard g(sinkMutex_); copy = sinks_; } for (auto* s : copy) s->onEvent(in.event); } } } { std::lock_guard g(mutex_); connected_ = false; fd_.reset(); } failPending("connection to the daemon was lost"); if (!stop_.load()) publish("disconnected"); } } } // namespace mologram