diff --git a/lib/mammoth/include/mammoth/endpoint_server.h b/lib/mammoth/include/mammoth/endpoint_server.h index 7224d11..79260e3 100644 --- a/lib/mammoth/include/mammoth/endpoint_server.h +++ b/lib/mammoth/include/mammoth/endpoint_server.h @@ -7,6 +7,7 @@ #include "mammoth/endpoint_client.h" #include "mammoth/request_context.h" #include "mammoth/response_context.h" +#include "mammoth/thread.h" class EndpointServer { public: @@ -15,13 +16,8 @@ class EndpointServer { EndpointServer& operator=(const EndpointServer&) = delete; glcr::ErrorOr> CreateClient(); - // FIXME: Release Cap here. - z_cap_t GetCap() { return endpoint_cap_; } - glcr::ErrorCode Receive(uint64_t* num_bytes, void* data, - z_cap_t* reply_port_cap); - - glcr::ErrorCode RunServer(); + Thread RunServer(); virtual glcr::ErrorCode HandleRequest(RequestContext& request, ResponseContext& response) = 0; @@ -34,4 +30,7 @@ class EndpointServer { static const uint64_t kBufferSize = 1024; uint8_t recieve_buffer_[kBufferSize]; + + friend void EndpointServerThreadBootstrap(void* endpoint_server); + void ServerThread(); }; diff --git a/lib/mammoth/src/endpoint_server.cpp b/lib/mammoth/src/endpoint_server.cpp index 42f5ecd..8a6dfce 100644 --- a/lib/mammoth/src/endpoint_server.cpp +++ b/lib/mammoth/src/endpoint_server.cpp @@ -2,6 +2,11 @@ #include "mammoth/debug.h" +// Declared as friend in EndpointServer. +void EndpointServerThreadBootstrap(void* endpoint_server) { + reinterpret_cast(endpoint_server)->ServerThread(); +} + glcr::ErrorOr> EndpointServer::CreateClient() { uint64_t client_cap; // FIXME: Restrict permissions to send-only here. @@ -9,21 +14,25 @@ glcr::ErrorOr> EndpointServer::CreateClient() { return EndpointClient::AdoptEndpoint(client_cap); } -glcr::ErrorCode EndpointServer::Receive(uint64_t* num_bytes, void* data, - z_cap_t* reply_port_cap) { - return ZEndpointRecv(endpoint_cap_, num_bytes, data, reply_port_cap); +Thread EndpointServer::RunServer() { + return Thread(EndpointServerThreadBootstrap, this); } -glcr::ErrorCode EndpointServer::RunServer() { +void EndpointServer::ServerThread() { while (true) { uint64_t message_size = kBufferSize; uint64_t reply_port_cap = 0; - RET_ERR(Receive(&message_size, recieve_buffer_, &reply_port_cap)); + glcr::ErrorCode err = ZEndpointRecv(endpoint_cap_, &message_size, + recieve_buffer_, &reply_port_cap); + if (err != glcr::OK) { + dbgln("Error in receive: %x", err); + continue; + } RequestContext request(recieve_buffer_, message_size); ResponseContext response(reply_port_cap); // FIXME: Consider pumping these errors into the response as well. - RET_ERR(HandleRequest(request, response)); + check(HandleRequest(request, response)); if (!response.HasWritten()) { dbgln("Returning without having written a response. Req type %x", request.request_id()); diff --git a/sys/denali/denali.cpp b/sys/denali/denali.cpp index 043de69..a7ba4e7 100644 --- a/sys/denali/denali.cpp +++ b/sys/denali/denali.cpp @@ -26,7 +26,8 @@ uint64_t main(uint64_t init_port_cap) { server->CreateClient()); check(stub.Register("denali", *client)); - RET_ERR(server->RunServer()); - // FIXME: Add thread join. + Thread server_thread = server->RunServer(); + + check(server_thread.Join()); return 0; } diff --git a/sys/yellowstone/yellowstone.cpp b/sys/yellowstone/yellowstone.cpp index d7ec505..6db5faf 100644 --- a/sys/yellowstone/yellowstone.cpp +++ b/sys/yellowstone/yellowstone.cpp @@ -14,6 +14,7 @@ uint64_t main(uint64_t port_cap) { ASSIGN_OR_RETURN(auto server, YellowstoneServer::Create()); Thread registration_thread = server->RunRegistration(); + Thread server_thread = server->RunServer(); uint64_t vaddr; check(ZAddressSpaceMap(gSelfVmasCap, 0, gBootDenaliVmmoCap, &vaddr)); @@ -21,7 +22,7 @@ uint64_t main(uint64_t port_cap) { server->CreateClient()); check(SpawnProcessFromElfRegion(vaddr, glcr::Move(client))); - check(server->RunServer()); + check(server_thread.Join()); check(registration_thread.Join()); dbgln("Yellowstone Finished Successfully."); return 0;