[Mammoth] Move request/response context to ipc/ folder.
This commit is contained in:
parent
f1cbfd18b7
commit
99a75a4a76
4 changed files with 2 additions and 4 deletions
|
|
@ -5,8 +5,8 @@
|
|||
#include <ztypes.h>
|
||||
|
||||
#include "mammoth/ipc/endpoint_client.h"
|
||||
#include "mammoth/request_context.h"
|
||||
#include "mammoth/response_context.h"
|
||||
#include "mammoth/ipc/request_context.h"
|
||||
#include "mammoth/ipc/response_context.h"
|
||||
#include "mammoth/thread.h"
|
||||
|
||||
class EndpointServer {
|
||||
|
|
|
|||
32
lib/mammoth/ipc/request_context.h
Normal file
32
lib/mammoth/ipc/request_context.h
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
#pragma once
|
||||
|
||||
#include <glacier/status/error.h>
|
||||
#include <stdint.h>
|
||||
|
||||
class RequestContext {
|
||||
public:
|
||||
RequestContext(void* buffer, uint64_t buffer_length)
|
||||
: buffer_(buffer), buffer_length_(buffer_length) {
|
||||
if (buffer_length_ < sizeof(uint64_t)) {
|
||||
request_id_ = -1;
|
||||
} else {
|
||||
request_id_ = *reinterpret_cast<uint64_t*>(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t request_id() { return request_id_; }
|
||||
|
||||
template <typename T>
|
||||
glcr::ErrorCode As(T** arg) {
|
||||
if (buffer_length_ < sizeof(T)) {
|
||||
return glcr::INVALID_ARGUMENT;
|
||||
}
|
||||
*arg = reinterpret_cast<T*>(buffer_);
|
||||
return glcr::OK;
|
||||
}
|
||||
|
||||
private:
|
||||
uint64_t request_id_;
|
||||
void* buffer_;
|
||||
uint64_t buffer_length_;
|
||||
};
|
||||
41
lib/mammoth/ipc/response_context.h
Normal file
41
lib/mammoth/ipc/response_context.h
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
#pragma once
|
||||
|
||||
#include <glacier/status/error.h>
|
||||
#include <zcall.h>
|
||||
#include <ztypes.h>
|
||||
|
||||
class ResponseContext {
|
||||
public:
|
||||
ResponseContext(z_cap_t reply_port) : reply_port_(reply_port) {}
|
||||
|
||||
ResponseContext(ResponseContext&) = delete;
|
||||
|
||||
template <typename T>
|
||||
glcr::ErrorCode WriteStruct(const T& response) {
|
||||
// FIXME: Here and below probably don't count as written on error.
|
||||
written_ = true;
|
||||
return ZReplyPortSend(reply_port_, sizeof(T), &response, 0, nullptr);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
glcr::ErrorCode WriteStructWithCap(const T& response, z_cap_t capability) {
|
||||
written_ = true;
|
||||
return ZReplyPortSend(reply_port_, sizeof(T), &response, 1, &capability);
|
||||
}
|
||||
|
||||
glcr::ErrorCode WriteError(glcr::ErrorCode code) {
|
||||
uint64_t response[2]{
|
||||
static_cast<uint64_t>(-1),
|
||||
code,
|
||||
};
|
||||
written_ = true;
|
||||
return static_cast<glcr::ErrorCode>(
|
||||
ZReplyPortSend(reply_port_, sizeof(response), &response, 0, nullptr));
|
||||
}
|
||||
|
||||
bool HasWritten() { return written_; }
|
||||
|
||||
private:
|
||||
z_cap_t reply_port_;
|
||||
bool written_ = false;
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue