19 #include <ndb_global.h>
21 #include <SocketClient.hpp>
22 #include <SocketAuthenticator.hpp>
24 SocketClient::SocketClient(
const char *server_name,
unsigned short port,
SocketAuthenticator *sa) :
25 m_connect_timeout_millisec(0)
29 m_server_name= server_name ? strdup(server_name) : 0;
30 my_socket_invalidate(&m_sockfd);
33 SocketClient::~SocketClient()
37 if (my_socket_valid(m_sockfd))
38 NDB_CLOSE_SOCKET(m_sockfd);
46 if (my_socket_valid(m_sockfd))
47 NDB_CLOSE_SOCKET(m_sockfd);
51 memset(&m_servaddr, 0,
sizeof(m_servaddr));
52 m_servaddr.sin_family = AF_INET;
53 m_servaddr.sin_port = htons(m_port);
55 if (Ndb_getInAddr(&m_servaddr.sin_addr, m_server_name))
59 m_sockfd= my_socket_create(AF_INET, SOCK_STREAM, 0);
60 if (!my_socket_valid(m_sockfd)) {
64 DBUG_PRINT(
"info",(
"NDB_SOCKET: " MY_SOCKET_FORMAT, MY_SOCKET_FORMAT_VALUE(m_sockfd)));
70 SocketClient::bind(
const char* bindaddress,
unsigned short localport)
72 if (!my_socket_valid(m_sockfd))
75 struct sockaddr_in local;
76 memset(&local, 0,
sizeof(local));
77 local.sin_family = AF_INET;
78 local.sin_port = htons(localport);
80 if (Ndb_getInAddr(&local.sin_addr, bindaddress))
82 return errno ? errno : EINVAL;
85 if (my_socket_reuseaddr(m_sockfd,
true) == -1)
87 int ret = my_socket_errno();
88 my_socket_close(m_sockfd);
89 my_socket_invalidate(&m_sockfd);
93 if (my_bind_inet(m_sockfd, &local) == -1)
95 int ret = my_socket_errno();
96 my_socket_close(m_sockfd);
97 my_socket_invalidate(&m_sockfd);
105 #define NONBLOCKERR(E) (E!=SOCKET_EAGAIN && E!=SOCKET_EWOULDBLOCK)
107 #define NONBLOCKERR(E) (E!=EINPROGRESS)
111 SocketClient::connect(
const char *toaddress,
unsigned short toport)
113 if (!my_socket_valid(m_sockfd))
125 m_server_name = strdup(toaddress);
127 memset(&m_servaddr, 0,
sizeof(m_servaddr));
128 m_servaddr.sin_family = AF_INET;
129 m_servaddr.sin_port = htons(toport);
131 if (Ndb_getInAddr(&m_servaddr.sin_addr, m_server_name))
133 my_socket_close(m_sockfd);
134 my_socket_invalidate(&m_sockfd);
140 if (my_socket_nonblock(m_sockfd,
true) < 0)
142 my_socket_close(m_sockfd);
143 my_socket_invalidate(&m_sockfd);
148 int r = my_connect_inet(m_sockfd, &m_servaddr);
152 if (r < 0 && NONBLOCKERR(my_socket_errno())) {
154 my_socket_close(m_sockfd);
155 my_socket_invalidate(&m_sockfd);
159 if (ndb_poll(m_sockfd,
true,
true,
true,
160 m_connect_timeout_millisec > 0 ?
161 m_connect_timeout_millisec : -1) <= 0)
165 my_socket_close(m_sockfd);
166 my_socket_invalidate(&m_sockfd);
175 SOCKET_SIZE_TYPE len=
sizeof(so_error);
176 if (my_getsockopt(m_sockfd, SOL_SOCKET, SO_ERROR, &so_error, &len) < 0)
178 my_socket_close(m_sockfd);
179 my_socket_invalidate(&m_sockfd);
185 my_socket_close(m_sockfd);
186 my_socket_invalidate(&m_sockfd);
192 if (my_socket_nonblock(m_sockfd,
true) < 0)
194 my_socket_close(m_sockfd);
195 my_socket_invalidate(&m_sockfd);
200 if (!m_auth->client_authenticate(m_sockfd))
202 my_socket_close(m_sockfd);
203 my_socket_invalidate(&m_sockfd);
207 NDB_SOCKET_TYPE sockfd = m_sockfd;
209 my_socket_invalidate(&m_sockfd);