Listening channels implemented
This commit is contained in:
@@ -5,26 +5,45 @@
|
||||
|
||||
class TNTest : public DrivenEngine {
|
||||
public:
|
||||
std::unique_ptr<Channel> chan_;
|
||||
std::vector<UniqueChannel> channels_;
|
||||
virtual void event_init() {
|
||||
chan_ = new_outgoing_channel("stanford.edu:80");
|
||||
chan_->out()->write_bytes("GET /index.html HTTP/1.1\n\n");
|
||||
// UniqueChannel ch = new_outgoing_channel("stanford.edu:80");
|
||||
// ch->out()->write_bytes("GET /index.html HTTP/1.1\n\n");
|
||||
// channels_.emplace_back(std::move(ch));
|
||||
listen_port(8085);
|
||||
}
|
||||
virtual void event_update() {
|
||||
std::string input = get_stdio_channel()->in()->read_entire_contents();
|
||||
if (input != "") {
|
||||
get_stdio_channel()->out()->write_bytes("stdin: ");
|
||||
get_stdio_channel()->out()->write_bytes(input);
|
||||
|
||||
void dump_lines(StreamBuffer *in, StreamBuffer *out, int chid) {
|
||||
while (true) {
|
||||
std::string l = in->readline();
|
||||
if (l == "") break;
|
||||
std::ostringstream oss;
|
||||
oss << "Chan " << chid << ": " << l;
|
||||
out->write_bytes(oss.str());
|
||||
}
|
||||
if (chan_ != nullptr) {
|
||||
if (chan_->closed()) {
|
||||
get_stdio_channel()->out()->write_bytes("Connection closed.\n");
|
||||
chan_.reset();
|
||||
}
|
||||
|
||||
virtual void event_update() {
|
||||
while (true) {
|
||||
UniqueChannel ch = new_incoming_channel();
|
||||
if (ch == nullptr) break;
|
||||
channels_.emplace_back(std::move(ch));
|
||||
}
|
||||
|
||||
Channel *stdioch = get_stdio_channel();
|
||||
dump_lines(stdioch->in(), stdioch->out(), 0);
|
||||
std::vector<UniqueChannel> keep;
|
||||
for (UniqueChannel &ch : channels_) {
|
||||
dump_lines(ch->in(), stdioch->out(), ch->chid());
|
||||
if (ch->closed()) {
|
||||
std::ostringstream oss;
|
||||
oss << "Chan " << ch->chid() << " closed.\n";
|
||||
stdioch->out()->write_bytes(oss.str());
|
||||
} else {
|
||||
chan_->in()->copy_into(get_stdio_channel()->out());
|
||||
chan_->in()->clear();
|
||||
keep.emplace_back(std::move(ch));
|
||||
}
|
||||
}
|
||||
channels_ = std::move(keep);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user