From 1d15adac481900347100cc52cea6258aae975c40 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Sat, 26 Nov 2011 23:50:07 +0100 Subject: [PATCH] Main loop: Switch to infinite ticking paradigm; agents connect on the fly, cannot reconnect yet --- main.cc | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/main.cc b/main.cc index 34f63cc..c3e24cf 100644 --- a/main.cc +++ b/main.cc @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -23,7 +24,6 @@ main(int argc, char *argv[]) srandom(time(NULL)); - signal(SIGPIPE, SIG_IGN); int lfd = socket(AF_INET, SOCK_STREAM, 0); struct sockaddr_in sin; memset(&sin, 0, sizeof(sin)); @@ -35,22 +35,29 @@ main(int argc, char *argv[]) bind(lfd, (struct sockaddr *) &sin, sizeof(sin)); listen(lfd, 10); - int cfd; - while ((cfd = accept(lfd, NULL, NULL)) >= 0) { - class connection conn(cfd); - class tile &agentpos = map.tile_at(random() % map.w, random() % map.h); - class agent agent(0, agentpos, conn); + signal(SIGPIPE, SIG_IGN); + int flags = fcntl(lfd, F_GETFL, 0); + fcntl(lfd, F_SETFL, flags | O_NONBLOCK); - while (true) { - std::cout << "tick " << tick_id << '\n'; - map.print_map(); - std::cout << '\n'; + class agent *agent = NULL; + while (true) { + std::cout << "tick " << tick_id << '\n'; + map.print_map(); + std::cout << '\n'; - agent.on_tick(); - - usleep(1000000); - tick_id++; + if (agent) { + agent->on_tick(); + } else { + int cfd = accept(lfd, NULL, NULL); + if (cfd >= 0) { + class connection *conn = new class connection(cfd); + class tile &agentpos = map.tile_at(random() % map.w, random() % map.h); + agent = new class agent(0, agentpos, *conn); + } } + + usleep(1000000); + tick_id++; } return 0;