/************************************************************************ * * $Id: example.cc,v 1.9 2002/10/16 19:36:38 crooney Exp $ * ***********************************************************************/ /************************************************************************** Copyright 2001, Christopher Rooney crooney@crooney.com This file is part of fpoller. fpoller is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. fpoller is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with fpoller; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *************************************************************************/ #include <iostream> #include <string> #include <sys/types.h> #include <sys/socket.h> #include <signal.h> #include <errno.h> #include "poller.h" #include "callbacks.h" namespace /*anonymous*/{ #ifdef TRADITIONAL typedef fpoller::traditional_poller example_poller; #else typedef fpoller::poller example_poller; #endif example_poller *the_poller = NULL; } typedef fpoller::write_callback<const char, fpoller::writer::close_on_exit> example_writer; struct example_parser{ example_parser(){} void parse (int fd, svec &incoming, int request_size){ the_poller->add(fd,fpoller::WRITE,new example_writer(pcc(incoming),request_size)); } }; typedef fpoller::read_callback<example_parser, fpoller::reader::socket_read, fpoller::reader::purge,fpoller::reader::stderr_report,fpoller::reader::termination_string> example_reader; int main(){ signal(SIGPIPE,SIG_IGN); fpoller::reader::termination_string::set_terminator("\n"); int fd; try { the_poller = new example_poller; fd = fpoller::get_listen_socket(8081); fpoller::set_non_blocking(fd); }catch (fpoller::exception &e){ std::cerr <<"fatal error. "<<e.where()<<"\n\t"<<e.what()<<"\n"; exit(1); } the_poller->add(fd,fpoller::READ,new fpoller::accept_callback<example_reader,example_poller>(the_poller)); int nready; for (;;){ if ((nready = the_poller->poll(1000)) >= 0){ std::cerr <<"poll finished "<<nready<<" ready \n"; }else{ std::cerr <<"errno == "<<errno<<" error from poll(): "<< strerror(errno) <<"\n"; } }//for delete the_poller; return 0; }