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