1 |
pippijn |
1.1 |
/* |
2 |
|
|
* Copyright © 2005 Atheme Development Group |
3 |
pippijn |
1.2 |
* Rights to this code are as documented in doc/pod/license.pod. |
4 |
pippijn |
1.1 |
* |
5 |
|
|
* This contains the connection_t structure. |
6 |
|
|
* |
7 |
pippijn |
1.3 |
* $Id: connection.h,v 1.2 2007-07-21 01:29:07 pippijn Exp $ |
8 |
pippijn |
1.1 |
*/ |
9 |
|
|
|
10 |
|
|
#ifndef CONNECTION_H |
11 |
|
|
#define CONNECTION_H |
12 |
|
|
|
13 |
|
|
#include <deque> |
14 |
|
|
|
15 |
|
|
#include <common/util.h> |
16 |
pippijn |
1.3 |
#include <ermyth/callback.h> |
17 |
pippijn |
1.1 |
|
18 |
|
|
#define SENDQSIZE (4096 - 40) |
19 |
|
|
|
20 |
|
|
/* packetqueue struct */ |
21 |
pippijn |
1.3 |
struct packetqueue : zero_initialised |
22 |
pippijn |
1.1 |
{ |
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 |
pippijn |
1.3 |
class connection_t : public zero_initialised |
35 |
pippijn |
1.1 |
{ |
36 |
pippijn |
1.3 |
void _destroy () |
37 |
|
|
{ |
38 |
|
|
delete this; |
39 |
|
|
} |
40 |
|
|
|
41 |
|
|
struct callbacks |
42 |
|
|
{ |
43 |
|
|
functor::callback1<connection_t *> connected; |
44 |
|
|
}; |
45 |
|
|
|
46 |
|
|
public: |
47 |
|
|
int index; |
48 |
|
|
|
49 |
pippijn |
1.1 |
typedef std::deque<packetqueue *> queue; |
50 |
|
|
|
51 |
|
|
char name[HOSTLEN]; |
52 |
|
|
char hbuf[BUFSIZE + 1]; |
53 |
|
|
|
54 |
|
|
queue recvq; |
55 |
|
|
queue sendq; |
56 |
|
|
|
57 |
|
|
int fd; |
58 |
|
|
int pollslot; |
59 |
|
|
|
60 |
|
|
time_t first_recv; |
61 |
|
|
time_t last_recv; |
62 |
|
|
|
63 |
|
|
struct sockaddr_in *sa; |
64 |
|
|
struct sockaddr saddr; |
65 |
|
|
unsigned int saddr_size; |
66 |
|
|
|
67 |
|
|
void (*read_handler) (connection_t *); |
68 |
|
|
void (*write_handler) (connection_t *); |
69 |
|
|
|
70 |
|
|
unsigned int flags; |
71 |
|
|
|
72 |
|
|
void (*recvq_handler) (connection_t *); |
73 |
|
|
void (*close_handler) (connection_t *); |
74 |
|
|
connection_t *listener; |
75 |
|
|
void *userdata; |
76 |
|
|
|
77 |
|
|
static callbacks callback; |
78 |
pippijn |
1.3 |
}; |
79 |
pippijn |
1.1 |
|
80 |
pippijn |
1.3 |
typedef indexing_vector<connection_t> connection_vector; |
81 |
|
|
E connection_vector connlist; |
82 |
pippijn |
1.1 |
|
83 |
|
|
#define CF_UPLINK 0x00000001 |
84 |
|
|
#define CF_DCCOUT 0x00000002 |
85 |
|
|
#define CF_DCCIN 0x00000004 |
86 |
|
|
|
87 |
|
|
#define CF_CONNECTING 0x00000008 |
88 |
|
|
#define CF_LISTENING 0x00000010 |
89 |
|
|
#define CF_CONNECTED 0x00000020 |
90 |
|
|
#define CF_DEAD 0x00000040 |
91 |
|
|
|
92 |
|
|
#define CF_NONEWLINE 0x00000080 |
93 |
|
|
#define CF_SEND_EOF 0x00000100 /* shutdown(2) write end if sendque empty */ |
94 |
|
|
#define CF_SEND_DEAD 0x00000200 /* write end shut down */ |
95 |
|
|
|
96 |
|
|
#define CF_IS_UPLINK(x) ((x)->flags & CF_UPLINK) |
97 |
|
|
#define CF_IS_DCC(x) ((x)->flags & (CF_DCCOUT | CF_DCCIN)) |
98 |
|
|
#define CF_IS_DCCOUT(x) ((x)->flags & CF_DCCOUT) |
99 |
|
|
#define CF_IS_DCCIN(x) ((x)->flags & CF_DCCIN) |
100 |
|
|
#define CF_IS_DEAD(x) ((x)->flags & CF_DEAD) |
101 |
|
|
#define CF_IS_CONNECTING(x) ((x)->flags & CF_CONNECTING) |
102 |
|
|
#define CF_IS_LISTENING(x) ((x)->flags & CF_LISTENING) |
103 |
|
|
|
104 |
pippijn |
1.3 |
E connection_t *connection_add (char const * const name, int fd, unsigned int flags, |
105 |
|
|
void (*read_handler) (connection_t *), void (*write_handler) (connection_t *)); |
106 |
|
|
E connection_t *connection_open_tcp (char const * const host, char const * const vhost, unsigned int port, |
107 |
|
|
void (*read_handler) (connection_t *), void (*write_handler) (connection_t *)); |
108 |
|
|
E connection_t *connection_open_listener_tcp (char const * const host, unsigned int port, void (*read_handler) (connection_t *)); |
109 |
|
|
E connection_t *connection_accept_tcp (connection_t *, void (*)(connection_t *), void (*)(connection_t *)); |
110 |
|
|
E void connection_setselect (connection_t *, void (*)(connection_t *), void (*)(connection_t *)); |
111 |
|
|
E void connection_close (connection_t *); |
112 |
|
|
E void connection_close_soon (connection_t *); |
113 |
|
|
E void connection_close_soon_children (connection_t *); |
114 |
|
|
E void connection_close_all (void); |
115 |
|
|
E void connection_stats (void (*)(char const *, void *), void *); |
116 |
|
|
E void connection_write (connection_t *to, char *format, ...); |
117 |
|
|
E void connection_write_raw (connection_t *to, char *data); |
118 |
|
|
E connection_t *connection_find (int); |
119 |
|
|
E void connection_select (time_t delay); |
120 |
|
|
E int connection_count (void); |
121 |
pippijn |
1.1 |
|
122 |
|
|
#endif |