1 |
/** |
2 |
* connection.h: This contains the connection_t structure. |
3 |
* |
4 |
* Copyright © 2007 Pippijn van Steenhoven / The Ermyth Team |
5 |
* Rights to this code are as documented in COPYING. |
6 |
* |
7 |
* |
8 |
* Portions of this file were derived from sources bearing the following license: |
9 |
* Copyright © 2005 Atheme Development Group |
10 |
* Rights to this code are as documented in doc/pod/license.pod. |
11 |
* |
12 |
* $Id: connection.h,v 1.7 2007-09-16 18:54:42 pippijn Exp $ |
13 |
*/ |
14 |
|
15 |
#ifndef CONNECTION_H |
16 |
#define CONNECTION_H |
17 |
|
18 |
#include <deque> |
19 |
|
20 |
#include <util/containers.h> |
21 |
#include <ermyth/callback.h> |
22 |
|
23 |
#define SENDQSIZE (4096 - 40) |
24 |
|
25 |
/* packetqueue struct */ |
26 |
struct packetqueue : zero_initialised |
27 |
{ |
28 |
node_t node; |
29 |
int firstused; /* offset of first used byte */ |
30 |
int firstfree; /* 1 + offset of last used byte */ |
31 |
char buf[SENDQSIZE]; |
32 |
|
33 |
packetqueue () |
34 |
: firstused (0), firstfree (0) |
35 |
{ |
36 |
} |
37 |
}; |
38 |
|
39 |
enum connection_flag |
40 |
{ |
41 |
CF_UPLINK = 1 << 0, |
42 |
CF_DCCOUT = 1 << 1, |
43 |
CF_DCCIN = 1 << 2, |
44 |
|
45 |
CF_CONNECTING = 1 << 3, |
46 |
CF_LISTENING = 1 << 4, |
47 |
CF_CONNECTED = 1 << 5, |
48 |
CF_DEAD = 1 << 6, |
49 |
|
50 |
CF_NONEWLINE = 1 << 7, |
51 |
CF_SEND_EOF = 1 << 8, /* shutdown(2) write end if sendque empty */ |
52 |
CF_SEND_DEAD = 1 << 9 /* write end shut down */ |
53 |
}; |
54 |
|
55 |
class connection_t : public object, public zero_initialised |
56 |
{ |
57 |
void _destroy () |
58 |
{ |
59 |
delete this; |
60 |
} |
61 |
|
62 |
struct callbacks |
63 |
{ |
64 |
functor::callback1<connection_t *> connected; |
65 |
}; |
66 |
|
67 |
public: |
68 |
unsigned flags; |
69 |
|
70 |
bool is_uplink () { return flags & CF_UPLINK; } |
71 |
bool is_dcc () { return flags & (CF_DCCOUT | CF_DCCIN); } |
72 |
bool is_dccout () { return flags & CF_DCCOUT; } |
73 |
bool is_dccin () { return flags & CF_DCCIN; } |
74 |
bool is_dead () { return flags & CF_DEAD; } |
75 |
bool is_connecting () { return flags & CF_CONNECTING; } |
76 |
bool is_listening () { return flags & CF_LISTENING; } |
77 |
|
78 |
public: |
79 |
unsigned index; // for list_type |
80 |
typedef indexing_vector<connection_t> list_type; |
81 |
static list_type list; |
82 |
|
83 |
public: |
84 |
|
85 |
typedef std::deque<packetqueue *> queue; |
86 |
|
87 |
char name[HOSTLEN]; |
88 |
char hbuf[BUFSIZE + 1]; |
89 |
|
90 |
queue recvq; |
91 |
queue sendq; |
92 |
|
93 |
int fd; |
94 |
int pollslot; |
95 |
|
96 |
time_t first_recv; |
97 |
time_t last_recv; |
98 |
|
99 |
struct sockaddr_in *sa; |
100 |
struct sockaddr saddr; |
101 |
unsigned int saddr_size; |
102 |
|
103 |
void (*read_handler) (connection_t *); |
104 |
void (*write_handler) (connection_t *); |
105 |
|
106 |
void (*recvq_handler) (connection_t *); |
107 |
void (*close_handler) (connection_t *); |
108 |
connection_t *listener; |
109 |
void *userdata; |
110 |
|
111 |
static callbacks callback; |
112 |
|
113 |
public: |
114 |
static connection_t *create (char const * const name, int fd, unsigned int flags, |
115 |
void (*read_handler) (connection_t *), void (*write_handler) (connection_t *)); |
116 |
static connection_t *open_tcp (char const * const host, char const * const vhost, unsigned int port, |
117 |
void (*read_handler) (connection_t *), void (*write_handler) (connection_t *)); |
118 |
static connection_t *open_listener_tcp (char const * const host, unsigned int port, void (*read_handler) (connection_t *)); |
119 |
static connection_t *accept_tcp (connection_t *, void (*)(connection_t *), void (*)(connection_t *)); |
120 |
|
121 |
void setselect (connection_t *cptr, void (*)(connection_t *), void (*)(connection_t *)); |
122 |
void close (); |
123 |
void close_soon (); |
124 |
void close_soon_children (); |
125 |
static void close_all (void); |
126 |
static void stats (void (*)(char const *, void *), void *); |
127 |
void write (char *format, ...); |
128 |
void write_raw (char *data); |
129 |
static connection_t *find (int); |
130 |
static void select (time_t delay); |
131 |
static int count (void); |
132 |
}; |
133 |
|
134 |
#endif |