| 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_CLIENT |
| 23 | #define __LIBT2N_SOCKET_CLIENT |
| 24 | |
| 25 | #include "client.hxx" |
| 26 | #include "socket_handler.hxx" |
| 27 | |
| 28 | struct sockaddr; |
| 29 | |
| 30 | namespace libt2n |
| 31 | { |
| 32 | /** @brief a connection from client to server using sockets. |
| 33 | |
| 34 | Use this class to connect from a client to a server. |
| 35 | */ |
| 36 | class socket_client_connection : public client_connection, public socket_handler |
| 37 | { |
| 38 | public: |
| 39 | static const int max_retries_default=3; |
| 40 | static const long long connect_timeout_usec_default=30000000; |
| 41 | |
| 42 | private: |
| 43 | void real_write(const std::string& data) |
| 44 | { socket_write(data); } |
| 45 | |
| 46 | void tcp_connect(int max_retries); |
| 47 | void unix_connect(int max_retries); |
| 48 | void connect_with_timeout(struct sockaddr *sock_addr,unsigned int sockaddr_size); |
| 49 | |
| 50 | int max_retries; |
| 51 | long long connect_timeout_usec; |
| 52 | |
| 53 | std::string path; |
| 54 | std::string server; |
| 55 | int port; |
| 56 | |
| 57 | std::string lastErrorMsg; |
| 58 | |
| 59 | protected: |
| 60 | |
| 61 | std::ostream* get_logstream(log_level_values level) |
| 62 | { return client_connection::get_logstream(level); } |
| 63 | |
| 64 | public: |
| 65 | socket_client_connection(int _port, const std::string& _server="127.0.0.1", |
| 66 | long long _connect_timeout_usec=connect_timeout_usec_default, |
| 67 | int _max_retries=max_retries_default, |
| 68 | std::ostream *_logstream=NULL, log_level_values _log_level=none); |
| 69 | socket_client_connection(const std::string& _path, |
| 70 | long long _connect_timeout_usec=connect_timeout_usec_default, |
| 71 | int _max_retries=max_retries_default, |
| 72 | std::ostream *_logstream=NULL, log_level_values _log_level=none); |
| 73 | |
| 74 | ~socket_client_connection(); |
| 75 | |
| 76 | /** @brief read data from the socket and copy it into buffer |
| 77 | @param usec_timeout wait until new data is found, max timeout usecs. |
| 78 | -1: wait endless |
| 79 | 0: return instantly |
| 80 | @param usec_timeout_remaining if non-NULL the function will write the |
| 81 | not used time to the given target |
| 82 | @retval true if new data was found (does not mean that the received data |
| 83 | is a complete packet though) |
| 84 | */ |
| 85 | bool fill_buffer(long long usec_timeout=-1, long long *usec_timeout_remaining=NULL) |
| 86 | { return socket_handler::fill_buffer(buffer,usec_timeout,usec_timeout_remaining); } |
| 87 | |
| 88 | virtual void close(); |
| 89 | |
| 90 | void reconnect(); |
| 91 | |
| 92 | std::string get_last_error_msg(void) |
| 93 | { return lastErrorMsg; } |
| 94 | }; |
| 95 | |
| 96 | } |
| 97 | |
| 98 | #endif |