diff --git a/lib/mammoth/include/mammoth/channel.h b/lib/mammoth/include/mammoth/channel.h index 90b1271..ba200de 100644 --- a/lib/mammoth/include/mammoth/channel.h +++ b/lib/mammoth/include/mammoth/channel.h @@ -1,7 +1,7 @@ #pragma once #include -#include +#include class Channel { public: @@ -13,6 +13,12 @@ class Channel { z_err_t WriteStr(const char* msg); z_err_t ReadStr(char* buffer, uint64_t* size); + template + z_err_t WriteStruct(T*); + + template + z_err_t ReadStructAndCap(T*, uint64_t*); + // FIXME: Close channel here. ~Channel() {} @@ -21,3 +27,22 @@ class Channel { }; uint64_t CreateChannels(Channel& c1, Channel& c2); + +template +z_err_t Channel::WriteStruct(T* obj) { + return ZChannelSend(chan_cap_, sizeof(T), obj, 0, nullptr); +} + +template +z_err_t Channel::ReadStructAndCap(T* obj, uint64_t* cap) { + uint64_t num_bytes, num_caps; + uint64_t ret = ZChannelRecv(chan_cap_, sizeof(T), obj, /* num_caps= */ 1, cap, + &num_bytes, &num_caps); + if (ret != Z_OK) { + return ret; + } + if (num_caps != 1 || num_bytes != sizeof(T)) { + return Z_ERR_INVALID; + } + return Z_OK; +}