1d60aa3a3703b0cf35aec168a269daaf9e1402eb
[libasyncio] / asyncio / async_socket.hpp
1 /** @file
2  * @brief socket classes for the AsyncIo framework.
3  *
4  *
5  * (c) Copyright 2008 by Intra2net AG
6  * 
7  * info@intra2net.com
8  */
9
10 #ifndef __SIMPLEIO__SIMPLESOCKET_HPP__
11 #define __SIMPLEIO__SIMPLESOCKET_HPP__
12
13 #include "async_io.hpp"
14
15 #include <string>
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>
22
23
24 namespace AsyncIo
25 {
26
27
28 typedef boost::shared_ptr< IOImplementation > IOImplementationPtr;
29
30
31 /**
32  * @brief base class for server sockets.
33  *
34  * Contains all the stuff which is common for different types of server sockets.
35  */
36 class ServerSocketBaseImplementation
37 : public IOImplementation
38 {
39    public:
40       typedef boost::function< void(IOImplementationPtr) > NewConnectionBaseCallbackFunc;
41
42    public:
43
44       void setNewConnectionBaseCallback( const NewConnectionBaseCallbackFunc& func);
45
46    protected:
47       ServerSocketBaseImplementation();
48
49
50       virtual void doRead();
51       virtual void doWrite();
52
53       virtual IOImplementationPtr acceptNewConnection(int fd, boost::any addr);
54
55
56    protected:
57
58       NewConnectionBaseCallbackFunc m_new_connection_base_callback;
59
60 }; // eo class ServerSocketBaseImplementation
61
62
63 typedef boost::shared_ptr< ServerSocketBaseImplementation > ServerSocketBaseImplementationPtr;
64
65
66 /*
67 ** unix domain sockets
68 */
69
70
71 template<
72    class IOClass
73 >
74 class UnixServerSocket;
75
76
77 /**
78  * @brief spezialized IO class for unix domain sockets.
79  *
80  */
81 class UnixIOSocket
82 : public IOImplementation
83 {
84    public:
85       UnixIOSocket();
86       UnixIOSocket(const std::string& path);
87
88       bool open(const std::string& path);
89
90    protected:
91       friend class UnixServerSocketBase;
92       friend class UnixServerSocket<UnixIOSocket>;
93
94       UnixIOSocket(
95          int fd, const std::string& path,
96          unsigned int peer_pid, unsigned int peer_uid, unsigned int peer_gid);
97
98    protected:
99
100       std::string  m_path;
101       unsigned int m_peer_pid;
102       unsigned int m_peer_uid;
103       unsigned int m_peer_gid;
104
105 }; // eo class UnixIOSocket
106
107 typedef boost::shared_ptr< UnixIOSocket > UnixIOSocketPtr;
108
109
110
111 /**
112  * @brief spezialized server socket class for unix domain sockets.
113  *
114  */
115 class UnixServerSocketBase
116 : public ServerSocketBaseImplementation
117 {
118    public:
119       UnixServerSocketBase();
120       UnixServerSocketBase(const std::string& path, int mode=0600);
121
122       bool open(const std::string& path, int mode= 0600);
123
124    protected:
125
126       virtual IOImplementationPtr acceptNewConnection(int fd, boost::any addr);
127
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);
132
133    protected:
134
135       std::string m_path;
136
137 }; // eo class UnixServerSocketBase
138
139
140 /**
141  * @brief unix server socket class which "produces" connections of a determined type.
142  *
143  + @param IOClass the type of the connections.
144  */
145 template<
146    class IOClass
147 >
148 class UnixServerSocket
149 : public UnixServerSocketBase
150 {
151       BOOST_STATIC_ASSERT(( boost::is_base_of<UnixIOSocket,IOClass>::value ));
152
153    public:
154       typedef boost::shared_ptr< IOClass > IOClassPtr;
155       typedef boost::function< void(IOClassPtr) > NewConnectionCallbackFunc;
156
157    public:
158
159       UnixServerSocket()
160       : UnixServerSocketBase()
161       {}
162
163       UnixServerSocket(const std::string& path, int mode=0600)
164       : UnixServerSocketBase(path,mode)
165       {}
166
167       void setNewConnectionCallback( const NewConnectionCallbackFunc& func)
168       {
169          if (func)
170          {
171             UnixServerSocketBase::setNewConnectionBaseCallback(
172                boost::bind(
173                   func,
174                   boost::bind<IOClassPtr, IOImplementationPtr>(
175                      &UnixServerSocket::my_ptr_cast,
176                      _1
177                   )
178                )
179             );
180          }
181          else
182          {
183             UnixServerSocketBase::setNewConnectionBaseCallback(
184                NewConnectionBaseCallbackFunc()
185             );
186          }
187       }
188
189    protected:
190
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)
195       {
196          return UnixIOSocketPtr(
197             new IOClass(fd, path, peer_pid, peer_uid, peer_gid)
198          );
199       }
200
201       static IOClassPtr my_ptr_cast(IOImplementationPtr ptr)
202       {
203          return boost::dynamic_pointer_cast<IOClass>(ptr);
204       }
205
206 }; // eo class UnixServerSocket
207
208
209 }// eo namespace AsyncIo
210
211
212 #endif