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