libt2n: (gerd) basic command handling (still some todos)
[libt2n] / src / socket_handler.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_SOCKET_HANDLER
20 #define __LIBT2N_SOCKET_HANDLER
21
22 #include "types.hxx"
23
24 namespace libt2n
25 {
26
27 /** @brief handles socket based communication.
28     Don't use directly, use socket_server or socket_client_connection instead.
29 */
30 class socket_handler
31 {
32     private:
33         static const unsigned int recv_buffer_size=2048;
34         static const unsigned int write_block_size=4096;
35
36         socket_type_value socket_type;
37
38         bool data_waiting(long long usec_timeout=-1);
39
40     protected:
41         int sock;
42
43         socket_handler(int _sock, socket_type_value _socket_type)
44             { sock=_sock; socket_type=_socket_type; }
45
46         void set_socket_options(int sock);
47
48         void log(log_level_values level, const std::string& message)
49             { log(level,message.c_str()); }
50         virtual void log(log_level_values level, const char* message)
51             { return; }
52
53         void socket_write(const std::string& data);
54
55         virtual void close();
56
57         bool fill_buffer(std::string& buffer, long long usec_timeout);
58         bool fill_buffer(std::string& buffer);
59
60     public:
61         /// is this a tcp or udp socket connection
62         socket_type_value get_type()
63             { return socket_type; }
64
65         bool is_closed();
66 };
67
68 }
69
70 #endif