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 * (c) Copyright 2008 by Intra2net AG
26 #ifndef __SIMPLEIO__SIMPLESOCKET_HPP__
27 #define __SIMPLEIO__SIMPLESOCKET_HPP__
29 #include "async_io.hpp"
32 #include <boost/any.hpp>
33 #include <boost/bind.hpp>
34 #include <boost/shared_ptr.hpp>
35 #include <boost/type_traits/is_base_of.hpp>
36 #include <boost/static_assert.hpp>
37 #include <boost/function.hpp>
44 typedef boost::shared_ptr< IOImplementation > IOImplementationPtr;
48 * @brief base class for server sockets.
50 * Contains all the stuff which is common for different types of server sockets.
52 class ServerSocketBaseImplementation
53 : public IOImplementation
56 typedef boost::function< void(IOImplementationPtr) > NewConnectionBaseCallbackFunc;
60 void setNewConnectionBaseCallback( const NewConnectionBaseCallbackFunc& func);
63 ServerSocketBaseImplementation();
66 virtual void doRead();
67 virtual void doWrite();
69 virtual IOImplementationPtr acceptNewConnection(int fd, boost::any addr);
74 NewConnectionBaseCallbackFunc m_new_connection_base_callback;
76 }; // eo class ServerSocketBaseImplementation
79 typedef boost::shared_ptr< ServerSocketBaseImplementation > ServerSocketBaseImplementationPtr;
83 ** unix domain sockets
90 class UnixServerSocket;
94 * @brief spezialized IO class for unix domain sockets.
98 : public IOImplementation
102 UnixIOSocket(const std::string& path);
104 bool open(const std::string& path);
107 friend class UnixServerSocketBase;
108 friend class UnixServerSocket<UnixIOSocket>;
111 int fd, const std::string& path,
112 unsigned int peer_pid, unsigned int peer_uid, unsigned int peer_gid);
117 unsigned int m_peer_pid;
118 unsigned int m_peer_uid;
119 unsigned int m_peer_gid;
121 }; // eo class UnixIOSocket
123 typedef boost::shared_ptr< UnixIOSocket > UnixIOSocketPtr;
128 * @brief spezialized server socket class for unix domain sockets.
131 class UnixServerSocketBase
132 : public ServerSocketBaseImplementation
135 UnixServerSocketBase();
136 UnixServerSocketBase(const std::string& path, int mode=0600);
138 bool open(const std::string& path, int mode= 0600);
142 virtual IOImplementationPtr acceptNewConnection(int fd, boost::any addr);
144 virtual UnixIOSocketPtr createIOSocket(
145 int fd, const std::string& path,
146 unsigned int peer_pid,
147 unsigned int peer_uid, unsigned int peer_gid);
153 }; // eo class UnixServerSocketBase
157 * @brief unix server socket class which "produces" connections of a determined type.
159 + @param IOClass the type of the connections.
164 class UnixServerSocket
165 : public UnixServerSocketBase
167 BOOST_STATIC_ASSERT(( boost::is_base_of<UnixIOSocket,IOClass>::value ));
170 typedef boost::shared_ptr< IOClass > IOClassPtr;
171 typedef boost::function< void(IOClassPtr) > NewConnectionCallbackFunc;
176 : UnixServerSocketBase()
179 UnixServerSocket(const std::string& path, int mode=0600)
180 : UnixServerSocketBase(path,mode)
183 void setNewConnectionCallback( const NewConnectionCallbackFunc& func)
187 UnixServerSocketBase::setNewConnectionBaseCallback(
190 boost::bind<IOClassPtr, IOImplementationPtr>(
191 &UnixServerSocket::my_ptr_cast,
199 UnixServerSocketBase::setNewConnectionBaseCallback(
200 NewConnectionBaseCallbackFunc()
207 virtual UnixIOSocketPtr createIOSocket(
208 int fd, const std::string& path,
209 unsigned int peer_pid,
210 unsigned int peer_uid, unsigned int peer_gid)
212 return UnixIOSocketPtr(
213 new IOClass(fd, path, peer_pid, peer_uid, peer_gid)
217 static IOClassPtr my_ptr_cast(IOImplementationPtr ptr)
219 return boost::dynamic_pointer_cast<IOClass>(ptr);
222 }; // eo class UnixServerSocket
225 }// eo namespace AsyncIo