Eliminate stdostream (finally, that was awful.)

This commit is contained in:
2025-12-15 22:42:02 -05:00
parent f528ba69fe
commit 9dc974ebca
6 changed files with 46 additions and 51 deletions

View File

@@ -66,35 +66,38 @@ static DrivenEngine *make_engine(std::string_view kind) {
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// The stdostream
// A std::ostream that sends its output to a StreamBuffer.
//
// This is not currently used so it's commented out. But if it's
// needed, it works fine: it can be ressurected.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
class StreamBufferWriter : public std::streambuf, public eng::opnew {
private:
StreamBuffer *target_;
public:
StreamBufferWriter(StreamBuffer *t) : target_(t) {}
// class StreamBufferWriter : public std::streambuf, public eng::opnew {
// private:
// StreamBuffer *target_;
// public:
// StreamBufferWriter(StreamBuffer *t) : target_(t) {}
virtual int_type overflow(int_type c) {
if (c != EOF) {
target_->write_uint8(c);
}
return c;
}
};
// virtual int_type overflow(int_type c) {
// if (c != EOF) {
// target_->write_uint8(c);
// }
// return c;
// }
// };
class StreamBufferOStream : public std::ostream, public eng::opnew {
private:
StreamBufferWriter writer_;
public:
StreamBufferOStream(StreamBuffer *t) : std::ostream(nullptr), writer_(t) {
rdbuf(&writer_);
}
virtual ~StreamBufferOStream() {
}
};
// class StreamBufferOStream : public std::ostream, public eng::opnew {
// private:
// StreamBufferWriter writer_;
// public:
// StreamBufferOStream(StreamBuffer *t) : std::ostream(nullptr), writer_(t) {
// rdbuf(&writer_);
// }
// virtual ~StreamBufferOStream() {
// }
// };
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
@@ -186,7 +189,6 @@ void DrivenEngine::stop_driver() {
DrivenEngine::DrivenEngine() {
next_unused_chid_ = 1;
stdio_channel_ = eng::make_shared<Channel>(this, 0, 0, "", false);
stdostream_.reset(new StreamBufferOStream(stdio_channel_->out()));
channels_[0] = stdio_channel_;
rescan_lua_source_ = false;
clock_ = 0.0;

View File

@@ -206,10 +206,6 @@ public:
//
SharedChannel get_stdio_channel();
// Obtain the output buffer of the stdio channel as an ostream.
//
std::ostream &stdostream() { return *stdostream_; }
// Set the prompt for the console.
//
void set_console_prompt(const eng::string &prompt);
@@ -304,7 +300,6 @@ private:
SharedChannel channels_[DRV_MAX_CHAN];
int next_unused_chid_;
SharedChannel stdio_channel_;
std::unique_ptr<std::ostream> stdostream_;
eng::vector<SharedChannel> accepted_channels_;
eng::vector<uint32_t> new_outgoing_;
eng::vector<uint32_t> listen_ports_;

View File

@@ -89,14 +89,10 @@ public:
double clock = get_clock();
if (clock > last_clock_ + 0.5) {
int ms = eng::memhash();
stdostream() << std::fixed << std::setprecision(2) << clock << " " << std::hex << ms << " ";
util::dprint(std::fixed, std::setprecision(2), clock, " ", std::hex, ms);
count_++;
last_clock_ = clock;
}
if (count_ == 4) {
stdostream() << std::endl;
count_ = 0;
}
}
};

View File

@@ -135,15 +135,15 @@ public:
}
virtual void do_syntax_error(std::string_view error) override {
stdostream() << "Syntax error: " << error << std::endl;
util::dprint("Syntax error: ", error, "\n");
}
virtual void do_unknown_command(std::string_view name) override {
stdostream() << "Unknown command: " << name << std::endl;
util::dprint("Unknown command: ", name);
}
virtual void do_view_command() override {
stdostream() << world_->tangibles_near_debug_string(actor_id_, 1000);
util::dprint(world_->tangibles_near_debug_string(actor_id_, 1000));
}
virtual void do_moveto_command(int x, int y) override {
@@ -181,7 +181,7 @@ public:
virtual void do_luaprobe_command(std::string_view cmd) override {
world_to_asynchronous();
stdostream() << world_->probe_lua_expr(actor_id_, cmd);
util::dprint(world_->probe_lua_expr(actor_id_, cmd));
world_to_synchronous();
}
@@ -210,7 +210,7 @@ public:
DebugCollector dbc("");
int64_t nactor = world_->patch(sb, &dbc);
if (nactor != actor_id_) change_actor_id(nactor);
dbc.dump(stdostream());
// dbc.dump(...);
} catch (const StreamException &sexcept) {
abandon_server();
return;
@@ -271,6 +271,7 @@ public:
break;
}
case AccessKind::CHANNEL_PRINTS: {
// If there's nothing new in the printbuffer, this is very fast.
world_to_asynchronous();
if (print_channeler_.channel(world_->get_printbuffer(actor_id_), retpk)) {
send_invocation(print_channeler_.invocation(actor_id_));

View File

@@ -49,7 +49,7 @@ public:
admin_id_ = master_->create_login_actor();
// Print out admin ID for debugging purposes.
stdostream() << "Admin actor id = " << admin_id_ << std::endl;
util::dprint("Admin actor id = ", admin_id_);
// Enable listening on port 8085 (client connections)
listen_port(8085);
@@ -65,7 +65,7 @@ public:
// Reset the print channeler.
print_channeler_.reset();
// Trigger the loading of the lua source.
rescan_lua_source(true);
}
@@ -75,15 +75,15 @@ public:
}
virtual void do_syntax_error(std::string_view error) override {
stdostream() << "Syntax error: " << error << std::endl;
util::dprint("Syntax error: ", error, "\n");
}
virtual void do_unknown_command(std::string_view name) override {
stdostream() << "Unknown command: " << name << std::endl;
util::dprint("Unknown command: ", name, "\n");
}
virtual void do_view_command() override {
stdostream() << master_->tangibles_near_debug_string(admin_id_, 1000);
util::dprint(master_->tangibles_near_debug_string(admin_id_, 1000));
}
virtual void do_moveto_command(int x, int y) override {
@@ -120,12 +120,12 @@ public:
virtual void do_luaprobe_command(std::string_view cmd) override {
master_->snapshot();
stdostream() << master_->probe_lua_expr(admin_id_, cmd);
util::dprint(master_->probe_lua_expr(admin_id_, cmd));
master_->rollback();
}
void delete_client(UniqueClient &client) {
stdostream() << "Client closed: actor id=" << client->actor_id_ << std::endl;
util::dprint("Client closed: actor id=", client->actor_id_);
master_->disconnected(client->actor_id_);
client.reset();
}
@@ -135,7 +135,7 @@ public:
sb->write_uint8(util::MSG_DIFF);
sb->write_uint32(0);
int64_t tw_1 = sb->total_writes();
//stdostream() << "Sending diffs to client " << client->actor_id_ << std::endl;
// util::dprint("Sending diffs to client ", client->actor_id_);
client->sync_->diff(client->actor_id_, full, master_.get(), sb);
int64_t tw_2 = sb->total_writes();
sb->overwrite_int32(tw_1, tw_2 - tw_1);
@@ -164,7 +164,7 @@ public:
delete_client(client);
return false;
}
//stdostream() << "Invoking: " << inv.debug_string() << std::endl;
// util::dprint("Invoking: ", inv.debug_string());
master_->invoke(inv);
client->channel_->out()->write_uint8(util::MSG_ACK);
client->channel_->out()->write_uint32(0);
@@ -198,13 +198,14 @@ public:
break;
}
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_));
}
break;
}
default: {
stdostream() << "Invalid event_access: " << int(kind) << std::endl;
util::dprint("Invalid event_access: ", int(kind));
}
}
}
@@ -246,7 +247,7 @@ public:
// the client model and the server synchronous model are identical.
client->sync_->create_login_actor();
clients_.emplace_back(client);
stdostream() << "New client: actor id=" << client->actor_id_ << std::endl;
util::dprint("New client: actor id=", client->actor_id_);
} else if (chan->port() == 8080) {
HttpChannel htchan;
htchan.channel_ = chan;

View File

@@ -149,7 +149,7 @@ public:
// Reset the print channeler.
void reset() { line_ = 0; }
// Copy any new lines from the printbuffer to the stdostream.
// Copy any new lines from the printbuffer to the stream buffer.
// Update the current line number. Return true if the printbuffer
// contains any lines that have already been channeled.
bool channel(const PrintBuffer *pb, StreamBuffer *sb);