forked from beanstalkd/beanstalkd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
conn.c
62 lines (46 loc) · 1.11 KB
/
conn.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* conn.c - network connection state */
#include <stdlib.h>
#include <stdio.h>
#include "conn.h"
#include "net.h"
#include "util.h"
#include "beanstalkd.h"
conn
make_conn(int fd, char start_state)
{
conn c;
c = malloc(sizeof(struct conn));
if (!c) return warn("OOM"), NULL;
c->fd = fd;
c->state = start_state;
c->cmd_read = 0;
c->in_job = NULL;
c->reserved_job = NULL;
return c;
}
int
conn_update_evq(conn c, const int flags, evh handler)
{
int r;
if (!c) return -1;
if (c->evq.ev_flags == flags) return 0;
/* If it's been added, try to delete it first */
if (c->evq.ev_base) {
r = event_del(&c->evq);
if (r == -1) return -1;
}
/* set the flags and handler, but don't change any existing handler */
event_set(&c->evq, c->fd, flags, c->evq.ev_callback ? : handler, c);
r = event_add(&c->evq, NULL);
if (r == -1) return -1;
return 0;
}
void
conn_close(conn c)
{
fprintf(stderr, "closing conn %d\n", c->fd);
event_del(&c->evq);
close(c->fd);
if (c->reserved_job) enqueue_job(c->reserved_job);
free(c);
}