• 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., 51 Franklin Street, Fifth Floor, Boston, MA
00016  * 02110-1301 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 #if defined(HAVE_BOOST_ASIO_HPP)
00027 # include <boost/asio.hpp>
00028 #else
00029 # include <asio.hpp>
00030 #endif
00031 
00032 #include <sync/xp/Synchronizer.h>
00033 #include "Session.h"
00034 
00035 class TCPAccountHandler;
00036 
00037 class IOServerHandler
00038 {
00039 public:
00040     IOServerHandler(int port, boost::function<void (IOServerHandler*, boost::shared_ptr<Session>)> af,
00041                     boost::function<void (boost::shared_ptr<Session>)> ef, asio::io_service& io_service_)
00042     :   accept_synchronizer(boost::bind(&IOServerHandler::_signal, this)),
00043         io_service(io_service_),
00044         m_pAcceptor(NULL),
00045         session_ptr(),
00046         m_af(af),
00047         m_ef(ef)
00048     {
00049         UT_DEBUGMSG(("IOServerHandler()\n"));
00050         m_pAcceptor = new asio::ip::tcp::acceptor(io_service, asio::ip::tcp::endpoint(asio::ip::tcp::v4(), port));
00051     }
00052 
00053     virtual ~IOServerHandler()
00054     {
00055         UT_DEBUGMSG(("IOServerHandler::~IOServerHandler()\n"));
00056         if (m_pAcceptor)
00057             stop();
00058     }
00059 
00060     virtual void stop()
00061     {
00062         UT_DEBUGMSG(("IOServerHandler::stop()\n"));
00063         if (m_pAcceptor)
00064         {
00065             m_pAcceptor->close();
00066             DELETEP(m_pAcceptor);
00067         }
00068     }
00069 
00070     void run()
00071     {
00072         UT_DEBUGMSG(("IOServerHandler::run()\n"));
00073         asyncAccept();
00074     }
00075 
00076     void asyncAccept()
00077     {
00078         UT_DEBUGMSG(("IOServerHandler::asyncAccept()\n"));
00079         UT_return_if_fail(m_pAcceptor);
00080         session_ptr.reset(new Session(io_service, m_ef));
00081         m_pAcceptor->async_accept(session_ptr->getSocket(),
00082             boost::bind(&IOServerHandler::handleAsyncAccept,
00083                 this, asio::placeholders::error));
00084     }
00085 
00086 private:
00087     void _signal()
00088     {
00089         UT_DEBUGMSG(("IOServerHandler::_signal()\n"));
00090         UT_return_if_fail(session_ptr);
00091         session_ptr->asyncReadHeader();
00092         m_af(this, session_ptr);
00093     }
00094 
00095     void handleAsyncAccept(const asio::error_code& ec)
00096     {
00097         UT_DEBUGMSG(("IOServerHandler::handleAsyncAccept()\n"));
00098         if (ec)
00099         {
00100             UT_DEBUGMSG(("Error accepting connection: %s\n", ec.message().c_str()));
00101             return;
00102         }
00103         accept_synchronizer.signal();
00104     }
00105 
00106     Synchronizer                accept_synchronizer;
00107     asio::io_service&           io_service;
00108     asio::ip::tcp::acceptor*    m_pAcceptor;
00109     boost::shared_ptr<Session>  session_ptr;
00110 
00111     boost::function<void (IOServerHandler*, boost::shared_ptr<Session>)> m_af;
00112     boost::function<void (boost::shared_ptr<Session>)> m_ef;
00113 };
00114 
00115 #endif /* __IO_SERVER_HANDLER__ */

Generated on Sun Feb 14 2021 for AbiWord by  doxygen 1.7.1