aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar ridiculousfish <corydoras@ridiculousfish.com>2013-02-16 00:32:15 -0800
committerGravatar ridiculousfish <corydoras@ridiculousfish.com>2013-02-16 00:32:15 -0800
commitb682c4102e31257f96094617275d451c1b0de855 (patch)
tree2df879d1871a658d4e293aad3e285e5c28e09f64
parent6d522e6ed6b2e87b193e928e888ca4747a4f974d (diff)
Eliminate next pointer from connection_t, turn connections into a std::list
-rw-r--r--env_universal.cpp2
-rw-r--r--env_universal_common.cpp5
-rw-r--r--env_universal_common.h13
-rw-r--r--fishd.cpp86
4 files changed, 45 insertions, 61 deletions
diff --git a/env_universal.cpp b/env_universal.cpp
index bd3350b8..d6f572e3 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 1aa169bf..5f1c1eaa 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 e5f44f76..294c0e0b 100644
--- a/env_universal_common.h
+++ b/env_universal_common.h
@@ -76,8 +76,14 @@ typedef std::queue<message_t *> 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 8023b9e8..ada93f01 100644
--- a/fishd.cpp
+++ b/fishd.cpp
@@ -64,6 +64,7 @@ time the original barrier request was sent have been received.
#include <errno.h>
#include <locale.h>
#include <signal.h>
+#include <list>
#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_t> 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();