[mammoth] Run EndpointServer in its own thread.
This commit is contained in:
parent
caccb08e16
commit
c70b5b0753
|
@ -7,6 +7,7 @@
|
||||||
#include "mammoth/endpoint_client.h"
|
#include "mammoth/endpoint_client.h"
|
||||||
#include "mammoth/request_context.h"
|
#include "mammoth/request_context.h"
|
||||||
#include "mammoth/response_context.h"
|
#include "mammoth/response_context.h"
|
||||||
|
#include "mammoth/thread.h"
|
||||||
|
|
||||||
class EndpointServer {
|
class EndpointServer {
|
||||||
public:
|
public:
|
||||||
|
@ -15,13 +16,8 @@ class EndpointServer {
|
||||||
EndpointServer& operator=(const EndpointServer&) = delete;
|
EndpointServer& operator=(const EndpointServer&) = delete;
|
||||||
|
|
||||||
glcr::ErrorOr<glcr::UniquePtr<EndpointClient>> CreateClient();
|
glcr::ErrorOr<glcr::UniquePtr<EndpointClient>> CreateClient();
|
||||||
// FIXME: Release Cap here.
|
|
||||||
z_cap_t GetCap() { return endpoint_cap_; }
|
|
||||||
|
|
||||||
glcr::ErrorCode Receive(uint64_t* num_bytes, void* data,
|
Thread RunServer();
|
||||||
z_cap_t* reply_port_cap);
|
|
||||||
|
|
||||||
glcr::ErrorCode RunServer();
|
|
||||||
|
|
||||||
virtual glcr::ErrorCode HandleRequest(RequestContext& request,
|
virtual glcr::ErrorCode HandleRequest(RequestContext& request,
|
||||||
ResponseContext& response) = 0;
|
ResponseContext& response) = 0;
|
||||||
|
@ -34,4 +30,7 @@ class EndpointServer {
|
||||||
|
|
||||||
static const uint64_t kBufferSize = 1024;
|
static const uint64_t kBufferSize = 1024;
|
||||||
uint8_t recieve_buffer_[kBufferSize];
|
uint8_t recieve_buffer_[kBufferSize];
|
||||||
|
|
||||||
|
friend void EndpointServerThreadBootstrap(void* endpoint_server);
|
||||||
|
void ServerThread();
|
||||||
};
|
};
|
||||||
|
|
|
@ -2,6 +2,11 @@
|
||||||
|
|
||||||
#include "mammoth/debug.h"
|
#include "mammoth/debug.h"
|
||||||
|
|
||||||
|
// Declared as friend in EndpointServer.
|
||||||
|
void EndpointServerThreadBootstrap(void* endpoint_server) {
|
||||||
|
reinterpret_cast<EndpointServer*>(endpoint_server)->ServerThread();
|
||||||
|
}
|
||||||
|
|
||||||
glcr::ErrorOr<glcr::UniquePtr<EndpointClient>> EndpointServer::CreateClient() {
|
glcr::ErrorOr<glcr::UniquePtr<EndpointClient>> EndpointServer::CreateClient() {
|
||||||
uint64_t client_cap;
|
uint64_t client_cap;
|
||||||
// FIXME: Restrict permissions to send-only here.
|
// FIXME: Restrict permissions to send-only here.
|
||||||
|
@ -9,21 +14,25 @@ glcr::ErrorOr<glcr::UniquePtr<EndpointClient>> EndpointServer::CreateClient() {
|
||||||
return EndpointClient::AdoptEndpoint(client_cap);
|
return EndpointClient::AdoptEndpoint(client_cap);
|
||||||
}
|
}
|
||||||
|
|
||||||
glcr::ErrorCode EndpointServer::Receive(uint64_t* num_bytes, void* data,
|
Thread EndpointServer::RunServer() {
|
||||||
z_cap_t* reply_port_cap) {
|
return Thread(EndpointServerThreadBootstrap, this);
|
||||||
return ZEndpointRecv(endpoint_cap_, num_bytes, data, reply_port_cap);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
glcr::ErrorCode EndpointServer::RunServer() {
|
void EndpointServer::ServerThread() {
|
||||||
while (true) {
|
while (true) {
|
||||||
uint64_t message_size = kBufferSize;
|
uint64_t message_size = kBufferSize;
|
||||||
uint64_t reply_port_cap = 0;
|
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);
|
RequestContext request(recieve_buffer_, message_size);
|
||||||
ResponseContext response(reply_port_cap);
|
ResponseContext response(reply_port_cap);
|
||||||
// FIXME: Consider pumping these errors into the response as well.
|
// FIXME: Consider pumping these errors into the response as well.
|
||||||
RET_ERR(HandleRequest(request, response));
|
check(HandleRequest(request, response));
|
||||||
if (!response.HasWritten()) {
|
if (!response.HasWritten()) {
|
||||||
dbgln("Returning without having written a response. Req type %x",
|
dbgln("Returning without having written a response. Req type %x",
|
||||||
request.request_id());
|
request.request_id());
|
||||||
|
|
|
@ -26,7 +26,8 @@ uint64_t main(uint64_t init_port_cap) {
|
||||||
server->CreateClient());
|
server->CreateClient());
|
||||||
check(stub.Register("denali", *client));
|
check(stub.Register("denali", *client));
|
||||||
|
|
||||||
RET_ERR(server->RunServer());
|
Thread server_thread = server->RunServer();
|
||||||
// FIXME: Add thread join.
|
|
||||||
|
check(server_thread.Join());
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,6 +14,7 @@ uint64_t main(uint64_t port_cap) {
|
||||||
|
|
||||||
ASSIGN_OR_RETURN(auto server, YellowstoneServer::Create());
|
ASSIGN_OR_RETURN(auto server, YellowstoneServer::Create());
|
||||||
Thread registration_thread = server->RunRegistration();
|
Thread registration_thread = server->RunRegistration();
|
||||||
|
Thread server_thread = server->RunServer();
|
||||||
|
|
||||||
uint64_t vaddr;
|
uint64_t vaddr;
|
||||||
check(ZAddressSpaceMap(gSelfVmasCap, 0, gBootDenaliVmmoCap, &vaddr));
|
check(ZAddressSpaceMap(gSelfVmasCap, 0, gBootDenaliVmmoCap, &vaddr));
|
||||||
|
@ -21,7 +22,7 @@ uint64_t main(uint64_t port_cap) {
|
||||||
server->CreateClient());
|
server->CreateClient());
|
||||||
check(SpawnProcessFromElfRegion(vaddr, glcr::Move(client)));
|
check(SpawnProcessFromElfRegion(vaddr, glcr::Move(client)));
|
||||||
|
|
||||||
check(server->RunServer());
|
check(server_thread.Join());
|
||||||
check(registration_thread.Join());
|
check(registration_thread.Join());
|
||||||
dbgln("Yellowstone Finished Successfully.");
|
dbgln("Yellowstone Finished Successfully.");
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Reference in New Issue