Commit | Line | Data |
---|---|---|
19facd85 TJ |
1 | /* |
2 | Copyright (C) 2006 by Intra2net AG - Gerd v. Egidy | |
3 | ||
4 | The software in this package is distributed under the GNU General | |
5 | Public License version 2 (with a special exception described below). | |
6 | ||
7 | A copy of GNU General Public License (GPL) is included in this distribution, | |
8 | in the file COPYING.GPL. | |
9 | ||
10 | As a special exception, if other files instantiate templates or use macros | |
11 | or inline functions from this file, or you compile this file and link it | |
12 | with other works to produce a work based on this file, this file | |
13 | does not by itself cause the resulting work to be covered | |
14 | by the GNU General Public License. | |
15 | ||
16 | However the source code for this file must still be made available | |
17 | in accordance with section (3) of the GNU General Public License. | |
18 | ||
19 | This exception does not invalidate any other reasons why a work based | |
20 | on this file might be covered by the GNU General Public License. | |
21 | */ | |
a11e19b7 GE |
22 | #ifndef __LIBT2N_SOCKET_HANDLER |
23 | #define __LIBT2N_SOCKET_HANDLER | |
24 | ||
a7170401 GE |
25 | #include <iostream> |
26 | ||
a11e19b7 GE |
27 | #include "types.hxx" |
28 | ||
29 | namespace libt2n | |
30 | { | |
31 | ||
94247295 GE |
32 | /** @brief handles socket based communication. |
33 | Don't use directly, use socket_server or socket_client_connection instead. | |
34 | */ | |
a11e19b7 GE |
35 | class socket_handler |
36 | { | |
37 | private: | |
517d1214 RP |
38 | static const unsigned int default_recv_buffer_size=2048; |
39 | static const unsigned int default_write_block_size=4096; | |
c7857475 | 40 | static const long long default_write_timeout=30000000; |
a11e19b7 GE |
41 | |
42 | socket_type_value socket_type; | |
43 | ||
9a5d7790 | 44 | bool data_waiting(long long usec_timeout,long long *usec_timeout_remaining=NULL); |
c7857475 | 45 | void wait_ready_to_write(int socket, long long write_block_timeout); |
a11e19b7 GE |
46 | |
47 | protected: | |
48 | int sock; | |
517d1214 RP |
49 | unsigned int recv_buffer_size; |
50 | unsigned int write_block_size; | |
c7857475 | 51 | long long write_timeout; |
a11e19b7 | 52 | |
517d1214 | 53 | socket_handler(int _sock, socket_type_value _socket_type); |
56f3994d | 54 | ~socket_handler(); |
a11e19b7 GE |
55 | |
56 | void set_socket_options(int sock); | |
57 | ||
a7170401 GE |
58 | virtual std::ostream* get_logstream(log_level_values level) |
59 | { return NULL; } | |
a11e19b7 | 60 | |
94247295 GE |
61 | void socket_write(const std::string& data); |
62 | ||
63 | virtual void close(); | |
a11e19b7 | 64 | |
9a5d7790 | 65 | bool fill_buffer(std::string& buffer, long long usec_timeout, long long* usec_timeout_remaining=NULL); |
a11e19b7 GE |
66 | bool fill_buffer(std::string& buffer); |
67 | ||
94247295 | 68 | public: |
517d1214 | 69 | /// is this a tcp or unix socket connection |
94247295 GE |
70 | socket_type_value get_type() |
71 | { return socket_type; } | |
a11e19b7 | 72 | |
644c4d26 | 73 | bool is_closed(); |
517d1214 RP |
74 | |
75 | void set_recv_buffer_size(unsigned int new_recv_buffer_size); | |
76 | void set_write_block_size(unsigned int new_write_block_size); | |
c7857475 | 77 | void set_write_timeout(long long new_write_timeout); |
517d1214 RP |
78 | |
79 | unsigned int get_recv_buffer_size() const { return recv_buffer_size; } | |
80 | unsigned int get_write_block_size() const { return write_block_size; } | |
c7857475 | 81 | long long get_write_timeout() const { return write_timeout; } |
a11e19b7 GE |
82 | }; |
83 | ||
84 | } | |
85 | ||
86 | #endif |