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_CLIENT |
23 | #define __LIBT2N_SOCKET_CLIENT | |
24 | ||
25 | #include "client.hxx" | |
26 | #include "socket_handler.hxx" | |
27 | ||
b604df5f GE |
28 | struct sockaddr; |
29 | ||
a11e19b7 GE |
30 | namespace libt2n |
31 | { | |
94247295 | 32 | /** @brief a connection from client to server using sockets. |
a11e19b7 | 33 | |
94247295 GE |
34 | Use this class to connect from a client to a server. |
35 | */ | |
644c4d26 | 36 | class socket_client_connection : public client_connection, public socket_handler |
a11e19b7 | 37 | { |
cc68aabb GE |
38 | public: |
39 | static const int max_retries_default=3; | |
b604df5f | 40 | static const long long connect_timeout_usec_default=30000000; |
644c4d26 | 41 | |
a11e19b7 GE |
42 | private: |
43 | void real_write(const std::string& data) | |
644c4d26 GE |
44 | { socket_write(data); } |
45 | ||
b604df5f GE |
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); | |
644c4d26 GE |
49 | |
50 | int max_retries; | |
b604df5f | 51 | long long connect_timeout_usec; |
644c4d26 GE |
52 | |
53 | std::string path; | |
54 | std::string server; | |
55 | int port; | |
a11e19b7 | 56 | |
fb3345ad GE |
57 | std::string lastErrorMsg; |
58 | ||
e1614a6d GE |
59 | protected: |
60 | ||
a7170401 GE |
61 | std::ostream* get_logstream(log_level_values level) |
62 | { return client_connection::get_logstream(level); } | |
63 | ||
a11e19b7 | 64 | public: |
b604df5f GE |
65 | socket_client_connection(int _port, const std::string& _server="127.0.0.1", |
66 | long long _connect_timeout_usec=connect_timeout_usec_default, | |
e1614a6d GE |
67 | int _max_retries=max_retries_default, |
68 | std::ostream *_logstream=NULL, log_level_values _log_level=none); | |
b604df5f GE |
69 | socket_client_connection(const std::string& _path, |
70 | long long _connect_timeout_usec=connect_timeout_usec_default, | |
e1614a6d GE |
71 | int _max_retries=max_retries_default, |
72 | std::ostream *_logstream=NULL, log_level_values _log_level=none); | |
a11e19b7 | 73 | |
ced847b2 TJ |
74 | ~socket_client_connection(); |
75 | ||
94247295 GE |
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. | |
45a2ebc9 GE |
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 | |
94247295 GE |
82 | @retval true if new data was found (does not mean that the received data |
83 | is a complete packet though) | |
84 | */ | |
45a2ebc9 GE |
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); } | |
a11e19b7 | 87 | |
56f3994d | 88 | virtual void close(); |
af84dfb5 GE |
89 | |
90 | void reconnect(); | |
fb3345ad GE |
91 | |
92 | std::string get_last_error_msg(void) | |
93 | { return lastErrorMsg; } | |
a11e19b7 GE |
94 | }; |
95 | ||
96 | } | |
97 | ||
98 | #endif |