18 #include <ndb_global.h>
20 #include <portlib/NdbTCP.h>
21 #include <portlib/NdbTick.h>
22 #include <util/socket_io.h>
23 #include <util/BaseString.hpp>
28 int timeout_millis,
int* total_elapsed_millis)
30 const NDB_TICKS start = NdbTick_CurrentMillisecond();
32 timeout_millis -= *total_elapsed_millis;
34 if (timeout_millis <= 0)
38 ndb_poll(socket, read, write,
false, timeout_millis);
41 const int elapsed_millis = (int)(NdbTick_CurrentMillisecond() - start);
42 assert(elapsed_millis >= 0);
45 *total_elapsed_millis += elapsed_millis;
53 read_socket(NDB_SOCKET_TYPE socket,
int timeout_millis,
54 char *
buf,
int buflen){
58 int elapsed_millis = 0;
59 const int res = poll_socket(socket,
true,
false,
60 timeout_millis, &elapsed_millis);
64 return (
int)my_recv(socket, &buf[0], buflen, 0);
69 readln_socket(NDB_SOCKET_TYPE socket,
int timeout_millis,
int *time,
70 char * buf,
int buflen, NdbMutex *mutex){
75 NdbMutex_Unlock(mutex);
77 const int res = poll_socket(socket,
true,
false,
78 timeout_millis, time);
91 while((t = (
int)my_recv(socket, ptr, len, MSG_PEEK)) == -1
92 && socket_errno == EINTR);
100 for(
int i = 0;
i<t;
i++)
107 for (len = 1 +
i; len; )
109 while ((t = (
int)my_recv(socket, ptr, len, 0)) == -1
110 && socket_errno == EINTR);
116 if (t > 1 && ptr[-2] ==
'\r')
130 for (
int tmp = t; tmp; )
132 while ((t = (
int)my_recv(socket, ptr, tmp, 0)) == -1 && socket_errno == EINTR);
140 if (t > 0 && buf[t-1] ==
'\r')
147 if (poll_socket(socket,
true,
false, timeout_millis, time) != 1)
161 write_socket(NDB_SOCKET_TYPE socket,
int timeout_millis,
int *time,
162 const char buf[],
int len){
164 if (poll_socket(socket,
false,
true, timeout_millis, time) != 1)
167 const char * tmp = &buf[0];
169 const int w = (int)my_send(socket, tmp, len, 0);
179 if (poll_socket(socket,
false,
true, timeout_millis, time) != 1)
188 print_socket(NDB_SOCKET_TYPE socket,
int timeout_millis,
int *time,
189 const char *
fmt, ...){
192 int ret = vprint_socket(socket, timeout_millis, time, fmt, ap);
200 println_socket(NDB_SOCKET_TYPE socket,
int timeout_millis,
int *time,
201 const char *
fmt, ...){
204 int ret = vprintln_socket(socket, timeout_millis, time, fmt, ap);
211 vprint_socket(NDB_SOCKET_TYPE socket,
int timeout_millis,
int *time,
212 const char *
fmt, va_list ap){
217 if (fmt != 0 && fmt[0] != 0) {
218 size = BaseString::vsnprintf(buf,
sizeof(buf), fmt, ap);
220 if(size >
sizeof(buf)) {
221 buf2 = (
char *)malloc(size);
224 BaseString::vsnprintf(buf2, size, fmt, ap);
229 int ret = write_socket(socket, timeout_millis, time, buf2, size);
237 vprintln_socket(NDB_SOCKET_TYPE socket,
int timeout_millis,
int *time,
238 const char * fmt, va_list ap){
243 if (fmt != 0 && fmt[0] != 0) {
244 size = BaseString::vsnprintf(buf,
sizeof(buf), fmt, ap)+1;
246 if(size >
sizeof(buf)) {
247 buf2 = (
char *)malloc(size);
250 BaseString::vsnprintf(buf2, size, fmt, ap);
257 int ret = write_socket(socket, timeout_millis, time, buf2, size);
269 ~INIT_WINSOCK2(
void);
275 INIT_WINSOCK2 g_init_winsock2;
277 INIT_WINSOCK2::INIT_WINSOCK2(
void)
278 : m_bAcceptable(false)
280 WORD wVersionRequested;
284 wVersionRequested = MAKEWORD( 2, 2 );
286 err = WSAStartup( wVersionRequested, &wsaData );
290 m_bAcceptable =
false;
299 if ( LOBYTE( wsaData.wVersion ) != 2 ||
300 HIBYTE( wsaData.wVersion ) != 2 ) {
304 m_bAcceptable =
false;
308 m_bAcceptable =
true;
311 INIT_WINSOCK2::~INIT_WINSOCK2(
void)
315 m_bAcceptable =
false;