633798b14b1ac631a3c9495077fe11767d54b1a1
[libt2n] / src / connection.hxx
1 /***************************************************************************
2  *   Copyright (C) 2006 by Gerd v. Egidy                                   *
3  *   gve@intra2net.com                                                     *
4  *                                                                         *
5  *   This library is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU Lesser General Public License version   *
7  *   2.1 as published by the Free Software Foundation.                     *
8  *                                                                         *
9  *   This library is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU Lesser General Public License for more details.                   *
13  *                                                                         *
14  *   You should have received a copy of the GNU Lesser General Public      *
15  *   License along with this program; if not, write to the                 *
16  *   Free Software Foundation, Inc.,                                       *
17  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
18  ***************************************************************************/
19 #ifndef __LIBT2N_CONNECTION
20 #define __LIBT2N_CONNECTION
21
22 #include <string>
23
24 namespace libt2n
25 {
26
27 /** @brief a connection between client and server. abstact.
28 */
29 class connection
30 {
31     private:
32         bool closed;
33
34     protected:
35         connection()
36         { closed=false; }
37
38         std::string buffer;
39
40         typedef unsigned int packet_size_indicator;
41
42         packet_size_indicator bytes_available();
43
44         virtual void real_write(const std::string& data)=0;
45
46     public:
47         virtual ~connection()
48             { close(); }
49
50         /// is this connection closed or not
51         bool is_closed()
52             { return closed; }
53
54         /// close this connection
55         virtual void close()
56             { closed=true; }
57
58         /** @brief look for new data and store it in the local buffer
59             @param usec_timeout wait until new data is found, max timeout usecs.
60                   -1: wait endless
61                    NULL: no timeout
62             @retval true if new data was found (does not mean that the received data 
63                     is a complete packet though)
64         */
65         virtual bool fill_buffer(long long usec_timeout=-1)=0;
66         bool get_packet(std::string& data);
67
68         /// returns true if a complete data packet is in the buffer. retrieve it with get_packet().
69         bool packet_available()
70             { return bytes_available(); }
71
72         void write(const std::string& data);
73 };
74
75 }
76
77 #endif