1 /***************************************************************************
2 * Copyright (C) 2006 by Gerd v. Egidy *
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. *
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. *
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 ***************************************************************************/
24 #include <sys/types.h>
25 #include <sys/socket.h>
28 #include <arpa/inet.h>
29 #include <netinet/in.h>
38 #include "socket_client.hxx"
39 #include "t2n_exception.hxx"
46 socket_client_connection::socket_client_connection(int _port, const std::string& _server,
47 long long _connect_timeout_usec, int _max_retries)
48 : client_connection(), socket_handler(0,tcp_s)
50 max_retries=_max_retries;
51 connect_timeout_usec=_connect_timeout_usec;
56 tcp_connect(max_retries);
59 socket_client_connection::socket_client_connection(const std::string& _path,
60 long long _connect_timeout_usec, int _max_retries)
61 : client_connection(), socket_handler(0,unix_s)
63 max_retries=_max_retries;
64 connect_timeout_usec=_connect_timeout_usec;
68 unix_connect(max_retries);
71 void socket_client_connection::tcp_connect(int max_retries)
73 struct sockaddr_in sock_addr;
75 sock_addr.sin_family = AF_INET;
76 sock_addr.sin_port = htons(port);
79 if (inet_aton(server.c_str(),&sock_addr.sin_addr)==0)
81 struct hostent *server_hent;
82 server_hent=gethostbyname(server.c_str());
83 if (server_hent == NULL)
84 throw t2n_connect_error(string("can't find server ")+server);
86 memcpy(&sock_addr.sin_addr,server_hent->h_addr_list[0],sizeof(sock_addr.sin_addr));
89 sock = socket(PF_INET, SOCK_STREAM, 0);
91 throw t2n_connect_error(string("socket() error: ")+strerror(errno));
95 connect_with_timeout((struct sockaddr *) &sock_addr,sizeof(sock_addr));
97 catch (t2n_connect_error &e)
99 // recurse if retries left
101 tcp_connect(max_retries-1);
104 do_callbacks(new_connection);
107 void socket_client_connection::unix_connect(int max_retries)
109 struct sockaddr_un unix_addr;
111 unix_addr.sun_family = AF_UNIX;
112 strcpy (unix_addr.sun_path, path.c_str());
114 sock = socket(PF_UNIX, SOCK_STREAM, 0);
116 throw t2n_connect_error(string("socket() error: ")+strerror(errno));
120 connect_with_timeout((struct sockaddr *) &unix_addr, sizeof(unix_addr));
122 catch (t2n_connect_error &e)
124 // recurse if retries left
126 unix_connect(max_retries-1);
129 do_callbacks(new_connection);
132 void socket_client_connection::connect_with_timeout(struct sockaddr *sock_addr,unsigned int sockaddr_size)
134 set_socket_options(sock);
136 int ret=::connect(sock,sock_addr, sockaddr_size);
140 if (errno==EINPROGRESS)
144 struct timeval *timeout_ptr;
146 if (connect_timeout_usec == -1)
152 // convert timeout from long long usec to int sec + int usec
153 tval.tv_sec = connect_timeout_usec / 1000000;
154 tval.tv_usec = connect_timeout_usec % 1000000;
157 fd_set connect_socket_set;
158 FD_ZERO(&connect_socket_set);
159 FD_SET(sock,&connect_socket_set);
162 while ((ret=select(FD_SETSIZE, NULL, &connect_socket_set, NULL, timeout_ptr)) &&
163 ret < 0 && errno==EINTR);
166 throw t2n_connect_error(string("connect() error (select): ")+strerror(errno));
168 socklen_t sopt=sizeof(int);
170 ret=getsockopt(sock, SOL_SOCKET, SO_ERROR, (void*)(&valopt), &sopt);
171 if (ret < 0 || valopt)
172 throw t2n_connect_error(string("connect() error (getsockopt): ")+strerror(errno));
175 throw t2n_connect_error(string("connect() error: ")+strerror(errno));
179 void socket_client_connection::close()
181 if (!client_connection::is_closed())
183 socket_handler::close();
184 client_connection::close();
188 /** @brief try to reconnect the current connection with the same connection credentials (host and port or path)
190 void socket_client_connection::reconnect()
192 // close the current connection if still open
195 socket_type_value type=get_type();
198 tcp_connect(max_retries);
199 else if (type == unix_s)
200 unix_connect(max_retries);