Eliminate next pointer from connection_t, turn connections into a std::list

This commit is contained in:
ridiculousfish 2013-02-16 00:32:15 -08:00
parent 6d522e6ed6
commit b682c4102e
4 changed files with 45 additions and 61 deletions

View file

@ -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 Make sure the connection is healthy. If not, close it, and try to
establish a new connection. establish a new connection.
*/ */
static void check_connection() static void check_connection(void)
{ {
if (! s_env_univeral_inited) if (! s_env_univeral_inited)
return; return;

View file

@ -931,12 +931,11 @@ void enqueue_all(connection_t *c)
try_send_all(c); try_send_all(c);
} }
connection_t::connection_t(const int input_fd) : connection_t::connection_t(int input_fd) :
fd(input_fd), fd(input_fd),
killme(false), killme(false),
buffer_consumed(0), buffer_consumed(0),
buffer_used(0), buffer_used(0)
next(NULL)
{ {
} }

View file

@ -76,8 +76,14 @@ typedef std::queue<message_t *> message_queue_t;
/** /**
This struct represents a connection between a universal variable server/client 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 The file descriptor this socket lives on
*/ */
@ -113,11 +119,6 @@ struct connection_t
*/ */
size_t buffer_used; size_t buffer_used;
/**
Link to the next connection
*/
struct connection_t *next;
/* Constructor */ /* Constructor */
connection_t(int input_fd); connection_t(int input_fd);
}; };

View file

@ -64,6 +64,7 @@ time the original barrier request was sent have been received.
#include <errno.h> #include <errno.h>
#include <locale.h> #include <locale.h>
#include <signal.h> #include <signal.h>
#include <list>
#include "fallback.h" #include "fallback.h"
#include "util.h" #include "util.h"
@ -140,7 +141,8 @@ time the original barrier request was sent have been received.
/** /**
The list of connections to clients 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 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) static void broadcast(fish_message_type_t type, const wchar_t *key, const wchar_t *val)
{ {
connection_t *c;
message_t *msg; message_t *msg;
if (!conn) if (connections.empty())
return; return;
msg = create_message(type, key, val); 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 prematurely
*/ */
for (c = conn; c; c=c->next) for (connection_list_t::iterator iter = connections.begin(); iter != connections.end(); ++iter)
{ {
msg->count++; 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(); init();
while (1) while (1)
{ {
connection_t *c;
int res; int res;
t = sizeof(remote); t = sizeof(remote);
@ -956,14 +956,15 @@ int main(int argc, char ** argv)
FD_ZERO(&write_fd); FD_ZERO(&write_fd);
FD_SET(sock, &read_fd); FD_SET(sock, &read_fd);
max_fd = sock+1; 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); const connection_t &c = *iter;
max_fd = maxi(max_fd, c->fd+1); 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 else
{ {
connection_t *newc = new connection_t(child_socket); connections.push_front(connection_t(child_socket));
newc->next = conn; connection_t &newc = connections.front();
send(newc->fd, GREETING, strlen(GREETING), MSG_DONTWAIT); send(newc.fd, GREETING, strlen(GREETING), MSG_DONTWAIT);
enqueue_all(newc); enqueue_all(&newc);
conn=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 Occasionally we save during normal use, so that we
@ -1044,47 +1044,31 @@ int main(int argc, char ** argv)
} }
} }
connection_t *prev=0; for (connection_list_t::iterator iter = connections.begin(); iter != connections.end(); )
c=conn; {
if (iter->killme)
{
debug(4, L"Close connection %d", iter->fd);
while (c) while (! iter->unsent.empty())
{ {
if (c->killme) message_t *msg = iter->unsent.front();
{ iter->unsent.pop();
debug(4, L"Close connection %d", c->fd);
while (! c->unsent.empty())
{
message_t *msg = c->unsent.front();
c->unsent.pop();
msg->count--; msg->count--;
if (! msg->count) if (! msg->count)
free(msg); free(msg);
} }
connection_destroy(c); connection_destroy(&*iter);
if (prev) iter = connections.erase(iter);
{
prev->next=c->next;
} }
else else
{ {
conn=c->next; ++iter;
}
delete c;
c=(prev?prev->next:conn);
}
else
{
prev=c;
c=c->next;
} }
} }
if (!conn) if (connections.empty())
{ {
debug(0, L"No more clients. Quitting"); debug(0, L"No more clients. Quitting");
save(); save();