rough framework
[lemu] / connections.h
1 #ifndef CONNECTIONS_H
2 #define CONNECTIONS_H
3
4 #include <event2/bufferevent.h>
5 #include <event2/buffer.h>
6 #include <event2/dns.h>
7
8 #include <stdlib.h>
9 #include <stdint.h>
10 #include <arpa/inet.h>
11 #include <sys/resource.h>
12 #include <sys/time.h>
13
14 #include <ossp/uuid.h>
15
16 #include "bsd_queue.h"
17 #include "workqueue.h"
18
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
25 };
26
27 typedef uint_fast8_t connection_flags_t;
28 enum connection_flags {
29 CONN_TYPE_SSL = (1<<0),
30 };
31
32 struct connection {
33 pthread_mutex_t rc_mutex; /* mutex for ref count */
34 size_t reference_count; /* */
35
36 struct server *server;
37
38 enum connection_state_enum state;
39 connection_flags_t flags;
40
41 struct bufferevent *bev;
42 uuid_t *uuid; /* uuid */
43
44 char name[256]; /* temporary auto-generated unique name */
45
46 #ifdef DARWIN
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 */
49 #else
50 struct timespec connect_timespec; /* time this connection entity was initialized */
51 struct timespec last_received_timespec; /* time of last data received from connection */
52 #endif /* DARWIN */
53
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 */
57
58 char *client_address;
59 struct sockaddr *sa;
60 int sa_len;
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 */
63 size_t dns_retries;
64
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 */
68
69 TAILQ_ENTRY(connection) tailq_entry;
70 };
71
72 struct connections {
73 pthread_mutex_t mutex;
74
75 TAILQ_HEAD(connection_tailq_head, connection) head;
76 size_t count;
77 };
78
79
80
81 int connections_init(struct connections *cs);
82 void connections_fini(struct connections *cs);
83
84 void connections_append(struct connection *c);
85 void connections_remove(struct connection *c);
86
87 struct connection ** connections_all_as_array(struct connections *cs, struct connection *c_exclude);
88
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);
92
93 int connection_command_enqueue(struct connection *c, struct command *command);
94
95 void connection_accounting_increment(struct connection *c, struct rusage *ru_start, struct rusage *ru_end);
96
97 void connection_lock_output(struct connection *c);
98 void connection_unlock_output(struct connection *c);
99
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, ...);
103
104 int connection_multi_send(struct connection **clist, char *data, size_t len);
105
106 void connection_resolve_hostname(struct connection *c);
107
108
109 #endif /* CONNECTIONS_H */