Migrate libasyncio from boost.signal to signals2 (#8756)
[libasyncio] / asyncio / async_socket.hpp
CommitLineData
8c15b8c7
TJ
1/*
2The software in this package is distributed under the GNU General
3Public License version 2 (with a special exception described below).
4
5A copy of GNU General Public License (GPL) is included in this distribution,
6in the file COPYING.GPL.
7
8As a special exception, if other files instantiate templates or use macros
9or inline functions from this file, or you compile this file and link it
10with other works to produce a work based on this file, this file
11does not by itself cause the resulting work to be covered
12by the GNU General Public License.
13
14However the source code for this file must still be made available
15in accordance with section (3) of the GNU General Public License.
16
17This exception does not invalidate any other reasons why a work based
18on this file might be covered by the GNU General Public License.
19*/
5c8a3d40 20/** @file
42b7c46d 21 * @brief socket classes for the AsyncIo framework.
5c8a3d40 22 *
6ac1fb46
RP
23 * @copyright Copyright 2008-2009 by Intra2net AG
24 * @contact Intra2net Opensource Team \<opensource@intra2net.com\>
5c8a3d40
RP
25 */
26
6ac1fb46
RP
27#ifndef __ASYNC_SOCKET_HPP__
28#define __ASYNC_SOCKET_HPP__
5c8a3d40 29
42b7c46d 30#include "async_io.hpp"
6ac1fb46 31#include "asyncio_system_tools.hpp"
5c8a3d40
RP
32
33#include <string>
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>
40
41
42b7c46d 42namespace AsyncIo
5c8a3d40
RP
43{
44
45
46typedef boost::shared_ptr< IOImplementation > IOImplementationPtr;
47
48
49/**
50 * @brief base class for server sockets.
51 *
52 * Contains all the stuff which is common for different types of server sockets.
53 */
54class ServerSocketBaseImplementation
55: public IOImplementation
56{
57 public:
58 typedef boost::function< void(IOImplementationPtr) > NewConnectionBaseCallbackFunc;
59
60 public:
61
62 void setNewConnectionBaseCallback( const NewConnectionBaseCallbackFunc& func);
63
64 protected:
65 ServerSocketBaseImplementation();
66
67
68 virtual void doRead();
69 virtual void doWrite();
70
71 virtual IOImplementationPtr acceptNewConnection(int fd, boost::any addr);
72
73
74 protected:
75
76 NewConnectionBaseCallbackFunc m_new_connection_base_callback;
77
78}; // eo class ServerSocketBaseImplementation
79
80
81typedef boost::shared_ptr< ServerSocketBaseImplementation > ServerSocketBaseImplementationPtr;
82
83
84/*
85** unix domain sockets
86*/
87
88
89template<
90 class IOClass
91>
92class UnixServerSocket;
93
94
95/**
6ac1fb46 96 * @brief specialized IO class for unix domain sockets.
5c8a3d40
RP
97 *
98 */
99class UnixIOSocket
100: public IOImplementation
101{
102 public:
103 UnixIOSocket();
104 UnixIOSocket(const std::string& path);
105
106 bool open(const std::string& path);
107
108 protected:
109 friend class UnixServerSocketBase;
110 friend class UnixServerSocket<UnixIOSocket>;
111
6ac1fb46
RP
112 UnixIOSocket(int fd, const std::string& path);
113
114 bool update_peer_information(int fd);
5c8a3d40
RP
115
116 protected:
117
118 std::string m_path;
119 unsigned int m_peer_pid;
120 unsigned int m_peer_uid;
121 unsigned int m_peer_gid;
122
123}; // eo class UnixIOSocket
124
125typedef boost::shared_ptr< UnixIOSocket > UnixIOSocketPtr;
126
127
128
129/**
6ac1fb46 130 * @brief specialized server socket class for unix domain sockets.
5c8a3d40
RP
131 *
132 */
133class UnixServerSocketBase
134: public ServerSocketBaseImplementation
135{
136 public:
137 UnixServerSocketBase();
138 UnixServerSocketBase(const std::string& path, int mode=0600);
139
140 bool open(const std::string& path, int mode= 0600);
141
142 protected:
143
144 virtual IOImplementationPtr acceptNewConnection(int fd, boost::any addr);
145
6ac1fb46 146 virtual UnixIOSocketPtr createIOSocket(int fd, const std::string& path);
5c8a3d40
RP
147
148 protected:
149
150 std::string m_path;
151
152}; // eo class UnixServerSocketBase
153
154
155/**
156 * @brief unix server socket class which "produces" connections of a determined type.
157 *
6ac1fb46 158 * @param IOClass the type of the connections.
5c8a3d40
RP
159 */
160template<
161 class IOClass
162>
163class UnixServerSocket
164: public UnixServerSocketBase
165{
166 BOOST_STATIC_ASSERT(( boost::is_base_of<UnixIOSocket,IOClass>::value ));
167
168 public:
169 typedef boost::shared_ptr< IOClass > IOClassPtr;
170 typedef boost::function< void(IOClassPtr) > NewConnectionCallbackFunc;
171
172 public:
173
174 UnixServerSocket()
175 : UnixServerSocketBase()
176 {}
177
178 UnixServerSocket(const std::string& path, int mode=0600)
179 : UnixServerSocketBase(path,mode)
180 {}
181
182 void setNewConnectionCallback( const NewConnectionCallbackFunc& func)
183 {
184 if (func)
185 {
186 UnixServerSocketBase::setNewConnectionBaseCallback(
187 boost::bind(
188 func,
189 boost::bind<IOClassPtr, IOImplementationPtr>(
190 &UnixServerSocket::my_ptr_cast,
191 _1
192 )
193 )
194 );
195 }
196 else
197 {
198 UnixServerSocketBase::setNewConnectionBaseCallback(
199 NewConnectionBaseCallbackFunc()
200 );
201 }
202 }
203
204 protected:
205
206 virtual UnixIOSocketPtr createIOSocket(
6ac1fb46 207 int fd, const std::string& path)
5c8a3d40
RP
208 {
209 return UnixIOSocketPtr(
6ac1fb46 210 new IOClass(fd, path)
5c8a3d40
RP
211 );
212 }
213
214 static IOClassPtr my_ptr_cast(IOImplementationPtr ptr)
215 {
216 return boost::dynamic_pointer_cast<IOClass>(ptr);
217 }
218
219}; // eo class UnixServerSocket
220
221
6ac1fb46
RP
222
223}// end of namespace AsyncIo
5c8a3d40
RP
224
225
226#endif