/** * connection.h: This contains the connection_t structure. * * Copyright © 2007 Pippijn van Steenhoven / The Ermyth Team * Rights to this code are as documented in COPYING. * * * Portions of this file were derived from sources bearing the following license: * Copyright © 2005 Atheme Development Group * Rights to this code are as documented in doc/pod/license.pod. * * $Id: connection.h,v 1.8 2007/09/22 14:27:26 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) { } }; enum connection_flag { CF_UPLINK = 1 << 0, CF_DCCOUT = 1 << 1, CF_DCCIN = 1 << 2, CF_CONNECTING = 1 << 3, CF_LISTENING = 1 << 4, CF_CONNECTED = 1 << 5, CF_DEAD = 1 << 6, CF_NONEWLINE = 1 << 7, CF_SEND_EOF = 1 << 8, /* shutdown(2) write end if sendque empty */ CF_SEND_DEAD = 1 << 9 /* write end shut down */ }; class connection_t : public object, public zero_initialised { void _destroy () { delete this; } struct callbacks { functor::callback1 connected; }; public: unsigned flags; bool is_uplink () { return flags & CF_UPLINK; } bool is_dcc () { return flags & (CF_DCCOUT | CF_DCCIN); } bool is_dccout () { return flags & CF_DCCOUT; } bool is_dccin () { return flags & CF_DCCIN; } bool is_dead () { return flags & CF_DEAD; } bool is_connecting () { return flags & CF_CONNECTING; } bool is_listening () { return flags & CF_LISTENING; } public: unsigned index; // for list_type typedef indexing_vector list_type; static list_type list; public: 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 *); void (*recvq_handler) (connection_t *); void (*close_handler) (connection_t *); connection_t *listener; void *userdata; static callbacks callback; public: static connection_t *create (char const * const name, int fd, unsigned int flags, void (*read_handler) (connection_t *), void (*write_handler) (connection_t *)); static connection_t *open_tcp (char const * const host, char const * const vhost, unsigned int port, void (*read_handler) (connection_t *), void (*write_handler) (connection_t *)); static connection_t *open_listener_tcp (char const * const host, unsigned int port, void (*read_handler) (connection_t *)); static connection_t *accept_tcp (connection_t *, void (*)(connection_t *), void (*)(connection_t *)); void setselect (connection_t *cptr, void (*)(connection_t *), void (*)(connection_t *)); void close (); void close_soon (); void close_soon_children (); static void close_all (void); static void stats (void (*)(char const *, void *), void *); void write (char *format, ...); void write_raw (char *data); static connection_t *find (int); static void select (time_t delay); static int count (void); }; #endif