From 6a3d9b2e4a65dc359b2564b3383f74a28cf327b0 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Sat, 26 Nov 2011 23:59:31 +0100 Subject: [PATCH] Destroy agent on disconnect; another agent can connect later --- connection.cc | 3 ++- connection.h | 1 + main.cc | 5 +++++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/connection.cc b/connection.cc index 206a1ef..dcd9696 100644 --- a/connection.cc +++ b/connection.cc @@ -12,5 +12,6 @@ connection::senses(int tick_id, char around[4]) { char buf[1024]; snprintf(buf, sizeof(buf), "tick %d\naround %c%c%c%c\n\n", tick_id, around[0], around[1], around[2], around[3]); - write(fd, buf, strlen(buf)); + if (write(fd, buf, strlen(buf)) < (ssize_t) strlen(buf)) + error = true; } diff --git a/connection.h b/connection.h index 1d1941e..b0e27c6 100644 --- a/connection.h +++ b/connection.h @@ -8,6 +8,7 @@ class connection { public: int fd; + bool error; connection(int fd_) : fd(fd_) {} diff --git a/main.cc b/main.cc index 677f8ff..0852f0f 100644 --- a/main.cc +++ b/main.cc @@ -47,6 +47,11 @@ main(int argc, char *argv[]) if (agent) { agent->on_tick(); + if (agent->conn && agent->conn->error) { + delete agent; + agent = NULL; + } + } else { int cfd = accept(lfd, NULL, NULL); if (cfd >= 0) {