DrivenEngine channels are now shared_ptr

This commit is contained in:
2022-01-05 12:50:16 -05:00
parent 6b23651606
commit c733023823
7 changed files with 103 additions and 87 deletions

View File

@@ -1,8 +1,7 @@
#include "drivenengine.hpp"
Channel::Channel(DrivenEngine *de, int chid, int port, const std::string &target) {
driven_ = de;
Channel::Channel(DrivenEngine *de, int chid, int port, const std::string &target, bool stop) {
chid_ = chid;
sb_in_.reset(new StreamBuffer);
sb_out_.reset(new StreamBuffer);
@@ -12,20 +11,12 @@ Channel::Channel(DrivenEngine *de, int chid, int port, const std::string &target
readline_enabled_ = (chid == 0);
readline_lastc_ = 0;
desired_prompt_ = "";
assert(driven_->channels_[chid_] == nullptr);
driven_->channels_[chid_] = this;
}
Channel::~Channel() {
assert(driven_->channels_[chid_] == this);
driven_->new_closed_.insert(chid_);
driven_->new_outgoing_.erase(chid_);
driven_->channels_[chid_] = nullptr;
stop_driver_ = stop;
}
void Channel::show_or_hide_command(bool ignore_sb_out) {
bool sb_out_empty = (sb_out_->fill() == 0) || ignore_sb_out;
if (!sb_out_empty || (!readline_enabled_) || (driven_->stop_driver_)) {
if (!sb_out_empty || (!readline_enabled_) || (stop_driver_)) {
int ccsize = current_prompt_.size() + current_command_.size();
readline_echo_ += util::repeat_string("\b \b", ccsize);
current_prompt_ = "";
@@ -140,8 +131,8 @@ int DrivenEngine::find_unused_chid() {
Channel *DrivenEngine::get_chid(int chid) {
assert(unsigned(chid) < MAX_CHAN);
assert(channels_[chid] != nullptr);
return channels_[chid];
assert(channels_[chid].get() != nullptr);
return channels_[chid].get();
}
void DrivenEngine::listen_port(int port) {
@@ -152,24 +143,26 @@ double DrivenEngine::get_clock() {
return clock_;
}
UniqueChannel DrivenEngine::new_outgoing_channel(const std::string &target) {
SharedChannel DrivenEngine::new_outgoing_channel(const std::string &target) {
int chid = find_unused_chid();
new_outgoing_.insert(chid);
return UniqueChannel(new Channel(this, chid, 0, target));
SharedChannel result = std::make_shared<Channel>(this, chid, 0, target, stop_driver_);
channels_[chid] = result;
return result;
}
UniqueChannel DrivenEngine::new_incoming_channel() {
SharedChannel DrivenEngine::new_incoming_channel() {
if (accepted_channels_.empty()) {
return nullptr;
} else {
UniqueChannel result = std::move(accepted_channels_.back());
SharedChannel result = std::move(accepted_channels_.back());
accepted_channels_.pop_back();
return result;
}
}
Channel *DrivenEngine::get_stdio_channel() {
return stdio_channel_.get();
SharedChannel DrivenEngine::get_stdio_channel() {
return stdio_channel_;
}
util::LuaSourcePtr DrivenEngine::get_lua_source() {
@@ -182,17 +175,17 @@ void DrivenEngine::rescan_lua_source() {
void DrivenEngine::stop_driver() {
stop_driver_ = true;
for (int i = 0; i < MAX_CHAN; i++) {
if (channels_[i] != nullptr) {
channels_[i]->stop_driver_ = true;
}
}
}
void DrivenEngine::drv_get_listen_ports(std::set<int> &ports) {
ports = listen_ports_;
}
void DrivenEngine::drv_get_new_closed(std::set<int> &channels) {
channels = std::move(new_closed_);
new_closed_.clear();
}
void DrivenEngine::drv_get_new_outgoing(std::set<int> &channels) {
channels = std::move(new_outgoing_);
new_outgoing_.clear();
@@ -206,6 +199,10 @@ bool DrivenEngine::drv_outgoing_empty(int chid) {
return get_chid(chid)->sb_out_->empty();
}
bool DrivenEngine::drv_get_channel_released(int chid) {
return channels_[chid].use_count() == 1;
}
void DrivenEngine::drv_peek_outgoing(int chid, int *nbytes, const char **bytes) {
return get_chid(chid)->peek_outgoing(nbytes, bytes);
}
@@ -229,11 +226,13 @@ void DrivenEngine::drv_notify_close(int chid, const std::string &err) {
Channel *ch = get_chid(chid);
ch->closed_ = true;
ch->error_ = err;
channels_[chid].reset();
}
int DrivenEngine::drv_notify_accept(int port) {
int chid = find_unused_chid();
accepted_channels_.emplace_back(new Channel(this, chid, port, ""));
channels_[chid] = std::make_shared<Channel>(this, chid, port, "", stop_driver_);
accepted_channels_.push_back(channels_[chid]);
return chid;
}
@@ -263,25 +262,15 @@ bool DrivenEngine::drv_get_stop_driver() {
}
DrivenEngine::DrivenEngine() {
for (int i = 0; i < MAX_CHAN; i++) {
channels_[i] = nullptr;
}
next_unused_chid_ = 1;
stdio_channel_.reset(new Channel(this, 0, 0, ""));
stdio_channel_ = std::make_shared<Channel>(this, 0, 0, "", false);
channels_[0] = stdio_channel_;
rescan_lua_source_ = true;
clock_ = 0.0;
stop_driver_ = false;
}
DrivenEngine::~DrivenEngine() {
// Delete the channels that we own.
stdio_channel_.reset();
accepted_channels_.clear();
// At this point, all channels should be gone.
for (int i = 0; i < MAX_CHAN; i++) {
assert(channels_[i] == nullptr);
}
}
DrivenEngine::~DrivenEngine() {}
static DrivenEngine *engine_;