/* * Copyright © 2005 Atheme Development Group * Rights to this code are as documented in doc/pod/license.pod. * * This contains the connection_t structure. * * $Id: connection.h,v 1.3 2007/08/28 17:08:06 pippijn Exp $ */ #ifndef CONNECTION_H #define CONNECTION_H #include #include #include #define SENDQSIZE (4096 - 40) /* packetqueue struct */ struct packetqueue : zero_initialised { node_t node; int firstused; /* offset of first used byte */ int firstfree; /* 1 + offset of last used byte */ char buf[SENDQSIZE]; packetqueue () : firstused (0), firstfree (0) { } }; class connection_t : public zero_initialised { void _destroy () { delete this; } struct callbacks { functor::callback1 connected; }; public: int index; typedef std::deque queue; char name[HOSTLEN]; char hbuf[BUFSIZE + 1]; queue recvq; queue sendq; int fd; int pollslot; time_t first_recv; time_t last_recv; struct sockaddr_in *sa; struct sockaddr saddr; unsigned int saddr_size; void (*read_handler) (connection_t *); void (*write_handler) (connection_t *); unsigned int flags; void (*recvq_handler) (connection_t *); void (*close_handler) (connection_t *); connection_t *listener; void *userdata; static callbacks callback; }; typedef indexing_vector connection_vector; E connection_vector connlist; #define CF_UPLINK 0x00000001 #define CF_DCCOUT 0x00000002 #define CF_DCCIN 0x00000004 #define CF_CONNECTING 0x00000008 #define CF_LISTENING 0x00000010 #define CF_CONNECTED 0x00000020 #define CF_DEAD 0x00000040 #define CF_NONEWLINE 0x00000080 #define CF_SEND_EOF 0x00000100 /* shutdown(2) write end if sendque empty */ #define CF_SEND_DEAD 0x00000200 /* write end shut down */ #define CF_IS_UPLINK(x) ((x)->flags & CF_UPLINK) #define CF_IS_DCC(x) ((x)->flags & (CF_DCCOUT | CF_DCCIN)) #define CF_IS_DCCOUT(x) ((x)->flags & CF_DCCOUT) #define CF_IS_DCCIN(x) ((x)->flags & CF_DCCIN) #define CF_IS_DEAD(x) ((x)->flags & CF_DEAD) #define CF_IS_CONNECTING(x) ((x)->flags & CF_CONNECTING) #define CF_IS_LISTENING(x) ((x)->flags & CF_LISTENING) E connection_t *connection_add (char const * const name, int fd, unsigned int flags, void (*read_handler) (connection_t *), void (*write_handler) (connection_t *)); E connection_t *connection_open_tcp (char const * const host, char const * const vhost, unsigned int port, void (*read_handler) (connection_t *), void (*write_handler) (connection_t *)); E connection_t *connection_open_listener_tcp (char const * const host, unsigned int port, void (*read_handler) (connection_t *)); E connection_t *connection_accept_tcp (connection_t *, void (*)(connection_t *), void (*)(connection_t *)); E void connection_setselect (connection_t *, void (*)(connection_t *), void (*)(connection_t *)); E void connection_close (connection_t *); E void connection_close_soon (connection_t *); E void connection_close_soon_children (connection_t *); E void connection_close_all (void); E void connection_stats (void (*)(char const *, void *), void *); E void connection_write (connection_t *to, char *format, ...); E void connection_write_raw (connection_t *to, char *data); E connection_t *connection_find (int); E void connection_select (time_t delay); E int connection_count (void); #endif