• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

IOServerHandler.h

Go to the documentation of this file.
00001 /* Copyright (C) 2007-2008 by Marc Maurer <uwog@uwog.net>
00002  *
00003  * This program is free software; you can redistribute it and/or
00004  * modify it under the terms of the GNU General Public License
00005  * as published by the Free Software Foundation; either version 2
00006  * of the License, or (at your option) any later version.
00007  *
00008  * This program is distributed in the hope that it will be useful,
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  * GNU General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU General Public License
00014  * along with this program; if not, write to the Free Software
00015  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
00016  * 02111-1307, USA.
00017  */
00018 
00019 #ifndef __IO_SERVER_HANDLER__
00020 #define __IO_SERVER_HANDLER__
00021 
00022 #include "ut_debugmsg.h"
00023 
00024 #include <boost/bind.hpp>
00025 #include <boost/function.hpp>
00026 #include <asio.hpp>
00027 
00028 #include <sync/xp/Synchronizer.h>
00029 #include "Session.h"
00030 
00031 class TCPAccountHandler;
00032 
00033 using asio::ip::tcp;
00034 
00035 class IOServerHandler
00036 {
00037 public:
00038     IOServerHandler(int port, boost::function<void (IOServerHandler*, boost::shared_ptr<Session>)> af,
00039                     boost::function<void (boost::shared_ptr<Session>)> ef, asio::io_service& io_service_)
00040     :   accept_synchronizer(boost::bind(&IOServerHandler::_signal, this)),
00041         io_service(io_service_),
00042         m_pAcceptor(NULL),
00043         session_ptr(),
00044         m_af(af),
00045         m_ef(ef)
00046     {
00047         UT_DEBUGMSG(("IOServerHandler()\n"));
00048         m_pAcceptor = new tcp::acceptor(io_service, tcp::endpoint(tcp::v4(), port));
00049     }
00050 
00051     virtual ~IOServerHandler()
00052     {
00053         UT_DEBUGMSG(("IOServerHandler::~IOServerHandler()\n"));
00054         if (m_pAcceptor)
00055             stop();
00056     }
00057 
00058     virtual void stop()
00059     {
00060         UT_DEBUGMSG(("IOServerHandler::stop()\n"));
00061         if (m_pAcceptor)
00062         {
00063             m_pAcceptor->close();
00064             DELETEP(m_pAcceptor);
00065         }
00066     }
00067 
00068     void run()
00069     {
00070         UT_DEBUGMSG(("IOServerHandler::run()\n"));
00071         asyncAccept();
00072     }
00073 
00074     void asyncAccept()
00075     {
00076         UT_DEBUGMSG(("IOServerHandler::asyncAccept()\n"));
00077         UT_return_if_fail(m_pAcceptor);
00078         session_ptr.reset(new Session(io_service, m_ef));
00079         m_pAcceptor->async_accept(session_ptr->getSocket(),
00080             boost::bind(&IOServerHandler::handleAsyncAccept,
00081                 this, asio::placeholders::error));
00082     }
00083 
00084 private:
00085     void _signal()
00086     {
00087         UT_DEBUGMSG(("IOServerHandler::_signal()\n"));
00088         UT_return_if_fail(session_ptr);
00089         session_ptr->asyncReadHeader();
00090         m_af(this, session_ptr);
00091     }
00092 
00093     void handleAsyncAccept(const asio::error_code& ec)
00094     {
00095         UT_DEBUGMSG(("IOServerHandler::handleAsyncAccept()\n"));
00096         if (ec)
00097         {
00098             UT_DEBUGMSG(("Error accepting connection: %s\n", ec.message().c_str()));
00099             return;
00100         }
00101         accept_synchronizer.signal();
00102     }
00103 
00104     Synchronizer                accept_synchronizer;
00105     asio::io_service&           io_service;
00106     asio::ip::tcp::acceptor*    m_pAcceptor;
00107     boost::shared_ptr<Session>  session_ptr;
00108 
00109     boost::function<void (IOServerHandler*, boost::shared_ptr<Session>)> m_af;
00110     boost::function<void (boost::shared_ptr<Session>)> m_ef;
00111 };
00112 
00113 #endif /* __IO_SERVER_HANDLER__ */

Generated on Sat May 26 2012 for AbiWord by  doxygen 1.7.1