From b682c4102e31257f96094617275d451c1b0de855 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sat, 16 Feb 2013 00:32:15 -0800 Subject: [PATCH] Eliminate next pointer from connection_t, turn connections into a std::list --- env_universal.cpp | 2 +- env_universal_common.cpp | 5 +-- env_universal_common.h | 13 +++--- fishd.cpp | 86 ++++++++++++++++------------------------ 4 files changed, 45 insertions(+), 61 deletions(-) diff --git a/env_universal.cpp b/env_universal.cpp index bd3350b8f..d6f572e35 100644 --- a/env_universal.cpp +++ b/env_universal.cpp @@ -205,7 +205,7 @@ static void callback(fish_message_type_t type, const wchar_t *name, const wchar_ Make sure the connection is healthy. If not, close it, and try to establish a new connection. */ -static void check_connection() +static void check_connection(void) { if (! s_env_univeral_inited) return; diff --git a/env_universal_common.cpp b/env_universal_common.cpp index 1aa169bfe..5f1c1eaa5 100644 --- a/env_universal_common.cpp +++ b/env_universal_common.cpp @@ -931,12 +931,11 @@ void enqueue_all(connection_t *c) try_send_all(c); } -connection_t::connection_t(const int input_fd) : +connection_t::connection_t(int input_fd) : fd(input_fd), killme(false), buffer_consumed(0), - buffer_used(0), - next(NULL) + buffer_used(0) { } diff --git a/env_universal_common.h b/env_universal_common.h index e5f44f760..294c0e0b8 100644 --- a/env_universal_common.h +++ b/env_universal_common.h @@ -76,8 +76,14 @@ typedef std::queue message_queue_t; /** This struct represents a connection between a universal variable server/client */ -struct connection_t +class connection_t { + private: + /* No assignment */ + connection_t &operator=(const connection_t &); + + public: + /** The file descriptor this socket lives on */ @@ -112,11 +118,6 @@ struct connection_t Number of bytes that have been read into the buffer. */ size_t buffer_used; - - /** - Link to the next connection - */ - struct connection_t *next; /* Constructor */ connection_t(int input_fd); diff --git a/fishd.cpp b/fishd.cpp index 8023b9e8f..ada93f01c 100644 --- a/fishd.cpp +++ b/fishd.cpp @@ -64,6 +64,7 @@ time the original barrier request was sent have been received. #include #include #include +#include #include "fallback.h" #include "util.h" @@ -140,7 +141,8 @@ time the original barrier request was sent have been received. /** The list of connections to clients */ -static connection_t *conn; +typedef std::list connection_list_t; +static connection_list_t connections; /** The socket to accept new clients on @@ -622,10 +624,9 @@ unlock: */ static void broadcast(fish_message_type_t type, const wchar_t *key, const wchar_t *val) { - connection_t *c; message_t *msg; - if (!conn) + if (connections.empty()) return; msg = create_message(type, key, val); @@ -635,15 +636,15 @@ static void broadcast(fish_message_type_t type, const wchar_t *key, const wchar_ prematurely */ - for (c = conn; c; c=c->next) + for (connection_list_t::iterator iter = connections.begin(); iter != connections.end(); ++iter) { msg->count++; - c->unsent.push(msg); + iter->unsent.push(msg); } - for (c = conn; c; c=c->next) + for (connection_list_t::iterator iter = connections.begin(); iter != connections.end(); ++iter) { - try_send_all(c); + try_send_all(&*iter); } } @@ -947,7 +948,6 @@ int main(int argc, char ** argv) init(); while (1) { - connection_t *c; int res; t = sizeof(remote); @@ -956,14 +956,15 @@ int main(int argc, char ** argv) FD_ZERO(&write_fd); FD_SET(sock, &read_fd); max_fd = sock+1; - for (c=conn; c; c=c->next) + for (connection_list_t::const_iterator iter = connections.begin(); iter != connections.end(); ++iter) { - FD_SET(c->fd, &read_fd); - max_fd = maxi(max_fd, c->fd+1); + const connection_t &c = *iter; + FD_SET(c.fd, &read_fd); + max_fd = maxi(max_fd, c.fd+1); - if (! c->unsent.empty()) + if (! c.unsent.empty()) { - FD_SET(c->fd, &write_fd); + FD_SET(c.fd, &write_fd); } } @@ -1008,28 +1009,27 @@ int main(int argc, char ** argv) } else { - connection_t *newc = new connection_t(child_socket); - newc->next = conn; - send(newc->fd, GREETING, strlen(GREETING), MSG_DONTWAIT); - enqueue_all(newc); - conn=newc; + connections.push_front(connection_t(child_socket)); + connection_t &newc = connections.front(); + send(newc.fd, GREETING, strlen(GREETING), MSG_DONTWAIT); + enqueue_all(&newc); } } } - for (c=conn; c; c=c->next) + for (connection_list_t::iterator iter = connections.begin(); iter != connections.end(); ++iter) { - if (FD_ISSET(c->fd, &write_fd)) + if (FD_ISSET(iter->fd, &write_fd)) { - try_send_all(c); + try_send_all(&*iter); } } - for (c=conn; c; c=c->next) + for (connection_list_t::iterator iter = connections.begin(); iter != connections.end(); ++iter) { - if (FD_ISSET(c->fd, &read_fd)) + if (FD_ISSET(iter->fd, &read_fd)) { - read_message(c); + read_message(&*iter); /* Occasionally we save during normal use, so that we @@ -1044,47 +1044,31 @@ int main(int argc, char ** argv) } } - connection_t *prev=0; - c=conn; - - while (c) + for (connection_list_t::iterator iter = connections.begin(); iter != connections.end(); ) { - if (c->killme) + if (iter->killme) { - debug(4, L"Close connection %d", c->fd); + debug(4, L"Close connection %d", iter->fd); - while (! c->unsent.empty()) + while (! iter->unsent.empty()) { - message_t *msg = c->unsent.front(); - c->unsent.pop(); + message_t *msg = iter->unsent.front(); + iter->unsent.pop(); msg->count--; - if (!msg->count) + if (! msg->count) free(msg); } - connection_destroy(c); - if (prev) - { - prev->next=c->next; - } - else - { - conn=c->next; - } - - delete c; - - c=(prev?prev->next:conn); - + connection_destroy(&*iter); + iter = connections.erase(iter); } else { - prev=c; - c=c->next; + ++iter; } } - if (!conn) + if (connections.empty()) { debug(0, L"No more clients. Quitting"); save();