Skip to content

Commit

Permalink
feat: dbus error support
Browse files Browse the repository at this point in the history
  • Loading branch information
zsien committed Apr 7, 2021
1 parent cd1b17d commit 3e0cdcf
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 9 deletions.
6 changes: 6 additions & 0 deletions example/example.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "example.h"

#include <dbusx/method.h>
#include <dbusx/error.h>

example::example() {
}
Expand All @@ -9,6 +10,10 @@ uint32_t example::string_length(const std::string &str) {
return str.length();
}

int32_t example::always_error(int32_t) {
throw dbusx::error("org.freedesktop.DBus.Error.FileNotFound", "error message");
}

std::string example::interface_name() {
return "cn.zsien.dbusx.example.control";
}
Expand All @@ -18,6 +23,7 @@ dbusx::vtable::vtable example::exported() {
.methods =
{
{"stringLength", dbusx::method<&example::string_length>::get_vtable()},
{"alwaysError", dbusx::method<&example::always_error>::get_vtable()},
},
.properties = {},
.signals = {},
Expand Down
3 changes: 2 additions & 1 deletion example/example.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class example : public dbusx::interface {
public:
example();

uint32_t string_length(const std::string &str);
uint32_t string_length(const std::string &str);
int32_t always_error(int32_t);

virtual std::string interface_name() override;
virtual dbusx::vtable::vtable exported() override;
Expand Down
26 changes: 26 additions & 0 deletions include/dbusx/error.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef DBUSX_ERROR_H
#define DBUSX_ERROR_H

#include <exception>
#include <string>

namespace dbusx {

class error : public std::exception {
friend class message;

public:
error(const std::string &name, const std::string &message)
: name_(name)
, message_(message) {}

const char *what() const noexcept override { return message_.c_str(); }

private:
const std::string name_;
const std::string message_;
};

} // namespace dbusx

#endif //! DBUSX_ERROR_H
3 changes: 3 additions & 0 deletions include/dbusx/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace dbusx {

class message_private;

class error;

class message {
public:
enum class type {
Expand Down Expand Up @@ -64,6 +66,7 @@ class message {
message &operator=(message &&r) noexcept;

message create_return() const;
message create_error(const error &error) const;

std::string get_destination() const;
std::string get_path() const;
Expand Down
24 changes: 16 additions & 8 deletions include/dbusx/method.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "message.h"
#include "signature.h"
#include "vtable.h"
#include "error.h"

namespace dbusx {

Expand Down Expand Up @@ -54,15 +55,22 @@ struct method<F> {
}

static void invoke(interface *o, const message &m) {
C *obj = reinterpret_cast<C *>(o);
OUT r = (obj->*F)(m.read<IN>()...);
// TODO: out
try {
C *obj = reinterpret_cast<C *>(o);
OUT r = (obj->*F)(m.read<IN>()...);
// TODO: out
(void)r;

message ret = m.create_return();
ret.append(r);
bool a = ret.send();
(void)a;
// m.reply(r);
message ret = m.create_return();
ret.append(r);
bool a = ret.send();
(void)a;
// m.reply(r);
} catch (const dbusx::error &e) {
message ret = m.create_error(e);
bool a = ret.send();
(void)a;
}
}
};

Expand Down
17 changes: 17 additions & 0 deletions src/sdbus/message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "message_private.h"
#include "dbusx/signature.h"
#include "dbusx/error.h"

using namespace dbusx;

Expand All @@ -28,6 +29,22 @@ message message::create_return() const {
return msg;
}

message message::create_error(const error &err) const {
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wc99-extensions"
#endif
auto e = SD_BUS_ERROR_MAKE_CONST(err.name_.c_str(), err.message_.c_str());
#ifdef __clang__
#pragma clang diagnostic pop
#endif

message msg;
sd_bus_message_new_method_error(d_ptr_->message_, &msg.d_ptr_->message_, &e);

return msg;
}

std::string message::get_destination() const {
return sd_bus_message_get_destination(d_ptr_->message_);
}
Expand Down

0 comments on commit 3e0cdcf

Please sign in to comment.