From b7b4ad85770f73da509392edba5eadc41f5f14e7 Mon Sep 17 00:00:00 2001 From: Josh Yelon Date: Fri, 15 Oct 2021 13:51:32 -0400 Subject: [PATCH] A little cleanup, and some work on LpxClient --- luprex/core/cpp/drivenengine.hpp | 10 +++++++++- luprex/core/cpp/lpxclient.cpp | 16 ++++++++++++++-- luprex/core/cpp/lpxserver.cpp | 4 ++-- luprex/core/cpp/textgame.cpp | 2 +- luprex/core/cpp/world-core.cpp | 4 ++-- luprex/core/cpp/world-testing.cpp | 24 ++++++++++++------------ luprex/core/cpp/world.hpp | 7 ++++++- 7 files changed, 46 insertions(+), 21 deletions(-) diff --git a/luprex/core/cpp/drivenengine.hpp b/luprex/core/cpp/drivenengine.hpp index 63a06d23..848198ee 100644 --- a/luprex/core/cpp/drivenengine.hpp +++ b/luprex/core/cpp/drivenengine.hpp @@ -178,6 +178,7 @@ private: bool readline_enabled_; friend class DrivenEngine; }; + using UniqueChannel = std::unique_ptr; class DrivenEngine { @@ -206,7 +207,14 @@ public: // double get_clock(); - // Create a channel and open an outgoing connection. + // Create a channel and open an outgoing connection. The channel creation + // always succeeds. You can write to the channel immediately. You can + // read, too, but of course there won't be anything in the incoming buffer + // yet. In future update events, data will show up in the incoming buffer, + // and will have been sent from the outgoing buffer. In future update + // events, the channel may get closed by the remote. If the connection + // fails (say, the remote host doesn't exist), then the Channel will get + // closed with an error. // // DRIVER: The channel object is created instantly, but it does nothing // until the driver notices the new channel. The driver is responsible for diff --git a/luprex/core/cpp/lpxclient.cpp b/luprex/core/cpp/lpxclient.cpp index ae3c2c88..57750b01 100644 --- a/luprex/core/cpp/lpxclient.cpp +++ b/luprex/core/cpp/lpxclient.cpp @@ -9,10 +9,10 @@ class LpxClient : public DrivenEngine { public: using StringVec = LuaConsole::StringVec; - std::unique_ptr world_; + UniqueWorld world_; int64_t actor_id_; InvocationQueue unack_; - Channel *channel_; + UniqueChannel channel_; LuaConsole console_; Gui gui_; int64_t gui_place_; @@ -26,6 +26,18 @@ public: public: virtual void event_init(int argc, char *argv[]) { + // Create the world model. + world_.reset(new World(util::WORLD_TYPE_C_SYNC)); + + // This is a temporary actor that will be used only until the server sends + // us the first difference transmission. We do this only to establish + // the invariant that there's always an actor. When the first difference + // transmission arrives, this actor may be deleted, or it may just be + // ignored, at the server's discretion. + actor_id_ = world_->create_login_actor(); + + // Establish a connection to the server. + channel_ = new_outgoing_channel("localhost:8085"); } virtual void event_update() { diff --git a/luprex/core/cpp/lpxserver.cpp b/luprex/core/cpp/lpxserver.cpp index b662d348..e1ae68bd 100644 --- a/luprex/core/cpp/lpxserver.cpp +++ b/luprex/core/cpp/lpxserver.cpp @@ -8,12 +8,12 @@ class ServerClient { public: int64_t actor_id_; Channel *channel_; - std::unique_ptr sync_; + UniqueWorld sync_; }; class LpxServer : public DrivenEngine { public: - std::unique_ptr master_; + UniqueWorld master_; std::vector> clients_; public: diff --git a/luprex/core/cpp/textgame.cpp b/luprex/core/cpp/textgame.cpp index ab91823b..4ffdd438 100644 --- a/luprex/core/cpp/textgame.cpp +++ b/luprex/core/cpp/textgame.cpp @@ -21,7 +21,7 @@ class TextGame : public DrivenEngine { private: using StringVec = LuaConsole::StringVec; - std::unique_ptr world_; + UniqueWorld world_; LuaConsole console_; Gui gui_; int64_t gui_place_; diff --git a/luprex/core/cpp/world-core.cpp b/luprex/core/cpp/world-core.cpp index a8661849..12c50c02 100644 --- a/luprex/core/cpp/world-core.cpp +++ b/luprex/core/cpp/world-core.cpp @@ -161,7 +161,7 @@ Tangible *World::tangible_make(lua_State *L, int64_t id, const std::string &plan LuaStack LS(L, tangibles, database, metatab); // Create the C++ part of the structure. - std::unique_ptr &t = tangibles_[id]; + UniqueTangible &t = tangibles_[id]; assert (t == nullptr); t.reset(new Tangible(this, id)); @@ -564,7 +564,7 @@ void World::deserialize(StreamBuffer *sb) { size_t ntan = sb->read_uint32(); for (size_t i = 0; i < ntan; i++) { int64_t id = sb->read_int64(); - std::unique_ptr &t = tangibles_[id]; + UniqueTangible &t = tangibles_[id]; if (t == nullptr) { t.reset(new Tangible(this, id)); } else { diff --git a/luprex/core/cpp/world-testing.cpp b/luprex/core/cpp/world-testing.cpp index 55d18976..81a13778 100644 --- a/luprex/core/cpp/world-testing.cpp +++ b/luprex/core/cpp/world-testing.cpp @@ -216,7 +216,7 @@ std::string World::tangible_get_class(int64_t id) const { return result; } -static bool worlds_identical(const std::unique_ptr &w1, const std::unique_ptr &w2) { +static bool worlds_identical(const UniqueWorld &w1, const UniqueWorld &w2) { StreamBuffer sbw1, sbw2; w1->serialize(&sbw1); w2->serialize(&sbw2); @@ -225,9 +225,9 @@ static bool worlds_identical(const std::unique_ptr &w1, const std::unique LuaDefine(unittests_world1animdiff, "c") { - std::unique_ptr m(new World(util::WORLD_TYPE_MASTER)); - std::unique_ptr ss(new World(util::WORLD_TYPE_S_SYNC)); - std::unique_ptr cs(new World(util::WORLD_TYPE_C_SYNC)); + UniqueWorld m(new World(util::WORLD_TYPE_MASTER)); + UniqueWorld ss(new World(util::WORLD_TYPE_S_SYNC)); + UniqueWorld cs(new World(util::WORLD_TYPE_C_SYNC)); StreamBuffer sb; util::IdVector ids = util::id_vector_create(123, 345); @@ -295,8 +295,8 @@ LuaDefine(unittests_world1animdiff, "c") { } LuaDefine(unittests_world2pairtab, "c") { - std::unique_ptr m(new World(util::WORLD_TYPE_MASTER)); - std::unique_ptr ss(new World(util::WORLD_TYPE_S_SYNC)); + UniqueWorld m(new World(util::WORLD_TYPE_MASTER)); + UniqueWorld ss(new World(util::WORLD_TYPE_S_SYNC)); StreamBuffer sb; int ncreate; @@ -343,9 +343,9 @@ LuaDefine(unittests_world2pairtab, "c") { } LuaDefine(unittests_world3diffluatab, "c") { - std::unique_ptr m(new World(util::WORLD_TYPE_MASTER)); - std::unique_ptr ss(new World(util::WORLD_TYPE_S_SYNC)); - std::unique_ptr cs(new World(util::WORLD_TYPE_C_SYNC)); + UniqueWorld m(new World(util::WORLD_TYPE_MASTER)); + UniqueWorld ss(new World(util::WORLD_TYPE_S_SYNC)); + UniqueWorld cs(new World(util::WORLD_TYPE_C_SYNC)); StreamBuffer sb; // Initialize all three models so that a tangible exists. @@ -397,9 +397,9 @@ LuaDefine(unittests_world3diffluatab, "c") { } LuaDefine(unittests_world4difftanclass, "c") { - std::unique_ptr m(new World(util::WORLD_TYPE_MASTER)); - std::unique_ptr ss(new World(util::WORLD_TYPE_S_SYNC)); - std::unique_ptr cs(new World(util::WORLD_TYPE_C_SYNC)); + UniqueWorld m(new World(util::WORLD_TYPE_MASTER)); + UniqueWorld ss(new World(util::WORLD_TYPE_S_SYNC)); + UniqueWorld cs(new World(util::WORLD_TYPE_C_SYNC)); StreamBuffer sb; // Initialize all three models so that a tangible exists. diff --git a/luprex/core/cpp/world.hpp b/luprex/core/cpp/world.hpp index 2dd4deb1..520136b2 100644 --- a/luprex/core/cpp/world.hpp +++ b/luprex/core/cpp/world.hpp @@ -75,6 +75,8 @@ public: void configure_id_pool_for_actor() { id_player_pool_.set_fifo_capacity(20); } }; +using UniqueTangible = std::unique_ptr; + class World { public: using IdVector = util::IdVector; @@ -415,7 +417,7 @@ private: // Tangibles table. // - std::unordered_map> tangibles_; + std::unordered_map tangibles_; // Thread schedule: must include every thread, except // for the one currently-executing thread. @@ -443,4 +445,7 @@ private: friend int lfn_tangible_place(lua_State *L); }; +using UniqueWorld = std::unique_ptr; + + #endif // WORLD_HPP