2 * @brief socket classes for the AsyncIo framework.
5 * (c) Copyright 2008 by Intra2net AG
10 #ifndef __SIMPLEIO__SIMPLESOCKET_HPP__
11 #define __SIMPLEIO__SIMPLESOCKET_HPP__
13 #include "async_io.hpp"
16 #include <boost/any.hpp>
17 #include <boost/bind.hpp>
18 #include <boost/shared_ptr.hpp>
19 #include <boost/type_traits/is_base_of.hpp>
20 #include <boost/static_assert.hpp>
21 #include <boost/function.hpp>
28 typedef boost::shared_ptr< IOImplementation > IOImplementationPtr;
32 * @brief base class for server sockets.
34 * Contains all the stuff which is common for different types of server sockets.
36 class ServerSocketBaseImplementation
37 : public IOImplementation
40 typedef boost::function< void(IOImplementationPtr) > NewConnectionBaseCallbackFunc;
44 void setNewConnectionBaseCallback( const NewConnectionBaseCallbackFunc& func);
47 ServerSocketBaseImplementation();
50 virtual void doRead();
51 virtual void doWrite();
53 virtual IOImplementationPtr acceptNewConnection(int fd, boost::any addr);
58 NewConnectionBaseCallbackFunc m_new_connection_base_callback;
60 }; // eo class ServerSocketBaseImplementation
63 typedef boost::shared_ptr< ServerSocketBaseImplementation > ServerSocketBaseImplementationPtr;
67 ** unix domain sockets
74 class UnixServerSocket;
78 * @brief spezialized IO class for unix domain sockets.
82 : public IOImplementation
86 UnixIOSocket(const std::string& path);
88 bool open(const std::string& path);
91 friend class UnixServerSocketBase;
92 friend class UnixServerSocket<UnixIOSocket>;
95 int fd, const std::string& path,
96 unsigned int peer_pid, unsigned int peer_uid, unsigned int peer_gid);
101 unsigned int m_peer_pid;
102 unsigned int m_peer_uid;
103 unsigned int m_peer_gid;
105 }; // eo class UnixIOSocket
107 typedef boost::shared_ptr< UnixIOSocket > UnixIOSocketPtr;
112 * @brief spezialized server socket class for unix domain sockets.
115 class UnixServerSocketBase
116 : public ServerSocketBaseImplementation
119 UnixServerSocketBase();
120 UnixServerSocketBase(const std::string& path, int mode=0600);
122 bool open(const std::string& path, int mode= 0600);
126 virtual IOImplementationPtr acceptNewConnection(int fd, boost::any addr);
128 virtual UnixIOSocketPtr createIOSocket(
129 int fd, const std::string& path,
130 unsigned int peer_pid,
131 unsigned int peer_uid, unsigned int peer_gid);
137 }; // eo class UnixServerSocketBase
141 * @brief unix server socket class which "produces" connections of a determined type.
143 + @param IOClass the type of the connections.
148 class UnixServerSocket
149 : public UnixServerSocketBase
151 BOOST_STATIC_ASSERT(( boost::is_base_of<UnixIOSocket,IOClass>::value ));
154 typedef boost::shared_ptr< IOClass > IOClassPtr;
155 typedef boost::function< void(IOClassPtr) > NewConnectionCallbackFunc;
160 : UnixServerSocketBase()
163 UnixServerSocket(const std::string& path, int mode=0600)
164 : UnixServerSocketBase(path,mode)
167 void setNewConnectionCallback( const NewConnectionCallbackFunc& func)
171 UnixServerSocketBase::setNewConnectionBaseCallback(
174 boost::bind<IOClassPtr, IOImplementationPtr>(
175 &UnixServerSocket::my_ptr_cast,
183 UnixServerSocketBase::setNewConnectionBaseCallback(
184 NewConnectionBaseCallbackFunc()
191 virtual UnixIOSocketPtr createIOSocket(
192 int fd, const std::string& path,
193 unsigned int peer_pid,
194 unsigned int peer_uid, unsigned int peer_gid)
196 return UnixIOSocketPtr(
197 new IOClass(fd, path, peer_pid, peer_uid, peer_gid)
201 static IOClassPtr my_ptr_cast(IOImplementationPtr ptr)
203 return boost::dynamic_pointer_cast<IOClass>(ptr);
206 }; // eo class UnixServerSocket
209 }// eo namespace AsyncIo