1
/* Copyright (C) 2003 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
#include <ndb_global.h>
19
#include <socket_io.h>
25
read_socket(NDB_SOCKET_TYPE socket, int timeout_millis,
26
char * buf, int buflen){
32
FD_SET(socket, &readset);
34
struct timeval timeout;
35
timeout.tv_sec = (timeout_millis / 1000);
36
timeout.tv_usec = (timeout_millis % 1000) * 1000;
38
const int selectRes = select(socket + 1, &readset, 0, 0, &timeout);
46
return recv(socket, &buf[0], buflen, 0);
51
readln_socket(NDB_SOCKET_TYPE socket, int timeout_millis, int *time,
52
char * buf, int buflen, NdbMutex *mutex){
58
FD_SET(socket, &readset);
60
struct timeval timeout;
61
timeout.tv_sec = (timeout_millis / 1000);
62
timeout.tv_usec = (timeout_millis % 1000) * 1000;
65
NdbMutex_Unlock(mutex);
66
Uint64 tick= NdbTick_CurrentMillisecond();
67
const int selectRes = select(socket + 1, &readset, 0, 0, &timeout);
69
*time= NdbTick_CurrentMillisecond() - tick;
86
while((t = recv(socket, ptr, len, MSG_PEEK)) == -1 && errno == EINTR);
94
for(int i = 0; i<t; i++)
101
for (len = 1 + i; len; )
103
while ((t = recv(socket, ptr, len, 0)) == -1 && errno == EINTR);
109
if (i > 0 && buf[i-1] == '\r')
119
for (int tmp = t; tmp; )
121
while ((t = recv(socket, ptr, tmp, 0)) == -1 && errno == EINTR);
132
FD_SET(socket, &readset);
133
timeout.tv_sec = ((timeout_millis - *time) / 1000);
134
timeout.tv_usec = ((timeout_millis - *time) % 1000) * 1000;
136
tick= NdbTick_CurrentMillisecond();
137
const int selectRes = select(socket + 1, &readset, 0, 0, &timeout);
138
*time= NdbTick_CurrentMillisecond() - tick;
150
write_socket(NDB_SOCKET_TYPE socket, int timeout_millis, int *time,
151
const char buf[], int len){
154
FD_SET(socket, &writeset);
155
struct timeval timeout;
156
timeout.tv_sec = (timeout_millis / 1000);
157
timeout.tv_usec = (timeout_millis % 1000) * 1000;
160
Uint64 tick= NdbTick_CurrentMillisecond();
161
const int selectRes = select(socket + 1, 0, &writeset, 0, &timeout);
162
*time= NdbTick_CurrentMillisecond() - tick;
168
const char * tmp = &buf[0];
170
const int w = send(socket, tmp, len, 0);
181
FD_SET(socket, &writeset);
182
timeout.tv_sec = ((timeout_millis - *time) / 1000);
183
timeout.tv_usec = ((timeout_millis - *time) % 1000) * 1000;
185
Uint64 tick= NdbTick_CurrentMillisecond();
186
const int selectRes2 = select(socket + 1, 0, &writeset, 0, &timeout);
187
*time= NdbTick_CurrentMillisecond() - tick;
199
print_socket(NDB_SOCKET_TYPE socket, int timeout_millis, int *time,
200
const char * fmt, ...){
203
int ret = vprint_socket(socket, timeout_millis, time, fmt, ap);
211
println_socket(NDB_SOCKET_TYPE socket, int timeout_millis, int *time,
212
const char * fmt, ...){
215
int ret = vprintln_socket(socket, timeout_millis, time, fmt, ap);
222
vprint_socket(NDB_SOCKET_TYPE socket, int timeout_millis, int *time,
223
const char * fmt, va_list ap){
228
if (fmt != 0 && fmt[0] != 0) {
229
size = BaseString::vsnprintf(buf, sizeof(buf), fmt, ap);
230
/* Check if the output was truncated */
231
if(size > sizeof(buf)) {
232
buf2 = (char *)malloc(size);
235
BaseString::vsnprintf(buf2, size, fmt, ap);
240
int ret = write_socket(socket, timeout_millis, time, buf2, size);
248
vprintln_socket(NDB_SOCKET_TYPE socket, int timeout_millis, int *time,
249
const char * fmt, va_list ap){
254
if (fmt != 0 && fmt[0] != 0) {
255
size = BaseString::vsnprintf(buf, sizeof(buf), fmt, ap)+1;// extra byte for '/n'
256
/* Check if the output was truncated */
257
if(size > sizeof(buf)) {
258
buf2 = (char *)malloc(size);
261
BaseString::vsnprintf(buf2, size, fmt, ap);
268
int ret = write_socket(socket, timeout_millis, time, buf2, size);
280
~INIT_WINSOCK2(void);
286
INIT_WINSOCK2 g_init_winsock2;
288
INIT_WINSOCK2::INIT_WINSOCK2(void)
289
: m_bAcceptable(false)
291
WORD wVersionRequested;
295
wVersionRequested = MAKEWORD( 2, 2 );
297
err = WSAStartup( wVersionRequested, &wsaData );
299
/* Tell the user that we could not find a usable */
301
m_bAcceptable = false;
304
/* Confirm that the WinSock DLL supports 2.2.*/
305
/* Note that if the DLL supports versions greater */
306
/* than 2.2 in addition to 2.2, it will still return */
307
/* 2.2 in wVersion since that is the version we */
310
if ( LOBYTE( wsaData.wVersion ) != 2 ||
311
HIBYTE( wsaData.wVersion ) != 2 ) {
312
/* Tell the user that we could not find a usable */
315
m_bAcceptable = false;
318
/* The WinSock DLL is acceptable. Proceed. */
319
m_bAcceptable = true;
322
INIT_WINSOCK2::~INIT_WINSOCK2(void)
326
m_bAcceptable = false;