57 lines
1.6 KiB
C++
57 lines
1.6 KiB
C++
|
|
#include "textgame.hpp"
|
|
#include "driver.hpp"
|
|
#include "drivenengine.hpp"
|
|
|
|
class TNTest : public DrivenEngine {
|
|
public:
|
|
std::vector<UniqueChannel> channels_;
|
|
virtual void event_init() {
|
|
// 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);
|
|
}
|
|
|
|
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());
|
|
}
|
|
}
|
|
|
|
virtual void event_update() {
|
|
while (true) {
|
|
UniqueChannel ch = new_incoming_channel();
|
|
if (ch == nullptr) break;
|
|
ch->set_readline(true);
|
|
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 {
|
|
keep.emplace_back(std::move(ch));
|
|
}
|
|
}
|
|
channels_ = std::move(keep);
|
|
}
|
|
};
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
//TextGame tg;
|
|
TNTest tg;
|
|
driver_drive(&tg);
|
|
}
|