31c796c3b89ff90ae51ecb557c5f4a3f68ed16f0
[libasyncio] / glue_t2n / asyncio_t2n.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 /**
21  * @file
22  * @brief the "glue" between libt2n and AsyncIo framework.
23  *
24  * contains our own server and client class which should fit into the asnychronous way of "AsyncIo".
25  * We use our own classes since the libt2n socket classes are made for synchronous operation
26  * which can lead to problems even if we import the connection fd's into "AsyncIo"...
27  *
28  * @author Reinhard Pfau \<reinhard.pfau@intra2net.com\>
29  *
30  * @copyright &copy; Copyright 2008 by Intra2net AG
31  *
32  * @todo support for TCP/IP connections.
33  */
34
35 #ifndef __CONND_GLUE_T2N_HPP__
36 #define __CONND_GLUE_T2N_HPP__
37
38 #include <string>
39 #include <server.hxx>
40 #include <client.hxx>
41 #include <async_io.hpp>
42 #include <async_socket.hpp>
43 #include <pointer_func.hpp>
44 #include <boost/shared_ptr.hpp>
45 #include <boost/weak_ptr.hpp>
46 #include <boost/signal.hpp>
47
48
49 namespace AsyncIo
50 {
51
52 using namespace I2n;
53
54 /**
55  * @brief specialized version of the libt2n client connection which fits into the AsyncIo framework.
56  *
57  */
58 class T2NClientConnection
59 : public libt2n::client_connection
60 {
61     public:
62
63         T2NClientConnection();
64         virtual ~T2NClientConnection();
65
66         bool isOpen();
67
68         bool reopen(bool force= false);
69
70
71         /*
72         ** overloaded methods from libt2n classes:
73         */
74
75         virtual void close();
76
77     protected:
78
79         T2NClientConnection( AsyncIo::IOImplementationPtr connection );
80
81         void newDataSlot();
82
83         void eofSlot();
84
85         /*
86         ** overloaded methods from t2n classes:
87         */
88
89         virtual void real_write(const std::string& data);
90
91         virtual bool fill_buffer(long long usec_timeout=-1,long long* usec_timeout_remaining=NULL);
92
93     protected:
94
95         AsyncIo::IOImplementationPtr m_real_connection;
96
97         bool m_got_new_data;
98
99 }; // eo class T2NClientConnection
100
101
102 typedef boost::shared_ptr< T2NClientConnection > T2NClientConnectionPtr;
103
104
105 class T2NServerBase;
106
107 typedef boost::shared_ptr< T2NServerBase > T2NServerBasePtr;
108 typedef boost::weak_ptr< T2NServerBase > T2NServerBaseWeakPtr;
109
110 /**
111  * @brief specialized version of the libt2n server connection which fits into the AsyncIo framework.
112  *
113  */
114 class T2NServerConnection
115 : public libt2n::server_connection
116 {
117         friend class T2NServerBase;
118     public:
119
120         T2NServerConnection();
121         virtual ~T2NServerConnection();
122
123
124         /*
125         ** overloaded methods from libt2n classes:
126         */
127
128         virtual void close();
129
130     protected:
131
132         T2NServerConnection(
133             T2NServerBasePtr server,
134             AsyncIo::IOImplementationPtr connection,
135             int timeout);
136
137         void newDataSlot();
138
139         void eofSlot();
140
141         bool low_fill_buffer(bool wait, long long usec_timeout=-1, long long* usec_timeout_remaining=NULL);
142
143         /*
144         ** overloaded methods from t2n classes:
145         */
146
147         virtual void real_write(const std::string& data);
148
149         virtual bool fill_buffer(long long usec_timeout=-1,long long* usec_timeout_remaining=NULL);
150
151
152     protected:
153
154         AsyncIo::IOImplementationPtr m_real_connection;
155         T2NServerBaseWeakPtr m_server_weak_ptr;
156
157         bool m_got_new_data;
158
159 }; // eo class T2NServerConnection
160
161
162
163 /**
164  * @brief base server class for handling server ports for libt2n.
165  *
166  * Needs to be derived for the real type of connection (unix, IPv4, etc.).
167  *
168  * Does all necessary connection handling and realizes the abstract methods from
169  * the libt2n::server class.
170  */
171 class T2NServerBase
172 : public libt2n::server
173 , virtual public SharedBase
174 {
175     public:
176         virtual ~T2NServerBase();
177
178         bool isOpen();
179
180         /*
181         ** overloaded methods from t2n classes:
182         */
183
184         virtual bool fill_buffer(long long usec_timeout=-1, long long* timeout_remaining=NULL);
185
186     public:
187
188         boost::signal< void() > m_signal_client_got_new_data;
189
190     protected:
191
192         T2NServerBase( AsyncIo::ServerSocketBaseImplementationPtr server_port);
193
194         void newConnectionSlot(AsyncIo::IOImplementationPtr io_ptr);
195
196         void clientGotNewDataSlot();
197
198         /*
199         ** overloaded methods from t2n classes:
200         */
201
202         virtual bool fill_connection_buffers(void);
203
204
205     protected:
206
207         AsyncIo::ServerSocketBaseImplementationPtr m_server_port;
208         bool m_new_data_available;
209
210 }; // eo T2NServerBase
211
212
213 typedef boost::shared_ptr< T2NServerBase > T2NServerBasePtr;
214
215
216 T2NServerBasePtr createT2NUnixServerPort(const std::string& path, int mode= 0600);
217
218 T2NClientConnectionPtr createT2NUnixClientConnection(const std::string& path);
219
220
221 } // eo namespace AsyncIo
222
223 #endif