Progress on mingw driver
This commit is contained in:
@@ -15,10 +15,26 @@ Channel::Channel(DrivenEngine *de, int chid, int port, const std::string &target
|
||||
|
||||
Channel::~Channel() {
|
||||
assert(driven_->channels_[chid_] == this);
|
||||
driven_->channels_.erase(chid_);
|
||||
if (driven_->recent_channel_ == this) {
|
||||
driven_->recent_channel_ = driven_->stdio_channel_.get();
|
||||
driven_->new_closed_.insert(chid_);
|
||||
driven_->new_outgoing_.erase(chid_);
|
||||
driven_->channels_[chid_] = nullptr;
|
||||
}
|
||||
|
||||
int DrivenEngine::find_unused_chid() {
|
||||
// Note: channel ID zero is special, it is never reused.
|
||||
for (int i = 0; i < MAX_CHAN; i++) {
|
||||
int id = next_unused_chid_++;
|
||||
if (next_unused_chid_ == MAX_CHAN) next_unused_chid_ = 1;
|
||||
if (channels_[id] == nullptr) return id;
|
||||
}
|
||||
assert(false);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Channel *DrivenEngine::get_chid(int chid) {
|
||||
assert(unsigned(chid) < MAX_CHAN);
|
||||
assert(channels_[chid] != nullptr);
|
||||
return channels_[chid];
|
||||
}
|
||||
|
||||
double DrivenEngine::get_clock() {
|
||||
@@ -26,7 +42,9 @@ double DrivenEngine::get_clock() {
|
||||
}
|
||||
|
||||
std::unique_ptr<Channel> DrivenEngine::new_outgoing_channel(const std::string &target) {
|
||||
return std::unique_ptr<Channel>(new Channel(this, next_channel_id_++, 0, target));
|
||||
int chid = find_unused_chid();
|
||||
new_outgoing_.insert(chid);
|
||||
return std::unique_ptr<Channel>(new Channel(this, chid, 0, target));
|
||||
}
|
||||
|
||||
std::unique_ptr<Channel> DrivenEngine::new_incoming_channel() {
|
||||
@@ -55,42 +73,24 @@ void DrivenEngine::stop_driver() {
|
||||
stop_driver_ = true;
|
||||
}
|
||||
|
||||
void DrivenEngine::drv_logmode_write(const std::string &filename, int64_t maxsize) {
|
||||
// NOT IMPLEMENTED YET, but it's okay as a stub.
|
||||
void DrivenEngine::drv_get_new_closed(std::set<int> &channels) {
|
||||
channels = std::move(new_closed_);
|
||||
new_closed_.clear();
|
||||
}
|
||||
|
||||
void DrivenEngine::drv_logmode_replay(const std::string &filename) {
|
||||
// NOT IMPLEMENTED YET.
|
||||
assert(false);
|
||||
}
|
||||
|
||||
void DrivenEngine::drv_logmode_none() {
|
||||
// NOT IMPLEMENTED YET, but it's okay as a stub.
|
||||
}
|
||||
|
||||
Channel *DrivenEngine::get_chid(int chid) {
|
||||
// We cache the most recently used channel.
|
||||
if (recent_channel_->chid_ != chid) {
|
||||
auto iter = channels_.find(chid);
|
||||
assert(iter != channels_.end());
|
||||
recent_channel_ = iter->second;
|
||||
}
|
||||
return recent_channel_;
|
||||
}
|
||||
|
||||
void DrivenEngine::drv_list_channels(std::vector<int> &channels) {
|
||||
channels.clear();
|
||||
for (const auto &p : channels_) {
|
||||
if (!p.second->closed_) {
|
||||
channels.push_back(p.first);
|
||||
}
|
||||
}
|
||||
void DrivenEngine::drv_get_new_outgoing(std::set<int> &channels) {
|
||||
channels = std::move(new_outgoing_);
|
||||
new_outgoing_.clear();
|
||||
}
|
||||
|
||||
const std::string &DrivenEngine::drv_get_target(int chid) {
|
||||
return get_chid(chid)->target_;
|
||||
}
|
||||
|
||||
bool DrivenEngine::drv_outgoing_empty(int chid) {
|
||||
return get_chid(chid)->sb_out_->empty();
|
||||
}
|
||||
|
||||
void DrivenEngine::drv_peek_outgoing(int chid, int *nbytes, const char **bytes) {
|
||||
Channel *ch = get_chid(chid);
|
||||
*nbytes = ch->sb_out_->fill();
|
||||
@@ -101,7 +101,7 @@ void DrivenEngine::drv_sent_outgoing(int chid, int nbytes) {
|
||||
get_chid(chid)->sb_out_->read_bytes(nbytes);
|
||||
}
|
||||
|
||||
void DrivenEngine::drv_recv_incoming(int chid, int nbytes, char *bytes) {
|
||||
void DrivenEngine::drv_recv_incoming(int chid, int nbytes, const char *bytes) {
|
||||
if (nbytes > 0) {
|
||||
get_chid(chid)->sb_in_->write_bytes(bytes, nbytes);
|
||||
}
|
||||
@@ -112,7 +112,7 @@ void DrivenEngine::drv_notify_close(int chid) {
|
||||
}
|
||||
|
||||
int DrivenEngine::drv_notify_accept(int port) {
|
||||
int chid = next_channel_id_++;
|
||||
int chid = find_unused_chid();
|
||||
accepted_channels_.emplace_back(new Channel(this, chid, port, ""));
|
||||
return chid;
|
||||
}
|
||||
@@ -138,16 +138,12 @@ bool DrivenEngine::drv_get_stop_driver() {
|
||||
return stop_driver_;
|
||||
}
|
||||
|
||||
bool DrivenEngine::drv_step_logfile() {
|
||||
// NOT IMPLEMENTED
|
||||
assert(false);
|
||||
return false;
|
||||
}
|
||||
|
||||
DrivenEngine::DrivenEngine() {
|
||||
next_channel_id_ = 1;
|
||||
for (int i = 0; i < MAX_CHAN; i++) {
|
||||
channels_[i] = nullptr;
|
||||
}
|
||||
next_unused_chid_ = 1;
|
||||
stdio_channel_.reset(new Channel(this, 0, 0, ""));
|
||||
recent_channel_ = stdio_channel_.get();
|
||||
rescan_lua_source_ = true;
|
||||
clock_ = 0.0;
|
||||
stop_driver_ = false;
|
||||
@@ -158,7 +154,9 @@ DrivenEngine::~DrivenEngine() {
|
||||
stdio_channel_.reset();
|
||||
accepted_channels_.clear();
|
||||
// At this point, all channels should be gone.
|
||||
assert(channels_.empty());
|
||||
for (int i = 0; i < MAX_CHAN; i++) {
|
||||
assert(channels_[i] == nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
static DrivenEngine *engine_;
|
||||
|
||||
Reference in New Issue
Block a user