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