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
establish a new connection.
*/
static void check_connection()
static void check_connection(void)
{
if (! s_env_univeral_inited)
return;

View file

@ -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)
{
}

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
*/
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);

View file

@ -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();