Properly fix the license for C++ template usage. This means we needed to change from...
[libasyncio] / asyncio / async_socket.hpp
1 /*
2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
4
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
7
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.
13
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.
16
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.
19 */
20 /** @file
21  * @brief socket classes for the AsyncIo framework.
22  *
23  * (c) Copyright 2008 by Intra2net AG
24  */
25
26 #ifndef __SIMPLEIO__SIMPLESOCKET_HPP__
27 #define __SIMPLEIO__SIMPLESOCKET_HPP__
28
29 #include "async_io.hpp"
30
31 #include <string>
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>
38
39
40 namespace AsyncIo
41 {
42
43
44 typedef boost::shared_ptr< IOImplementation > IOImplementationPtr;
45
46
47 /**
48  * @brief base class for server sockets.
49  *
50  * Contains all the stuff which is common for different types of server sockets.
51  */
52 class ServerSocketBaseImplementation
53 : public IOImplementation
54 {
55    public:
56       typedef boost::function< void(IOImplementationPtr) > NewConnectionBaseCallbackFunc;
57
58    public:
59
60       void setNewConnectionBaseCallback( const NewConnectionBaseCallbackFunc& func);
61
62    protected:
63       ServerSocketBaseImplementation();
64
65
66       virtual void doRead();
67       virtual void doWrite();
68
69       virtual IOImplementationPtr acceptNewConnection(int fd, boost::any addr);
70
71
72    protected:
73
74       NewConnectionBaseCallbackFunc m_new_connection_base_callback;
75
76 }; // eo class ServerSocketBaseImplementation
77
78
79 typedef boost::shared_ptr< ServerSocketBaseImplementation > ServerSocketBaseImplementationPtr;
80
81
82 /*
83 ** unix domain sockets
84 */
85
86
87 template<
88    class IOClass
89 >
90 class UnixServerSocket;
91
92
93 /**
94  * @brief spezialized IO class for unix domain sockets.
95  *
96  */
97 class UnixIOSocket
98 : public IOImplementation
99 {
100    public:
101       UnixIOSocket();
102       UnixIOSocket(const std::string& path);
103
104       bool open(const std::string& path);
105
106    protected:
107       friend class UnixServerSocketBase;
108       friend class UnixServerSocket<UnixIOSocket>;
109
110       UnixIOSocket(
111          int fd, const std::string& path,
112          unsigned int peer_pid, unsigned int peer_uid, unsigned int peer_gid);
113
114    protected:
115
116       std::string  m_path;
117       unsigned int m_peer_pid;
118       unsigned int m_peer_uid;
119       unsigned int m_peer_gid;
120
121 }; // eo class UnixIOSocket
122
123 typedef boost::shared_ptr< UnixIOSocket > UnixIOSocketPtr;
124
125
126
127 /**
128  * @brief spezialized server socket class for unix domain sockets.
129  *
130  */
131 class UnixServerSocketBase
132 : public ServerSocketBaseImplementation
133 {
134    public:
135       UnixServerSocketBase();
136       UnixServerSocketBase(const std::string& path, int mode=0600);
137
138       bool open(const std::string& path, int mode= 0600);
139
140    protected:
141
142       virtual IOImplementationPtr acceptNewConnection(int fd, boost::any addr);
143
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);
148
149    protected:
150
151       std::string m_path;
152
153 }; // eo class UnixServerSocketBase
154
155
156 /**
157  * @brief unix server socket class which "produces" connections of a determined type.
158  *
159  + @param IOClass the type of the connections.
160  */
161 template<
162    class IOClass
163 >
164 class UnixServerSocket
165 : public UnixServerSocketBase
166 {
167       BOOST_STATIC_ASSERT(( boost::is_base_of<UnixIOSocket,IOClass>::value ));
168
169    public:
170       typedef boost::shared_ptr< IOClass > IOClassPtr;
171       typedef boost::function< void(IOClassPtr) > NewConnectionCallbackFunc;
172
173    public:
174
175       UnixServerSocket()
176       : UnixServerSocketBase()
177       {}
178
179       UnixServerSocket(const std::string& path, int mode=0600)
180       : UnixServerSocketBase(path,mode)
181       {}
182
183       void setNewConnectionCallback( const NewConnectionCallbackFunc& func)
184       {
185          if (func)
186          {
187             UnixServerSocketBase::setNewConnectionBaseCallback(
188                boost::bind(
189                   func,
190                   boost::bind<IOClassPtr, IOImplementationPtr>(
191                      &UnixServerSocket::my_ptr_cast,
192                      _1
193                   )
194                )
195             );
196          }
197          else
198          {
199             UnixServerSocketBase::setNewConnectionBaseCallback(
200                NewConnectionBaseCallbackFunc()
201             );
202          }
203       }
204
205    protected:
206
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)
211       {
212          return UnixIOSocketPtr(
213             new IOClass(fd, path, peer_pid, peer_uid, peer_gid)
214          );
215       }
216
217       static IOClassPtr my_ptr_cast(IOImplementationPtr ptr)
218       {
219          return boost::dynamic_pointer_cast<IOClass>(ptr);
220       }
221
222 }; // eo class UnixServerSocket
223
224
225 }// eo namespace AsyncIo
226
227
228 #endif