Sirikata
libcore/include/sirikata/core/network/Asio.hpp
Go to the documentation of this file.
00001 /*  Sirikata Network Utilities
00002  *  Asio.hpp
00003  *
00004  *  Copyright (c) 2009, Daniel Reiter Horn
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions are
00009  *  met:
00010  *  * Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer.
00012  *  * Redistributions in binary form must reproduce the above copyright
00013  *    notice, this list of conditions and the following disclaimer in
00014  *    the documentation and/or other materials provided with the
00015  *    distribution.
00016  *  * Neither the name of Sirikata nor the names of its contributors may
00017  *    be used to endorse or promote products derived from this software
00018  *    without specific prior written permission.
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00021  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00022  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00023  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
00024  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00025  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00026  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00027  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00029  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031  */
00032 
00033 #ifndef _SIRIKATA_ASIO_HPP_
00034 #define _SIRIKATA_ASIO_HPP_
00035 
00042 #include <boost/asio.hpp>
00043 #include "IOService.hpp"
00044 
00045 namespace Sirikata {
00046 namespace Network {
00047 
00053 class InternalIOWork : public InternalIOService::work {
00054 public:
00055     InternalIOWork(IOService& serv, const String& name = "");
00056     InternalIOWork(IOService* serv, const String& name = "");
00057 
00058     ~InternalIOWork();
00059 private:
00060     void logEvent(const String& evt);
00061 
00062     String mName;
00063 };
00064 
00070 class InternalIOStrand : public boost::asio::io_service::strand {
00071 public:
00072     InternalIOStrand(IOService &io);
00073     InternalIOStrand(IOService* io);
00074 };
00075 
00076 
00077 typedef boost::asio::ip::tcp::socket InternalTCPSocket;
00078 typedef boost::asio::ip::tcp::acceptor InternalTCPAcceptor;
00079 typedef boost::asio::ip::tcp::resolver InternalTCPResolver;
00080 
00084 class SIRIKATA_EXPORT TCPSocket: public InternalTCPSocket {
00085   public:
00086     TCPSocket(IOService&io);
00087     TCPSocket(IOService* io);
00088     virtual ~TCPSocket(); // Users of subclasses may use TCPSocket interface directly
00089 };
00090 
00094 class SIRIKATA_EXPORT TCPListener :public InternalTCPAcceptor {
00095 public:
00096     TCPListener(IOService&io, const boost::asio::ip::tcp::endpoint&);
00097     TCPListener(IOService* io, const boost::asio::ip::tcp::endpoint&);
00098     virtual ~TCPListener(); // Users of subclasses may use TCPListener interface directly
00099 
00100     void async_accept(TCPSocket&socket,
00101                       const std::tr1::function<void(const boost::system::error_code& ) > &cb);
00102 };
00103 
00107 class SIRIKATA_EXPORT TCPResolver : public InternalTCPResolver {
00108   public:
00109     TCPResolver(IOService&io);
00110     TCPResolver(IOService* io);
00111     virtual ~TCPResolver(); // Users of subclasses may use TCPResolver interface directly
00112 };
00113 
00114 
00115 
00116 
00117 typedef boost::asio::ip::udp::socket InternalUDPSocket;
00118 typedef boost::asio::ip::udp::resolver InternalUDPResolver;
00119 
00123 class SIRIKATA_EXPORT UDPSocket: public InternalUDPSocket {
00124   public:
00125     UDPSocket(IOService&io);
00126     UDPSocket(IOService* io);
00127     virtual ~UDPSocket(); // Users of subclasses may use UDPSocket interface directly
00128 };
00129 
00133 class SIRIKATA_EXPORT UDPResolver : public InternalUDPResolver {
00134   public:
00135     UDPResolver(IOService&io);
00136     UDPResolver(IOService* io);
00137     virtual ~UDPResolver(); // Users of subclasses may use UDPResolver interface directly
00138 };
00139 
00146 class SIRIKATA_EXPORT DeadlineTimer : public boost::asio::deadline_timer {
00147 public:
00148     DeadlineTimer(IOService& io);
00149     DeadlineTimer(IOService* io);
00150     virtual ~DeadlineTimer();
00154     std::size_t cancel (boost::system::error_code &ec) {
00155         return this->boost::asio::deadline_timer::cancel(ec);
00156     }
00157 
00161     std::size_t cancel () {
00162         return this->boost::asio::deadline_timer::cancel();
00163     }
00164 };
00165 
00166 } // namespace Network
00167 } // namespace Sirikata
00168 
00169 #endif //_SIRIKATA_ASIO_HPP_