Use fewer pointers refactor: get rid of visible_world_ in drivenengine. The world is now fully owned by the drivenengine.

This commit is contained in:
2026-02-21 19:27:42 -05:00
parent 48b7bf37da
commit 00ef81bf0d
4 changed files with 47 additions and 63 deletions

View File

@@ -23,13 +23,11 @@ using ClientVector = eng::vector<UniqueClient>;
class LpxServer : public DrivenEngine {
public:
UniqueWorld master_;
ClientVector clients_;
PrintChanneler print_channeler_;
HttpChannelMap http_client_channels_;
HttpChannelVec http_server_channels_;
eng::vector<Invocation> delayed_invocations_;
int64_t admin_id_ = 0;
double next_tick_ = 0;
lua_State *lua_syntax_checker_;
@@ -40,13 +38,13 @@ public:
lua_syntax_checker_ = LuaCoreStack::newstate(eng::l_alloc);
// Create the master world model.
master_.reset(new World(WORLD_TYPE_MASTER));
world_.reset(new World(WORLD_TYPE_MASTER));
// Create the admin actor. Note: there isn't any 'init' function yet.
admin_id_ = master_->create_login_actor();
actor_id_ = world_->create_login_actor();
// Print out admin ID for debugging purposes.
util::dprint("Admin actor id = ", admin_id_);
util::dprint("Admin actor id = ", actor_id_);
// Enable listening on port 8085 (client connections)
listen_port(8085);
@@ -54,9 +52,6 @@ public:
// Enable listening on port 8080 (http server connections)
listen_port(8080);
// Export stuff to the graphics engine.
set_visible_world_and_actor(master_.get(), admin_id_);
// Reset the print channeler.
print_channeler_.reset();
@@ -75,7 +70,7 @@ public:
void delete_client(UniqueClient &client) {
util::dprint("Client closed: actor id=", client->actor_id_);
master_->disconnected(client->actor_id_);
world_->disconnected(client->actor_id_);
client.reset();
}
@@ -85,7 +80,7 @@ public:
sb->write_uint32(0);
int64_t tw_1 = sb->total_writes();
// util::dprint("Sending diffs to client ", client->actor_id_);
client->sync_->diff(client->actor_id_, full, master_.get(), sb);
client->sync_->diff(client->actor_id_, full, world_.get(), sb);
int64_t tw_2 = sb->total_writes();
sb->overwrite_int32(tw_1, tw_2 - tw_1);
}
@@ -114,7 +109,7 @@ public:
return false;
}
// util::dprint("Invoking: ", inv.debug_string());
master_->invoke(inv);
world_->invoke(inv);
client->channel_->out()->write_uint8(util::MSG_ACK);
client->channel_->out()->write_uint32(0);
client->sync_->invoke(inv);
@@ -123,20 +118,20 @@ public:
}
virtual void event_access(AccessKind kind, int64_t place_id, std::string_view datapk, StreamBuffer *retpk) override {
if (place_id == 0) place_id = admin_id_;
if (place_id == 0) place_id = actor_id_;
switch (kind) {
case AccessKind::INVOKE_LUA_CALL:
case AccessKind::INVOKE_LUA_EXPR:
case AccessKind::INVOKE_FLUSH_PRINTS:
case AccessKind::INVOKE_TICK:
case AccessKind::INVOKE_LUA_SOURCE: {
delayed_invocations_.emplace_back(kind, admin_id_, place_id, datapk);
delayed_invocations_.emplace_back(kind, actor_id_, place_id, datapk);
break;
}
case AccessKind::PROBE_LUA_CALL: {
master_->snapshot();
master_->probe_lua_call(admin_id_, place_id, datapk, retpk);
master_->rollback();
world_->snapshot();
world_->probe_lua_call(actor_id_, place_id, datapk, retpk);
world_->rollback();
break;
}
case AccessKind::VALIDATE_LUA_EXPR: {
@@ -148,8 +143,8 @@ public:
}
case AccessKind::CHANNEL_PRINTS: {
// If there's nothing new in the printbuffer, this is very fast.
if (print_channeler_.channel(master_->get_printbuffer(admin_id_), retpk)) {
master_->invoke(print_channeler_.invocation(admin_id_));
if (print_channeler_.channel(world_->get_printbuffer(actor_id_), retpk)) {
world_->invoke(print_channeler_.invocation(actor_id_));
}
set_have_prints(false);
break;
@@ -171,7 +166,7 @@ public:
// Execute any queued invocations.
// We just feed these directly into the master model.
for (const Invocation &inv : delayed_invocations_) {
master_->invoke(inv);
world_->invoke(inv);
}
delayed_invocations_.clear();
@@ -181,7 +176,7 @@ public:
if (chan == nullptr) break;
if (chan->port() == 8085) {
Client *client = new Client;
client->actor_id_ = master_->create_login_actor();
client->actor_id_ = world_->create_login_actor();
// TODO: initialize the login actor on the master.
client->channel_ = std::move(chan);
client->async_diff_ = true;
@@ -202,7 +197,7 @@ public:
// If the clock has advanced far enough, tick the master model.
if (clock >= next_tick_) {
master_->invoke(Invocation(AccessKind::INVOKE_TICK, 0, 0, ""));
world_->invoke(Invocation(AccessKind::INVOKE_TICK, 0, 0, ""));
for (UniqueClient &client : clients_) {
client->async_diff_ = true;
}
@@ -248,7 +243,7 @@ public:
util::remove_nullptrs(clients_);
// Look for new outgoing HTTP client requests.
for (const auto &pair : master_->http_requests()) {
for (const auto &pair : world_->http_requests()) {
const HttpClientRequest &request = pair.second;
HttpChannel &channel = http_client_channels_[request.request_id()];
if (channel.channel_ == nullptr) {
@@ -281,7 +276,7 @@ public:
for (const HttpParser &response : http_responses) {
http_client_channels_.erase(response.request_id());
}
master_->http_responses(http_responses);
world_->http_responses(http_responses);
// Maintain incoming HTTP server channels.
for (HttpChannel &htchan : http_server_channels_) {
@@ -292,7 +287,7 @@ public:
htchan.parsed_bytes_ = chan->in()->fill();
if (parser.complete()) {
StreamBuffer *sb = chan->out();
HttpServerResponse resp = master_->http_serve(parser);
HttpServerResponse resp = world_->http_serve(parser);
resp.send(sb);
htchan.channel_ = nullptr;
}
@@ -301,7 +296,7 @@ public:
util::remove_marked_items(http_server_channels_);
// Notify the driver if there are any prints.
set_have_prints(print_channeler_.have_prints(master_->get_printbuffer(admin_id_)));
set_have_prints(print_channeler_.have_prints(world_->get_printbuffer(actor_id_)));
}
};