4 #include <event2/bufferevent.h>
5 #include <event2/buffer.h>
6 #include <event2/dns.h>
10 #include <arpa/inet.h>
11 #include <sys/resource.h>
14 #include <ossp/uuid.h>
16 #include "bsd_queue.h"
17 #include "workqueue.h"
19 enum connection_state_enum
{
20 CONNECTION_STATE_INIT
= 0,
21 CONNECTION_STATE_CONNECTED
,
22 CONNECTION_STATE_AUTHENTICATED
,
23 CONNECTION_STATE_WANT_CLOSE
,
24 CONNECTION_STATE_CLOSED
27 typedef uint_fast8_t connection_flags_t
;
28 enum connection_flags
{
29 CONN_TYPE_SSL
= (1<<0),
33 pthread_mutex_t rc_mutex
; /* mutex for ref count */
34 size_t reference_count
; /* */
36 struct server
*server
;
38 enum connection_state_enum state
;
39 connection_flags_t flags
;
41 struct bufferevent
*bev
;
42 uuid_t
*uuid
; /* uuid */
44 char name
[256]; /* temporary auto-generated unique name */
47 struct timeval connect_time
; /* time of instantiation of this connection entity */
48 struct timeval last_received_time
; /* time of last data received from connection */
50 struct timespec connect_timespec
; /* time this connection entity was initialized */
51 struct timespec last_received_timespec
; /* time of last data received from connection */
54 size_t total_read
; /* a tally of number of bytes read from connection */
55 struct timeval utime
; /* accounting: rusage usertime accumulation */
56 struct timeval stime
; /* accounting: rusage systime accumulation */
61 struct evdns_request
*evdns_request
; /* the request for looking up the reverse of sa */
62 struct event
*dns_retry_ev
; /* timeout event for retrying failed dns */
65 pthread_mutex_t commands_mutex
;
66 STAILQ_HEAD(commands_stailq_head
, command
) commands_head
;
67 volatile struct thread_struct
*owner
; /* set if command queue is currently being consumed */
69 TAILQ_ENTRY(connection
) tailq_entry
;
73 pthread_mutex_t mutex
;
75 TAILQ_HEAD(connection_tailq_head
, connection
) head
;
81 int connections_init(struct connections
*cs
);
82 void connections_fini(struct connections
*cs
);
84 void connections_append(struct connection
*c
);
85 void connections_remove(struct connection
*c
);
87 struct connection
** connections_all_as_array(struct connections
*cs
, struct connection
*c_exclude
);
89 int connection_init(struct server
*s
, struct connection
*c
, struct bufferevent
*bev
, struct sockaddr
*sock
, int socklen
, connection_flags_t flags
);
90 void connection_inc_ref(struct connection
*c
);
91 void connection_free(struct connection
*c
);
93 int connection_command_enqueue(struct connection
*c
, struct command
*command
);
95 void connection_accounting_increment(struct connection
*c
, struct rusage
*ru_start
, struct rusage
*ru_end
);
97 void connection_lock_output(struct connection
*c
);
98 void connection_unlock_output(struct connection
*c
);
100 int connection_printf(struct connection
*c
, const char *fmt
, ...);
101 int connection_vprintf(struct connection
*c
, const char *fmt
, va_list ap
);
102 int connection_printf_broadcast(struct connection
*sender
, bool exclude_sender
, const char *fmt
, ...);
104 int connection_multi_send(struct connection
**clist
, char *data
, size_t len
);
106 void connection_resolve_hostname(struct connection
*c
);
109 #endif /* CONNECTIONS_H */