2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
8 As a special exception, if other files instantiate templates or use macros
9 or inline functions from this file, or you compile this file and link it
10 with other works to produce a work based on this file, this file
11 does not by itself cause the resulting work to be covered
12 by the GNU General Public License.
14 However the source code for this file must still be made available
15 in accordance with section (3) of the GNU General Public License.
17 This exception does not invalidate any other reasons why a work based
18 on this file might be covered by the GNU General Public License.
21 * @brief socket classes for the AsyncIo framework.
23 * @copyright Copyright 2008-2009 by Intra2net AG
24 * @contact Intra2net Opensource Team \<opensource@intra2net.com\>
27 #ifndef __ASYNC_SOCKET_HPP__
28 #define __ASYNC_SOCKET_HPP__
30 #include "async_io.hpp"
31 #include "asyncio_system_tools.hpp"
34 #include <boost/any.hpp>
35 #include <boost/bind.hpp>
36 #include <boost/shared_ptr.hpp>
37 #include <boost/type_traits/is_base_of.hpp>
38 #include <boost/static_assert.hpp>
39 #include <boost/function.hpp>
46 typedef boost::shared_ptr< IOImplementation > IOImplementationPtr;
50 * @brief base class for server sockets.
52 * Contains all the stuff which is common for different types of server sockets.
54 class ServerSocketBaseImplementation
55 : public IOImplementation
58 typedef boost::function< void(IOImplementationPtr) > NewConnectionBaseCallbackFunc;
62 void setNewConnectionBaseCallback( const NewConnectionBaseCallbackFunc& func);
65 ServerSocketBaseImplementation();
68 virtual void doRead();
69 virtual void doWrite();
71 virtual IOImplementationPtr acceptNewConnection(int fd, boost::any addr);
76 NewConnectionBaseCallbackFunc m_new_connection_base_callback;
78 }; // eo class ServerSocketBaseImplementation
81 typedef boost::shared_ptr< ServerSocketBaseImplementation > ServerSocketBaseImplementationPtr;
85 ** unix domain sockets
92 class UnixServerSocket;
96 * @brief specialized IO class for unix domain sockets.
100 : public IOImplementation
104 UnixIOSocket(const std::string& path);
106 bool open(const std::string& path);
109 friend class UnixServerSocketBase;
110 friend class UnixServerSocket<UnixIOSocket>;
112 UnixIOSocket(int fd, const std::string& path);
114 bool update_peer_information(int fd);
119 unsigned int m_peer_pid;
120 unsigned int m_peer_uid;
121 unsigned int m_peer_gid;
123 }; // eo class UnixIOSocket
125 typedef boost::shared_ptr< UnixIOSocket > UnixIOSocketPtr;
130 * @brief specialized server socket class for unix domain sockets.
133 class UnixServerSocketBase
134 : public ServerSocketBaseImplementation
137 UnixServerSocketBase();
138 UnixServerSocketBase(const std::string& path, int mode=0600);
140 bool open(const std::string& path, int mode= 0600);
144 virtual IOImplementationPtr acceptNewConnection(int fd, boost::any addr);
146 virtual UnixIOSocketPtr createIOSocket(int fd, const std::string& path);
152 }; // eo class UnixServerSocketBase
156 * @brief unix server socket class which "produces" connections of a determined type.
158 * @param IOClass the type of the connections.
163 class UnixServerSocket
164 : public UnixServerSocketBase
166 BOOST_STATIC_ASSERT(( boost::is_base_of<UnixIOSocket,IOClass>::value ));
169 typedef boost::shared_ptr< IOClass > IOClassPtr;
170 typedef boost::function< void(IOClassPtr) > NewConnectionCallbackFunc;
175 : UnixServerSocketBase()
178 UnixServerSocket(const std::string& path, int mode=0600)
179 : UnixServerSocketBase(path,mode)
182 void setNewConnectionCallback( const NewConnectionCallbackFunc& func)
186 UnixServerSocketBase::setNewConnectionBaseCallback(
189 boost::bind<IOClassPtr, IOImplementationPtr>(
190 &UnixServerSocket::my_ptr_cast,
198 UnixServerSocketBase::setNewConnectionBaseCallback(
199 NewConnectionBaseCallbackFunc()
206 virtual UnixIOSocketPtr createIOSocket(
207 int fd, const std::string& path)
209 return UnixIOSocketPtr(
210 new IOClass(fd, path)
214 static IOClassPtr my_ptr_cast(IOImplementationPtr ptr)
216 return boost::dynamic_pointer_cast<IOClass>(ptr);
219 }; // eo class UnixServerSocket
223 }// end of namespace AsyncIo