Add struct reading from channels

This commit is contained in:
Drew Galbraith 2023-06-19 21:47:23 -07:00
parent a15ab24d9b
commit ec915338dc
1 changed files with 26 additions and 1 deletions

View File

@ -1,7 +1,7 @@
#pragma once #pragma once
#include <stdint.h> #include <stdint.h>
#include <ztypes.h> #include <zcall.h>
class Channel { class Channel {
public: public:
@ -13,6 +13,12 @@ class Channel {
z_err_t WriteStr(const char* msg); z_err_t WriteStr(const char* msg);
z_err_t ReadStr(char* buffer, uint64_t* size); z_err_t ReadStr(char* buffer, uint64_t* size);
template <typename T>
z_err_t WriteStruct(T*);
template <typename T>
z_err_t ReadStructAndCap(T*, uint64_t*);
// FIXME: Close channel here. // FIXME: Close channel here.
~Channel() {} ~Channel() {}
@ -21,3 +27,22 @@ class Channel {
}; };
uint64_t CreateChannels(Channel& c1, Channel& c2); uint64_t CreateChannels(Channel& c1, Channel& c2);
template <typename T>
z_err_t Channel::WriteStruct(T* obj) {
return ZChannelSend(chan_cap_, sizeof(T), obj, 0, nullptr);
}
template <typename T>
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;
}