Browse Source

Reference clients by id, not fd since fd can be the same and be invalidated

master
Con Kolivas 11 years ago
parent
commit
718bf11a46
  1. 22
      src/connector.c

22
src/connector.c

@ -38,12 +38,14 @@ struct client_instance {
char buf[PAGESIZE]; char buf[PAGESIZE];
int bufofs; int bufofs;
int fd; int fd;
int id;
}; };
typedef struct client_instance client_instance_t; typedef struct client_instance client_instance_t;
/* For the hashtable of all clients */ /* For the hashtable of all clients */
static client_instance_t *clients; static client_instance_t *clients;
static int client_id;
/* Accepts incoming connections to the server socket and generates client /* Accepts incoming connections to the server socket and generates client
* instances */ * instances */
@ -72,7 +74,8 @@ retry:
client->fd = fd; client->fd = fd;
ck_wlock(&ci->lock); ck_wlock(&ci->lock);
HASH_ADD_INT(clients, fd, client); client->id = client_id++;
HASH_ADD_INT(clients, id, client);
ci->nfds++; ci->nfds++;
ck_wunlock(&ci->lock); ck_wunlock(&ci->lock);
@ -148,7 +151,7 @@ reparse:
void *receiver(void *arg) void *receiver(void *arg)
{ {
conn_instance_t *ci = (conn_instance_t *)arg; conn_instance_t *ci = (conn_instance_t *)arg;
client_instance_t *client, *tmp; client_instance_t *client, *tmp, *tmpa;
struct pollfd fds[65536]; struct pollfd fds[65536];
int ret, nfds, i; int ret, nfds, i;
@ -179,20 +182,25 @@ retry:
if (!ret) if (!ret)
goto retry; goto retry;
for (i = 0; i < nfds; i++) { for (i = 0; i < nfds; i++) {
int fd;
if (!(fds[i].revents & POLLIN)) if (!(fds[i].revents & POLLIN))
continue; continue;
fd = fds[i].fd; client = NULL;
ck_rlock(&ci->lock); ck_rlock(&ci->lock);
HASH_FIND_INT(clients, &fd, client); HASH_ITER(hh, clients, tmp, tmpa) {
if (tmp->fd == fds[i].fd) {
client = tmp;
break;
}
}
ck_runlock(&ci->lock);
if (!client) if (!client)
LOGWARNING("Failed to find client with fd %d in hashtable!", fd); LOGWARNING("Failed to find client with fd %d in hashtable!", fds[i].fd);
else else
parse_client_msg(ci, client); parse_client_msg(ci, client);
ck_runlock(&ci->lock);
if (--ret < 1) if (--ret < 1)
break; break;

Loading…
Cancel
Save