Finished code for globals, but something's not working with client/server

This commit is contained in:
2023-04-10 16:00:47 -04:00
parent ef7d0ec365
commit ac383c616f
9 changed files with 230 additions and 55 deletions

View File

@@ -6,6 +6,7 @@
#include "traceback.hpp"
#include "pprint.hpp"
#include "util.hpp"
#include "serializelua.hpp"
#include <iostream>
@@ -75,7 +76,7 @@ World::World(WorldType wt) {
clock_ = 0;
// Initialize global variable state.
next_gvar_assign_ = 1;
assign_seqno_ = 1;
// There shouldn't be any lua errors in the sourceDB at this
// point, since there's no lua code in the sourceDB.
@@ -959,6 +960,53 @@ std::ostream *World::lthread_print_stream() const {
}
}
void World::set_global(LuaCoreStack &LS0, const eng::string &gvar, LuaSlot value) {
lua_State *L = LS0.state();
LuaVar globaldb, copy;
LuaExtStack LS(L, globaldb, copy);
// Serialize then deserialize the data, to produce a copy.
StreamBuffer sb;
eng::string error = serialize_lua(LS, value, &sb);
if (!error.empty()) {
luaL_error(L, "%s", error.c_str());
return;
}
eng::string serialized(sb.view());
error = deserialize_lua(LS, copy, &sb);
if (!error.empty()) {
luaL_error(L, "%s", error.c_str());
return;
}
// Store the copy in the globalDB.
LS.rawget(globaldb, LuaRegistry, "globaldb");
LS.rawset(globaldb, gvar, copy);
// Store the serialized blob.
gvname_to_serial_[gvar] = serialized;
// Implement the tracking so that we can rapidly determine which global
// variables need to be difference transmitted.
//
// In the master model, we generate a sequence number for the assignment.
// We store the mapping from global variable name to that sequence number
// and vice versa.
//
// On the client side, we just record the global variable in a list
// of recently modified globals.
//
if (is_authoritative()) {
int64_t &seqno = gvname_to_seqno_[gvar];
seqno_to_gvname_.erase(seqno);
seqno = assign_seqno_++;
seqno_to_gvname_[seqno] = gvar;
} else {
gvname_modified_.insert(gvar);
}
}
void World::serialize(StreamBuffer *sb) {
assert(stack_is_clear());
assert(redirects_.empty());