Avoid a race condition with two time() calls.
[beanstalkd.git] / conn.h
blob64d135c3a9c0a7eff95b58218df4b3aa2e36cacf
1 /* conn.h - network connection state */
3 /* Copyright (C) 2007 Keith Rarick and Philotic Inc.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #ifndef conn_h
20 #define conn_h
22 #include <sys/types.h>
23 #include <sys/time.h>
24 #include <event.h>
25 #include "ms.h"
26 #include "tube.h"
27 #include "job.h"
29 /* A command can be at most LINE_BUF_SIZE chars, including "\r\n". This value
30 * MUST be enough to hold the longest possible command or reply line, which is
31 * currently "USING a{200}\r\n". */
32 #define LINE_BUF_SIZE 208
34 /* CONN_TYPE_* are bit masks */
35 #define CONN_TYPE_PRODUCER 1
36 #define CONN_TYPE_WORKER 2
37 #define CONN_TYPE_WAITING 4
39 #define conn_waiting(c) ((c)->type & CONN_TYPE_WAITING)
41 typedef struct conn *conn;
43 struct conn {
44 conn prev, next; /* linked list of connections */
45 int fd;
46 char state;
47 char type;
48 struct event evq;
49 int pending_timeout;
51 /* we cannot share this buffer with the reply line because we might read in
52 * command line data for a subsequent command, and we need to store it
53 * here. */
54 char cmd[LINE_BUF_SIZE]; /* this string is NOT NUL-terminated */
55 int cmd_len;
56 int cmd_read;
57 const char *reply;
58 int reply_len;
59 int reply_sent;
60 char reply_buf[LINE_BUF_SIZE]; /* this string IS NUL-terminated */
62 /* A job to be read from the client. */
63 job in_job;
65 /* Memoization of the soonest job */
66 job soonest_job;
68 /* How many bytes of in_job->body have been read so far. If in_job is NULL
69 * while in_job_read is nonzero, we are in bit bucket mode and
70 * in_job_read's meaning is inverted -- then it counts the bytes that
71 * remain to be thrown away. */
72 int in_job_read;
74 job out_job;
75 int out_job_sent;
76 struct job reserved_jobs; /* doubly-linked list header */
77 tube use;
78 struct ms watch;
81 conn make_conn(int fd, char start_state, tube use, tube watch);
83 int conn_set_evq(conn c, const int events, evh handler);
84 int conn_update_evq(conn c, const int flags);
86 void conn_close(conn c);
88 conn conn_remove(conn c);
89 void conn_insert(conn head, conn c);
91 int count_cur_conns();
92 unsigned int count_tot_conns();
93 int count_cur_producers();
94 int count_cur_workers();
96 void conn_set_producer(conn c);
97 void conn_set_worker(conn c);
99 job soonest_job(conn c);
100 int has_reserved_this_job(conn c, job j);
101 int conn_has_close_deadline(conn c);
102 int conn_ready(conn c);
104 #endif /*conn_h*/