Files
integration/luprex/core/cpp/main.cpp

56 lines
1.6 KiB
C++
Raw Normal View History

2020-11-12 14:24:33 -05:00
2021-01-22 19:10:47 -05:00
#include "textgame.hpp"
#include "driver.hpp"
#include "drivenengine.hpp"
class TNTest : public DrivenEngine {
public:
2021-10-08 21:03:52 -04:00
std::vector<UniqueChannel> channels_;
virtual void event_init() {
2021-10-08 21:03:52 -04:00
// 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);
}
2021-10-08 21:03:52 -04:00
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() {
2021-10-08 21:03:52 -04:00
while (true) {
UniqueChannel ch = new_incoming_channel();
if (ch == nullptr) break;
channels_.emplace_back(std::move(ch));
}
2021-10-08 21:03:52 -04:00
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 {
2021-10-08 21:03:52 -04:00
keep.emplace_back(std::move(ch));
}
}
2021-10-08 21:03:52 -04:00
channels_ = std::move(keep);
}
};
2020-11-12 14:24:33 -05:00
int main(int argc, char **argv)
{
//TextGame tg;
TNTest tg;
driver_drive(&tg);
2020-11-12 14:24:33 -05:00
}