| 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 | */ |
| 22 | #ifndef __LIBT2N_SOCKET_HANDLER |
| 23 | #define __LIBT2N_SOCKET_HANDLER |
| 24 | |
| 25 | #include <iostream> |
| 26 | |
| 27 | #include "types.hxx" |
| 28 | |
| 29 | namespace libt2n |
| 30 | { |
| 31 | |
| 32 | /** @brief handles socket based communication. |
| 33 | Don't use directly, use socket_server or socket_client_connection instead. |
| 34 | */ |
| 35 | class socket_handler |
| 36 | { |
| 37 | private: |
| 38 | static const unsigned int default_recv_buffer_size=2048; |
| 39 | static const unsigned int default_write_block_size=4096; |
| 40 | static const long long default_write_timeout=30000000; |
| 41 | |
| 42 | socket_type_value socket_type; |
| 43 | |
| 44 | bool data_waiting(long long usec_timeout,long long *usec_timeout_remaining=NULL); |
| 45 | void wait_ready_to_write(int socket, long long write_block_timeout); |
| 46 | |
| 47 | protected: |
| 48 | int sock; |
| 49 | unsigned int recv_buffer_size; |
| 50 | unsigned int write_block_size; |
| 51 | long long write_timeout; |
| 52 | |
| 53 | socket_handler(int _sock, socket_type_value _socket_type); |
| 54 | ~socket_handler(); |
| 55 | |
| 56 | void set_socket_options(int sock); |
| 57 | |
| 58 | virtual std::ostream* get_logstream(log_level_values level) |
| 59 | { return NULL; } |
| 60 | |
| 61 | void socket_write(const std::string& data); |
| 62 | |
| 63 | virtual void close(); |
| 64 | |
| 65 | bool fill_buffer(std::string& buffer, long long usec_timeout, long long* usec_timeout_remaining=NULL); |
| 66 | bool fill_buffer(std::string& buffer); |
| 67 | |
| 68 | public: |
| 69 | /// is this a tcp or unix socket connection |
| 70 | socket_type_value get_type() |
| 71 | { return socket_type; } |
| 72 | |
| 73 | int get_socket() |
| 74 | { return sock; } |
| 75 | |
| 76 | bool is_closed(); |
| 77 | |
| 78 | void set_recv_buffer_size(unsigned int new_recv_buffer_size); |
| 79 | void set_write_block_size(unsigned int new_write_block_size); |
| 80 | void set_write_timeout(long long new_write_timeout); |
| 81 | |
| 82 | unsigned int get_recv_buffer_size() const { return recv_buffer_size; } |
| 83 | unsigned int get_write_block_size() const { return write_block_size; } |
| 84 | long long get_write_timeout() const { return write_timeout; } |
| 85 | }; |
| 86 | |
| 87 | } |
| 88 | |
| 89 | #endif |