acadia/lib/mammoth/ipc/channel.h

48 lines
997 B
C
Raw Normal View History

#pragma once
2023-06-21 18:28:54 -07:00
#include <glacier/status/error.h>
#include <stdint.h>
2023-06-19 21:47:23 -07:00
#include <zcall.h>
class Channel {
public:
Channel() {}
void adopt_cap(uint64_t id);
z_cap_t release_cap();
z_cap_t cap();
z_err_t WriteStr(const char* msg);
z_err_t ReadStr(char* buffer, uint64_t* size);
2023-06-19 21:47:23 -07:00
template <typename T>
z_err_t WriteStruct(T*);
template <typename T>
z_err_t ReadStructAndCap(T*, uint64_t*);
// FIXME: Close channel here.
~Channel() {}
private:
z_cap_t chan_cap_ = 0;
};
uint64_t CreateChannels(Channel& c1, Channel& c2);
2023-06-19 21:47:23 -07:00
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 = sizeof(T);
uint64_t num_caps = 1;
2023-06-21 18:28:54 -07:00
RET_ERR(ZChannelRecv(chan_cap_, &num_bytes, obj, &num_caps, cap));
2023-06-19 21:47:23 -07:00
if (num_caps != 1 || num_bytes != sizeof(T)) {
2023-06-21 18:28:54 -07:00
return glcr::FAILED_PRECONDITION;
2023-06-19 21:47:23 -07:00
}
2023-06-21 18:28:54 -07:00
return glcr::OK;
2023-06-19 21:47:23 -07:00
}