libevent patch required for SMTP test
[ioevent.git] / EvBufferEventInternal.hpp
blob260bbeca47063700c43ac4b2a3222e4e567291b7
1 #ifndef _EV_BUFFER_EVENT_INTERNAL__HPP_
2 #define _EV_BUFFER_EVENT_INTERNAL__HPP_
4 #include <event.h>
7 struct EvBufferEventInternal
10 /* protected: */
11 /**
12 * Constructor to setup callbacks
14 EvBufferEventInternal()
16 _bufferevent = NULL;
19 /* protected: */
20 /**
21 * Open the bufferevent object with the file/socket descriptor
22 * by calling ::bufferevent_new()
24 struct bufferevent * _bufferevent_new(int fd) /* throw(...) */
26 return _bufferevent =
27 ::bufferevent_new(fd, _readcb_wrapper, _writecb_wrapper, _errorcb_wrapper,
28 reinterpret_cast< void * >(this));
31 /**
32 * Open the bufferevent object with the listen socket descriptor
33 * by calling ::bufev_socket_accept()
35 struct bufferevent * _bufev_socket_accept(int listen_sock) /* throw(...) */
37 return _bufferevent =
38 ::bufev_socket_accept(listen_sock, _readcb_wrapper, _writecb_wrapper, _errorcb_wrapper,
39 reinterpret_cast< void * >(this));
42 /* TODO (future): protected: */
43 /**
44 * Enable certain bufferevent callbacks by calling ::bufferevent_enable()
46 int _enable(short ev)
48 return ::bufferevent_enable(_bufferevent, ev);
51 /**
52 * Disable certain bufferevent callbacks by calling ::bufferevent_disable()
54 int _disable(short ev)
56 return ::bufferevent_disable(_bufferevent, ev);
59 int _write(char * buf, int len)
61 return ::bufferevent_write(_bufferevent, buf, len);
64 int _read(char * buf, int len)
66 return ::bufferevent_read(_bufferevent, buf, len);
69 int _input_length()
71 return EVBUFFER_LENGTH(_bufferevent->input);
73 int _output_length()
75 return EVBUFFER_LENGTH(_bufferevent->output);
78 /* public: */
79 virtual ~EvBufferEventInternal()
81 if (_bufferevent != NULL)
82 ::bufferevent_free(_bufferevent);
85 /* protected: */
86 struct bufferevent * _bufferevent;
88 /* TODO (future): protected: */
89 virtual void _readcb_handler()
90 { /* to be overridden by derived bufferevent class */ }
92 virtual void _writecb_handler()
93 { /* to be overridden by derived bufferevent class */ }
95 virtual void _errorcb_handler(short)
96 { /* to be overridden by derived bufferevent class */ }
98 /* TODO (future): private: */
99 static void _readcb_wrapper(bufferevent *, void * arg)
101 EvBufferEventInternal * bufferevent_self = reinterpret_cast< EvBufferEventInternal * >(arg);
103 bufferevent_self->_readcb_handler();
106 static void _writecb_wrapper(bufferevent *, void * arg)
108 EvBufferEventInternal * bufferevent_self = reinterpret_cast< EvBufferEventInternal * >(arg);
110 bufferevent_self->_writecb_handler();
113 static void _errorcb_wrapper(bufferevent *, short what, void * arg)
115 EvBufferEventInternal * bufferevent_self = reinterpret_cast< EvBufferEventInternal * >(arg);
117 bufferevent_self->_errorcb_handler(what);
122 #endif // _EV_BUFFER_EVENT_INTERNAL__HPP_
124 // vim: set filetype=cpp :