HTTP server networking side, also, fix the releasing of channels in driver

This commit is contained in:
2022-05-17 15:00:20 -04:00
parent 1ac1ab9420
commit cd3064eb05
4 changed files with 80 additions and 56 deletions

View File

@@ -17,7 +17,8 @@ static void if_error_print_and_exit(const std::string &str) {
}
}
static std::string_view read_file(const char *fn, char *buf, int bufsize, std::string &err) {
static std::string_view read_file(const char *fn, char *buf, int bufsize,
std::string &err) {
FILE *f = fopen(fn, "r");
if (f == 0) {
err = std::string("cannot read file") + fn;
@@ -55,13 +56,13 @@ class Driver {
ChanState state;
int nbytes;
const char *bytes;
bool released;
bool just_released;
bool ready_now;
bool ready_on_pollin;
bool ready_on_pollout;
bool ready_on_outgoing;
int last_write_nbytes;
bool marked_for_deletion() const { return state == CHAN_INACTIVE; }
};
std::vector<ChanInfo> chans_;
@@ -124,8 +125,6 @@ class Driver {
chan.chid = -1;
chan.nbytes = 0;
chan.bytes = 0;
chan.released = false;
chan.just_released = false;
chan.ready_now = false;
chan.ready_on_pollin = false;
chan.ready_on_pollout = false;
@@ -133,17 +132,6 @@ class Driver {
chan.last_write_nbytes = 0;
}
void cleanup_channels() {
for (int i = 0; i < int(chans_.size());) {
if (chans_[i].state == CHAN_INACTIVE) {
chans_[i] = chans_.back();
chans_.pop_back();
} else {
i += 1;
}
}
}
void handle_console_output() {
while (true) {
std::string_view s = recorder_.drv_peek_outgoing(0);
@@ -173,8 +161,6 @@ class Driver {
newchan.state = state;
newchan.nbytes = 0;
newchan.bytes = 0;
newchan.released = false;
newchan.just_released = false;
newchan.ready_now = false;
newchan.ready_on_pollin = false;
newchan.ready_on_pollout = true;
@@ -235,13 +221,6 @@ class Driver {
void advance_plaintext(ChanInfo &chan) {
std::string err;
// If the channel has no outgoing bytes and has been released,
// just close it.
if (chan.released) {
close_channel(chan, "");
return;
}
// Try to write plaintext to the channel.
std::string_view s = recorder_.drv_peek_outgoing(chan.chid);
if (s.size() > 0) {
@@ -376,17 +355,24 @@ class Driver {
int mstimeout = read_console_recently_ ? 100 : 1000;
// Peek output buffers and determine channel release flags.
bool any_released = false;
for (ChanInfo &chan : chans_) {
std::string_view s = recorder_.drv_peek_outgoing(chan.chid);
chan.nbytes = s.size();
chan.bytes = s.data();
chan.just_released = false;
if ((chan.nbytes == 0) && (!chan.released)) {
chan.released = recorder_.drv_get_channel_released(chan.chid);
chan.just_released = chan.released;
if (chan.nbytes == 0) {
if (recorder_.drv_get_channel_released(chan.chid)) {
close_channel(chan, "");
any_released = true;
}
}
}
// Delete any released channels
if (any_released) {
util::remove_marked_items(chans_);
}
// Construct the struct pollfd vector.
int pollsize = 0;
for (const auto &p : listen_sockets_) {
@@ -402,13 +388,12 @@ class Driver {
pfd.events = 0;
pfd.revents = 0;
if (chan.ready_now) mstimeout = 0;
if (chan.just_released) mstimeout = 0;
if (chan.ready_on_pollin) pfd.events |= POLLIN;
if (chan.ready_on_pollout) pfd.events |= POLLOUT;
if (chan.ready_on_outgoing && (chan.nbytes > 0))
pfd.events |= POLLOUT;
// std::cerr << "evt=" << pfd.events << ".nb=" << chan.nbytes << "
// ";
// std::cerr << "evt=" << pfd.events << ".nb=" << chan.nbytes <<
// std::endl;
}
// Do the poll.
@@ -430,7 +415,7 @@ class Driver {
bool pollin = ((pfd.revents & POLLIN) != 0);
bool pollout = ((pfd.revents & POLLOUT) != 0);
bool pollerr = ((pfd.revents & (POLLERR | POLLHUP)) != 0);
if (chan.ready_now || pollerr || chan.just_released ||
if (chan.ready_now || pollerr ||
(chan.ready_on_pollin && pollin) ||
(chan.ready_on_pollout && pollout) ||
(chan.ready_on_outgoing && (chan.nbytes > 0) && pollout)) {
@@ -445,7 +430,7 @@ class Driver {
}
// Delete any newly-inactive channels
cleanup_channels();
util::remove_marked_items(chans_);
}
int replay_logfile(const char *fn, bool verbose) {