From 859ceb6a8e65409480bc10e6b684fc668728f5cd Mon Sep 17 00:00:00 2001 From: Chris Brody Date: Wed, 19 Dec 2007 23:06:58 +0100 Subject: [PATCH] Initial version. --- .gitignore | 14 ++++++ ioevent | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++ test/io-test-eof.cpp | 80 ++++++++++++++++++++++++++++++++++ test/io-time-test.cpp | 70 ++++++++++++++++++++++++++++++ 4 files changed, 282 insertions(+) create mode 100644 .gitignore create mode 100644 ioevent create mode 100644 test/io-test-eof.cpp create mode 100644 test/io-time-test.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..093a3d9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,14 @@ +ev.h +ev.c +ev++.h +event.h +event.c +ev_vars.h +ev_wrap.h +ev_select.c +ev_poll.c +ev_epoll.c +ev_kqueue.c +ev_port.c +test/io-test-eof +test/io-time-test diff --git a/ioevent b/ioevent new file mode 100644 index 0000000..1bb3772 --- /dev/null +++ b/ioevent @@ -0,0 +1,118 @@ +#ifndef _IOEVENT__HPP_ +#define _IOEVENT__HPP_ + +/// \file ioevent +/// \brief Inline I/O event watcher classes +/// Copying: http://en.wikipedia.org/wiki/Copyright-free +/// Intended for public-domain wherever possible: +/// http://en.wikipedia.org/wiki/Public_domain + +/// \author Chris Brody mailto:chris.brody@gmail.com + +/// May be used with http://libev.schmorp.de/ or +/// http://monkey.org/~provos/libevent/ + +#include +#include + +/// I/O event callback type +/// @see http://monkey.org/~provos/libevent/ +typedef void cbtype (int, short, void *); + +/// Macro - reference to an I/O event loop +#define IOLOOP event_base * + +/// Construct new I/O event loop +inline IOLOOP +ioloop_new() +{ + return (IOLOOP)::event_init(); +} + +/// Dispatch incoming I/O events & timers +/// (event loop with no special flags) +/// @param l Reference to I/O event loop +inline void +ioloop_dispatch(IOLOOP l) +{ + ::event_base_loop(l, 0); +} + +/// Event loop - dispatch incoming I/O events & timers +/// @param l Reference to I/O event loop +/// @param flags Desired EV flags for event loop +inline void +ioloop_loop(IOLOOP l, int flags) +{ + ::event_base_loop(l, flags); +} + +/// Base I/O watcher class +struct iowatcher : public event +{ + void set (IOLOOP l) + { + ::event_base_set(l, this); + } + + void set (int s, short ev) + { + ::event_set(this, s, (ev & (EV_READ | EV_WRITE)) ? (ev | EV_PERSIST) : ev, ev_callback, ev_arg); + } + + void start() + { + ::event_add(this, NULL); + } + + void stop() + { + ::event_del(this); + } + +protected: + iowatcher(int s, short ev, cbtype cb, void * v = 0) + { + ::event_set(this, s, (ev & (EV_READ | EV_WRITE)) ? (ev | EV_PERSIST) : ev, cb, v); + } + + iowatcher(cbtype cb, void * v = 0) + { + ::event_set(this, -1, 0, cb, v); + } + +//private: + iowatcher() { } +}; + +/// I/O event watcher +struct ioevent : public iowatcher +{ + ioevent(int s, short ev, cbtype cb, void * v = 0) : iowatcher(s, ev, cb, v) { } + + ioevent(cbtype cb, void * v = 0) : iowatcher(cb, v) { } + +private: + ioevent() { } +}; + +/// I/O timer watcher +struct itimer : public iowatcher +{ + itimer(cbtype cb, void * v = 0) : iowatcher(cb, v) { } + + void start(int ts) + { + struct timeval tv; + + tv.tv_sec = ts; + tv.tv_usec = 0; + + ::event_add(this, &tv); + } + +private: + itimer() { } +}; + +#endif diff --git a/test/io-test-eof.cpp b/test/io-test-eof.cpp new file mode 100644 index 0000000..b8e6d4a --- /dev/null +++ b/test/io-test-eof.cpp @@ -0,0 +1,80 @@ +/* + * Embedded build: + * c++ -I$(LIBEV_SRC) -o io-test-eof -DEV_STANDALONE=1 io-test-eof.cpp $(LIBEV_SRC)/ev.c $(LIBEV_SRC)/event.c + * + * + * Wed 2006-12-27 - Modified by Leandro Lucarella + * + * Adapted to test the C++ inteface. + * + * Wed 2007-12-19 - Modified by Chris Brody + * + * Adapted to test the ioevent C++ inteface. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +int test_okay = 1; +int called = 0; + +ioevent * ev; + +void +read_cb(int fd, short event, void *arg) +{ + char buf[256]; + int len; + + len = read(fd, buf, sizeof(buf)); + + printf("%s: read %d%s\n", __func__, + len, len ? "" : " - means EOF"); + + if (len == 0) { + ev->stop(); + + if (called == 1) + test_okay = 0; + } + + called++; +} + +int +main (int argc, char **argv) +{ + const char * test = "test string"; + int pair[2]; + + IOLOOP loop = ioloop_new(); + + if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) + return (1); + + write(pair[0], test, strlen(test)+1); + shutdown(pair[0], SHUT_WR); + + /* Init one event */ + ev = new ioevent(pair[1], EV_READ, read_cb, NULL); + + ev->start(); + + ioloop_dispatch(loop); + + delete ev; + + return (test_okay); +} + diff --git a/test/io-time-test.cpp b/test/io-time-test.cpp new file mode 100644 index 0000000..fb151ce --- /dev/null +++ b/test/io-time-test.cpp @@ -0,0 +1,70 @@ +/* + * Embedded build: + * c++ -I$(LIBEV_SRC) -o io-time-test -DEV_STANDALONE=1 io-time-test.cpp $(LIBEV_SRC)/ev.c $(LIBEV_SRC)/event.c + * + * + * Wed 2007-12-19 - Modified by Chris Brody + * + * Adapted to test C++ inteface. + * + */ + + +#include + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#ifndef WIN32 +#include +#include +#endif +#include +#ifdef HAVE_SYS_TIME_H +#include +#endif +#include +#include +#include +#include +#include + +#include + +int lasttime; + +void +timeout_cb(int fd, short event, void * arg) +{ + itimer * timeout = (itimer *)arg; + + int newtime = time(NULL); + + printf("%s: called at %d: %d\n", __func__, newtime, + newtime - lasttime); + + lasttime = newtime; + + timeout->start(2); +} + +int +main (int argc, char **argv) +{ + /* Initalize the io loop */ + IOLOOP l = ioloop_new(); + + /* Init itimer */ + itimer timeout(timeout_cb, &timeout); + + timeout.start(2); + + lasttime = time(NULL); + + ioloop_dispatch(l); + + return (0); +} + -- 2.11.4.GIT