Abstract event polling function in EventPoll.

This commit is contained in:
Tatsuhiro Tsujikawa 2012-02-10 00:45:33 +09:00
parent ad50b75d75
commit 24aff9ac44
7 changed files with 319 additions and 30 deletions

View File

@ -78,6 +78,9 @@ fi
AM_CONDITIONAL([HAVE_CUNIT], [ test "x${have_cunit}" = "xyes" ]) AM_CONDITIONAL([HAVE_CUNIT], [ test "x${have_cunit}" = "xyes" ])
AX_HAVE_EPOLL([have_epoll=yes], [have_epoll=no]) AX_HAVE_EPOLL([have_epoll=yes], [have_epoll=no])
if test "x${have_epoll}" = "xyes"; then
AC_DEFINE([HAVE_EPOLL], [1], [Define to 1 if you have the `epoll`.])
fi
AM_CONDITIONAL([HAVE_EPOLL], [ test "x${have_epoll}" = "xyes" ]) AM_CONDITIONAL([HAVE_EPOLL], [ test "x${have_epoll}" = "xyes" ])
# openssl (for examples) # openssl (for examples)

36
examples/EventPoll.h Normal file
View File

@ -0,0 +1,36 @@
/*
* Spdylay - SPDY Library
*
* Copyright (c) 2012 Tatsuhiro Tsujikawa
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef EVENT_POLL_H
#define EVENT_POLL_H
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif // HAVE_CONFIG_H
#ifdef HAVE_EPOLL
# include "EventPoll_epoll.h"
#endif // HAVE_EPOLL
#endif // EVENT_POLL_H

44
examples/EventPollEvent.h Normal file
View File

@ -0,0 +1,44 @@
/*
* Spdylay - SPDY Library
*
* Copyright (c) 2012 Tatsuhiro Tsujikawa
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef EVENT_POLL_EVENT_H
#define EVENT_POLL_EVENT_H
namespace spdylay {
enum EventPollEvent {
EP_POLLIN = 1,
EP_POLLOUT = 1 << 1,
EP_POLLHUP = 1 << 2,
EP_POLLERR = 1 << 3
};
enum EventPollOp {
EP_ADD,
EP_MOD
};
} // namespace spdylay
#endif // EVENT_POLL_EVENT_H

116
examples/EventPoll_epoll.cc Normal file
View File

@ -0,0 +1,116 @@
/*
* Spdylay - SPDY Library
*
* Copyright (c) 2012 Tatsuhiro Tsujikawa
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "EventPoll_epoll.h"
#include <unistd.h>
#include <cassert>
namespace spdylay {
EventPoll::EventPoll(size_t max_events)
: max_events_(max_events), num_events_(0)
{
epfd_ = epoll_create(1);
assert(epfd_ != -1);
evlist_ = new epoll_event[max_events_];
}
EventPoll::~EventPoll()
{
if(epfd_ != -1) {
close(epfd_);
}
delete [] evlist_;
}
int EventPoll::poll(int timeout)
{
num_events_ = 0;
int n = epoll_wait(epfd_, evlist_, max_events_, timeout);
if(n > 0) {
num_events_ = n;
}
return n;
}
int EventPoll::get_num_events()
{
return num_events_;
}
void* EventPoll::get_user_data(size_t p)
{
return evlist_[p].data.ptr;
}
int EventPoll::get_events(size_t p)
{
int events = 0;
int revents = evlist_[p].events;
if(revents & EPOLLIN) {
events |= EP_POLLIN;
}
if(revents & EPOLLOUT) {
events |= EP_POLLOUT;
}
if(revents & EPOLLHUP) {
events |= EP_POLLHUP;
}
if(revents & EPOLLERR) {
events |= EP_POLLERR;
}
return events;
}
namespace {
int update_event(int epfd, int op, int fd, int events, void *user_data)
{
epoll_event ev;
ev.events = 0;
if(events & EP_POLLIN) {
ev.events |= EPOLLIN;
}
if(events & EP_POLLOUT) {
ev.events |= EPOLLOUT;
}
ev.data.ptr = user_data;
return epoll_ctl(epfd, op, fd, &ev);
}
} // namespace
int EventPoll::ctl_event(int op, int fd, int events, void *user_data)
{
if(op == EP_ADD) {
op = EPOLL_CTL_ADD;
} else if(op == EP_MOD) {
op = EPOLL_CTL_MOD;
} else {
return -1;
}
return update_event(epfd_, op, fd, events, user_data);
}
} // namespace spdylay

View File

@ -0,0 +1,60 @@
/*
* Spdylay - SPDY Library
*
* Copyright (c) 2012 Tatsuhiro Tsujikawa
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef EVENT_POLL_EPOLL_H
#define EVENT_POLL_EPOLL_H
#include <cstdlib>
#include <sys/epoll.h>
#include "EventPollEvent.h"
namespace spdylay {
class EventPoll {
public:
EventPoll(size_t max_events);
~EventPoll();
// Returns 0 if this function succeeds, or -1.
// On success
int poll(int timeout);
// Returns number of events detected in previous call of poll().
int get_num_events();
// Returns events of p-eth event.
int get_events(size_t p);
// Returns user data of p-th event.
void* get_user_data(size_t p);
// Adds/Modifies event to watch.
int ctl_event(int op, int fd, int events, void *user_data);
private:
int epfd_;
size_t max_events_;
epoll_event *evlist_;
size_t num_events_;
};
} // namespace spdylay
#endif // EVENT_POLL_EPOLL_H

View File

@ -33,7 +33,11 @@ spdycat_SOURCES = uri.cc spdylay_ssl.cc util.cc spdycat.cc
if HAVE_EPOLL if HAVE_EPOLL
spdyd_SOURCES = uri.cc spdylay_ssl.cc util.cc spdyd.cc spdyd_SOURCES = uri.cc spdylay_ssl.cc util.cc spdyd.cc EventPoll.h \
EventPollEvent.h
spdyd_SOURCES += EventPoll_epoll.cc EventPoll_epoll.h
bin_PROGRAMS += spdyd bin_PROGRAMS += spdyd
endif # HAVE_EPOLL endif # HAVE_EPOLL

View File

@ -32,7 +32,6 @@
#include <netinet/tcp.h> #include <netinet/tcp.h>
#include <signal.h> #include <signal.h>
#include <getopt.h> #include <getopt.h>
#include <sys/epoll.h>
#include <cassert> #include <cassert>
#include <cstdio> #include <cstdio>
@ -56,6 +55,7 @@
#include "spdylay_ssl.h" #include "spdylay_ssl.h"
#include "uri.h" #include "uri.h"
#include "util.h" #include "util.h"
#include "EventPoll.h"
namespace spdylay { namespace spdylay {
@ -99,17 +99,30 @@ class Sessions;
class EventHandler { class EventHandler {
public: public:
EventHandler() : mark_del_(false) {}
virtual ~EventHandler() {} virtual ~EventHandler() {}
virtual int execute(Sessions *sessions) = 0; virtual int execute(Sessions *sessions) = 0;
virtual bool want_read() = 0; virtual bool want_read() = 0;
virtual bool want_write() = 0; virtual bool want_write() = 0;
virtual int fd() const = 0; virtual int fd() const = 0;
virtual bool finish() = 0; virtual bool finish() = 0;
bool mark_del()
{
return mark_del_;
}
void mark_del(bool d)
{
mark_del_ = d;
}
private:
bool mark_del_;
}; };
class Sessions { class Sessions {
public: public:
Sessions(int epfd, SSL_CTX *ssl_ctx) : epfd_(epfd), ssl_ctx_(ssl_ctx) Sessions(int max_events, SSL_CTX *ssl_ctx)
: eventPoll_(max_events),
ssl_ctx_(ssl_ctx)
{} {}
~Sessions() ~Sessions()
{ {
@ -134,31 +147,40 @@ public:
} }
int add_poll(EventHandler *handler) int add_poll(EventHandler *handler)
{ {
return update_poll_internal(handler, EPOLL_CTL_ADD); return update_poll_internal(handler, EP_ADD);
} }
int mod_poll(EventHandler *handler) int mod_poll(EventHandler *handler)
{ {
return update_poll_internal(handler, EPOLL_CTL_MOD); return update_poll_internal(handler, EP_MOD);
}
int poll(int timeout)
{
return eventPoll_.poll(timeout);
}
void* get_user_data(int p)
{
return eventPoll_.get_user_data(p);
}
int get_events(int p)
{
return eventPoll_.get_events(p);
} }
private: private:
int update_poll_internal(EventHandler *handler, int op) int update_poll_internal(EventHandler *handler, int op)
{ {
epoll_event ev; int events = 0;
ev.events = 0;
if(handler->want_read()) { if(handler->want_read()) {
ev.events |= EPOLLIN; events |= EP_POLLIN;
} }
if(handler->want_write()) { if(handler->want_write()) {
ev.events |= EPOLLOUT; events |= EP_POLLOUT;
} }
ev.data.ptr = handler; return eventPoll_.ctl_event(op, handler->fd(), events, handler);
int r = epoll_ctl(epfd_, op, handler->fd(), &ev);
return r;
} }
std::set<EventHandler*> handlers_; std::set<EventHandler*> handlers_;
EventPoll eventPoll_;
SSL_CTX *ssl_ctx_; SSL_CTX *ssl_ctx_;
int epfd_;
}; };
namespace { namespace {
@ -814,47 +836,51 @@ int reactor()
} }
make_non_block(sfd); make_non_block(sfd);
int epfd = epoll_create(1); const size_t MAX_EVENTS = 256;
if(epfd == -1) { Sessions sessions(MAX_EVENTS, ssl_ctx);
perror("epoll_create");
return -1;
}
Sessions sessions(epfd, ssl_ctx);
ListenEventHandler *listen_hd = new ListenEventHandler(sfd); ListenEventHandler *listen_hd = new ListenEventHandler(sfd);
if(sessions.add_poll(listen_hd) == -1) { if(sessions.add_poll(listen_hd) == -1) {
std::cerr << "Adding listening socket to poll failed." << std::endl; std::cerr << "Adding listening socket to poll failed." << std::endl;
return -1; return -1;
} }
sessions.add_handler(listen_hd); sessions.add_handler(listen_hd);
const int MAX_EVENTS = 256; std::vector<EventHandler*> del_list;
epoll_event events[MAX_EVENTS];
while(1) { while(1) {
int n = epoll_wait(epfd, events, MAX_EVENTS, -1); int n = sessions.poll(-1);
if(n == -1) { if(n == -1) {
perror("epoll_wait"); perror("EventPoll");
} else { } else {
for(int i = 0; i < n; ++i) { for(int i = 0; i < n; ++i) {
EventHandler *hd = reinterpret_cast<EventHandler*>(events[i].data.ptr); EventHandler *hd = reinterpret_cast<EventHandler*>
(sessions.get_user_data(i));
int events = sessions.get_events(i);
int r = 0; int r = 0;
if((events[i].events & EPOLLIN) || (events[i].events & EPOLLOUT)) { if(hd->mark_del()) {
continue;
}
if((events & EP_POLLIN) || (events & EP_POLLOUT)) {
r = hd->execute(&sessions); r = hd->execute(&sessions);
} else if(events[i].events & (EPOLLERR | EPOLLHUP)) { } else if(events & (EP_POLLERR | EP_POLLHUP)) {
on_close(sessions, hd); hd->mark_del(true);
} }
if(r != 0) { if(r != 0) {
on_close(sessions, hd); hd->mark_del(true);
} else { } else {
if(hd->finish()) { if(hd->finish()) {
on_close(sessions, hd); hd->mark_del(true);
} else { } else {
sessions.mod_poll(hd); sessions.mod_poll(hd);
} }
} }
} }
for(std::vector<EventHandler*>::iterator i = del_list.begin(),
eoi = del_list.end(); i != eoi; ++i) {
on_close(sessions, *i);
}
del_list.clear();
} }
} }
on_close(sessions, listen_hd); on_close(sessions, listen_hd);
close(epfd);
return 0; return 0;
} }
} // namespace } // namespace