145 lines
4.5 KiB
Django/Jinja
145 lines
4.5 KiB
Django/Jinja
// Generated file -- DO NOT MODIFY.
|
|
#include "{{file}}.server.h"
|
|
|
|
#include <mammoth/util/debug.h>
|
|
#include <zcall.h>
|
|
|
|
{% if package != None %}
|
|
namespace {{package.cpp_namespace()}} {
|
|
{% endif %}
|
|
namespace {
|
|
|
|
const uint32_t kSentinel = 0xBEEFDEAD;
|
|
const uint32_t kHeaderSize = 0x10;
|
|
|
|
void WriteError(glcr::ByteBuffer& buffer, glcr::ErrorCode err) {
|
|
buffer.WriteAt<uint32_t>(0, kSentinel);
|
|
buffer.WriteAt<uint32_t>(4, kHeaderSize);
|
|
buffer.WriteAt<uint64_t>(8, err);
|
|
}
|
|
|
|
void WriteHeader(glcr::ByteBuffer& buffer, uint64_t message_length) {
|
|
buffer.WriteAt<uint32_t>(0, kSentinel);
|
|
buffer.WriteAt<uint32_t>(4, kHeaderSize + message_length);
|
|
buffer.WriteAt<uint64_t>(8, glcr::OK);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
{% for interface in interfaces %}
|
|
void {{interface.name}}ServerBaseThreadBootstrap(void* server_base) {
|
|
(({{interface.name}}ServerBase*)server_base)->ServerThread();
|
|
}
|
|
|
|
{{interface.name}}ServerBase::~{{interface.name}}ServerBase() {
|
|
if (endpoint_ != 0) {
|
|
check(ZCapRelease(endpoint_));
|
|
}
|
|
}
|
|
|
|
glcr::ErrorOr<z_cap_t> {{interface.name}}ServerBase::CreateClientCap() {
|
|
uint64_t client_cap;
|
|
RET_ERR(ZCapDuplicate(endpoint_, ~(kZionPerm_Read), &client_cap));
|
|
return client_cap;
|
|
}
|
|
|
|
glcr::ErrorOr<{{interface.name}}Client> {{interface.name}}ServerBase::CreateClient() {
|
|
uint64_t client_cap;
|
|
RET_ERR(ZCapDuplicate(endpoint_, ~(kZionPerm_Read), &client_cap));
|
|
return {{interface.name}}Client(client_cap);
|
|
}
|
|
|
|
Thread {{interface.name}}ServerBase::RunServer() {
|
|
return Thread({{interface.name}}ServerBaseThreadBootstrap, this);
|
|
}
|
|
|
|
void {{interface.name}}ServerBase::ServerThread() {
|
|
glcr::ByteBuffer recv_buffer(0x1000);
|
|
glcr::CapBuffer recv_cap(0x10);
|
|
glcr::ByteBuffer resp_buffer(0x1000);
|
|
glcr::CapBuffer resp_cap(0x10);
|
|
z_cap_t reply_port_cap;
|
|
|
|
while (true) {
|
|
uint64_t recv_cap_size = 0x10;
|
|
uint64_t recv_buf_size = 0x1000;
|
|
recv_cap.Reset();
|
|
glcr::ErrorCode recv_err = static_cast<glcr::ErrorCode>(ZEndpointRecv(endpoint_, &recv_buf_size, recv_buffer.RawPtr(), &recv_cap_size, recv_cap.RawPtr(), &reply_port_cap));
|
|
if (recv_err != glcr::OK) {
|
|
dbgln("Error in receive: {x}", recv_err);
|
|
continue;
|
|
}
|
|
|
|
uint64_t resp_length = 0;
|
|
|
|
glcr::ErrorCode reply_err = glcr::OK;
|
|
resp_cap.Reset();
|
|
glcr::Status err = HandleRequest(recv_buffer, recv_cap, resp_buffer, resp_length, resp_cap);
|
|
if (!err) {
|
|
WriteError(resp_buffer, err.code());
|
|
dbgln("Responding Error {}", err.message());
|
|
reply_err = static_cast<glcr::ErrorCode>(ZReplyPortSend(reply_port_cap, kHeaderSize, resp_buffer.RawPtr(), 0, nullptr));
|
|
} else {
|
|
WriteHeader(resp_buffer, resp_length);
|
|
reply_err = static_cast<glcr::ErrorCode>(ZReplyPortSend(reply_port_cap, kHeaderSize + resp_length, resp_buffer.RawPtr(), resp_cap.UsedSlots(), resp_cap.RawPtr()));
|
|
}
|
|
if (reply_err != glcr::OK) {
|
|
dbgln("Error in reply: {x}", reply_err);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
glcr::Status {{interface.name}}ServerBase::HandleRequest(const glcr::ByteBuffer& request,
|
|
const glcr::CapBuffer& req_caps,
|
|
glcr::ByteBuffer& response, uint64_t& resp_length,
|
|
glcr::CapBuffer& resp_caps) {
|
|
if (request.At<uint32_t>(0) != kSentinel) {
|
|
return glcr::InvalidArgument("Request Not Valid");
|
|
}
|
|
|
|
uint64_t method_select = request.At<uint64_t>(8);
|
|
|
|
switch(method_select) {
|
|
{%- for method in interface.methods %}
|
|
case {{method.number}}: {
|
|
|
|
{% if method.request != None %}
|
|
{{method.request}} yunq_request;
|
|
yunq::MessageView request_view(request, kHeaderSize);
|
|
RETURN_ERROR(yunq_request.ParseFromBytes(request_view, req_caps));
|
|
{% endif %}
|
|
|
|
{% if method.response != None %}
|
|
{{method.response}} yunq_response;
|
|
{% endif %}
|
|
|
|
{% if method.request == None %}
|
|
RETURN_ERROR(Handle{{method.name}}(yunq_response));
|
|
{% elif method.response == None %}
|
|
RETURN_ERROR(Handle{{method.name}}(yunq_request));
|
|
{% else %}
|
|
RETURN_ERROR(Handle{{method.name}}(yunq_request, yunq_response));
|
|
{% endif %}
|
|
|
|
{% if method.response != None %}
|
|
resp_length = yunq_response.SerializeToBytes(response, kHeaderSize, resp_caps);
|
|
{% else %}
|
|
resp_length = 0;
|
|
{% endif %}
|
|
break;
|
|
}
|
|
{%- endfor %}
|
|
default: {
|
|
return glcr::Unimplemented("Method unimplemented by server.");
|
|
}
|
|
}
|
|
return glcr::Status::Ok();
|
|
}
|
|
{% endfor %}
|
|
|
|
{% if package != None %}
|
|
} // namepace {{package.cpp_namespace()}}
|
|
{% endif %}
|