2023-06-21 21:26:24 -07:00
|
|
|
#include "mammoth/endpoint_server.h"
|
|
|
|
|
2023-08-01 15:52:08 -07:00
|
|
|
#include "mammoth/debug.h"
|
2023-06-21 23:14:42 -07:00
|
|
|
|
2023-08-01 16:08:34 -07:00
|
|
|
// Declared as friend in EndpointServer.
|
|
|
|
void EndpointServerThreadBootstrap(void* endpoint_server) {
|
|
|
|
reinterpret_cast<EndpointServer*>(endpoint_server)->ServerThread();
|
|
|
|
}
|
|
|
|
|
2023-06-26 11:54:36 -07:00
|
|
|
glcr::ErrorOr<glcr::UniquePtr<EndpointClient>> EndpointServer::CreateClient() {
|
2023-06-21 21:26:24 -07:00
|
|
|
uint64_t client_cap;
|
2023-11-02 22:12:55 -07:00
|
|
|
RET_ERR(ZCapDuplicate(endpoint_cap_, ~(kZionPerm_Read), &client_cap));
|
2023-06-21 21:26:24 -07:00
|
|
|
return EndpointClient::AdoptEndpoint(client_cap);
|
|
|
|
}
|
2023-06-21 23:14:42 -07:00
|
|
|
|
2023-08-01 16:08:34 -07:00
|
|
|
Thread EndpointServer::RunServer() {
|
|
|
|
return Thread(EndpointServerThreadBootstrap, this);
|
2023-06-21 23:14:42 -07:00
|
|
|
}
|
2023-08-01 15:52:08 -07:00
|
|
|
|
2023-08-01 16:08:34 -07:00
|
|
|
void EndpointServer::ServerThread() {
|
2023-08-01 15:52:08 -07:00
|
|
|
while (true) {
|
|
|
|
uint64_t message_size = kBufferSize;
|
|
|
|
uint64_t reply_port_cap = 0;
|
2023-10-24 23:32:05 -07:00
|
|
|
uint64_t num_caps = 0;
|
2023-11-02 21:55:12 -07:00
|
|
|
glcr::ErrorCode err = static_cast<glcr::ErrorCode>(
|
2023-10-24 23:32:05 -07:00
|
|
|
ZEndpointRecv(endpoint_cap_, &message_size, recieve_buffer_, &num_caps,
|
2023-11-02 21:55:12 -07:00
|
|
|
nullptr, &reply_port_cap));
|
2023-08-01 16:08:34 -07:00
|
|
|
if (err != glcr::OK) {
|
|
|
|
dbgln("Error in receive: %x", err);
|
|
|
|
continue;
|
|
|
|
}
|
2023-08-01 15:52:08 -07:00
|
|
|
|
|
|
|
RequestContext request(recieve_buffer_, message_size);
|
|
|
|
ResponseContext response(reply_port_cap);
|
|
|
|
// FIXME: Consider pumping these errors into the response as well.
|
2023-08-01 16:08:34 -07:00
|
|
|
check(HandleRequest(request, response));
|
2023-08-01 15:52:08 -07:00
|
|
|
if (!response.HasWritten()) {
|
|
|
|
dbgln("Returning without having written a response. Req type %x",
|
|
|
|
request.request_id());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|