1
/* Copyright (C) 2000 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 */
17
Note that we can't have assertion on file descriptors; The reason for
18
this is that during mysql shutdown, another thread can close a file
19
we are working on. In this case we should just return read errors from
26
#include <drizzled/util/test.h>
28
#include <sys/socket.h>
31
#include <sys/types.h>
32
#include <netinet/tcp.h>
33
#include <netinet/in.h>
46
int drizzleclient_vio_errno(Vio *vio)
53
size_t drizzleclient_vio_read(Vio * vio, unsigned char* buf, size_t size)
57
/* Ensure nobody uses drizzleclient_vio_read_buff and drizzleclient_vio_read simultaneously */
58
assert(vio->read_end == vio->read_pos);
59
r= read(vio->sd, buf, size);
66
Buffered read: if average read size is small it may
67
reduce number of syscalls.
70
size_t drizzleclient_vio_read_buff(Vio *vio, unsigned char* buf, size_t size)
73
#define VIO_UNBUFFERED_READ_MIN_SIZE 2048
75
if (vio->read_pos < vio->read_end)
77
rc= min((size_t) (vio->read_end - vio->read_pos), size);
78
memcpy(buf, vio->read_pos, rc);
81
Do not try to read from the socket now even if rc < size:
82
drizzleclient_vio_read can return -1 due to an error or non-blocking mode, and
83
the safest way to handle it is to move to a separate branch.
86
else if (size < VIO_UNBUFFERED_READ_MIN_SIZE)
88
rc= drizzleclient_vio_read(vio, (unsigned char*) vio->read_buffer, VIO_READ_BUFFER_SIZE);
89
if (rc != 0 && rc != (size_t) -1)
93
vio->read_pos= vio->read_buffer + size;
94
vio->read_end= vio->read_buffer + rc;
97
memcpy(buf, vio->read_buffer, rc);
101
rc= drizzleclient_vio_read(vio, buf, size);
104
#undef VIO_UNBUFFERED_READ_MIN_SIZE
108
size_t drizzleclient_vio_write(Vio * vio, const unsigned char* buf, size_t size)
112
r = write(vio->sd, buf, size);
117
int drizzleclient_vio_blocking(Vio * vio, bool set_blocking_mode, bool *old_mode)
121
*old_mode= drizzled::test(!(vio->fcntl_mode & O_NONBLOCK));
125
int old_fcntl=vio->fcntl_mode;
126
if (set_blocking_mode)
127
vio->fcntl_mode &= ~O_NONBLOCK; /* clear bit */
129
vio->fcntl_mode |= O_NONBLOCK; /* set bit */
130
if (old_fcntl != vio->fcntl_mode)
132
r= fcntl(vio->sd, F_SETFL, vio->fcntl_mode);
135
vio->fcntl_mode= old_fcntl;
144
drizzleclient_vio_is_blocking(Vio * vio)
147
r = !(vio->fcntl_mode & O_NONBLOCK);
153
int drizzleclient_vio_fastsend(Vio * vio)
159
error= setsockopt(vio->sd, IPPROTO_TCP, TCP_NODELAY,
160
&nodelay, sizeof(nodelay));
163
perror("setsockopt");
170
int32_t drizzleclient_vio_keepalive(Vio* vio, bool set_keep_alive)
178
r= setsockopt(vio->sd, SOL_SOCKET, SO_KEEPALIVE, (char *) &opt, sizeof(opt));
181
perror("setsockopt");
190
drizzleclient_vio_should_retry(Vio * vio)
194
return (en == EAGAIN || en == EINTR ||
200
drizzleclient_vio_was_interrupted(Vio *vio)
204
return (en == EAGAIN || en == EINTR ||
205
en == EWOULDBLOCK || en == ETIMEDOUT);
209
int drizzleclient_vio_close(Vio * vio)
212
if (vio->type != VIO_CLOSED)
214
assert(vio->sd >= 0);
215
if (shutdown(vio->sd, SHUT_RDWR))
220
vio->type= VIO_CLOSED;
227
const char *drizzleclient_vio_description(Vio * vio)
232
enum enum_vio_type drizzleclient_vio_type(Vio* vio)
237
int drizzleclient_vio_fd(Vio* vio)
242
bool drizzleclient_vio_peer_addr(Vio *vio, char *buf, uint16_t *port, size_t buflen)
245
char port_buf[NI_MAXSERV];
246
socklen_t addrLen = sizeof(vio->remote);
248
if (getpeername(vio->sd, (struct sockaddr *) (&vio->remote),
253
vio->addrLen= (int)addrLen;
255
if ((error= getnameinfo((struct sockaddr *)(&vio->remote),
258
port_buf, NI_MAXSERV, NI_NUMERICHOST|NI_NUMERICSERV)))
263
*port= (uint16_t)strtol(port_buf, (char **)NULL, 10);
269
/* Return 0 if there is data to be read */
271
bool drizzleclient_vio_poll_read(Vio *vio, int32_t timeout)
279
if ((res=poll(&fds,1,(int) timeout*1000)) <= 0)
281
return res < 0 ? false : true; /* Don't return 1 on errors */
283
return (fds.revents & (POLLIN | POLLERR | POLLHUP) ? false : true);
287
bool drizzleclient_vio_peek_read(Vio *vio, uint32_t *bytes)
290
ssize_t res= recv(vio->sd, &buf, sizeof(buf), MSG_PEEK);
294
*bytes= (uint32_t)res;
298
void drizzleclient_vio_timeout(Vio *vio, bool is_sndtimeo, int32_t timeout)
302
/* POSIX specifies time as struct timeval. */
303
struct timeval wait_timeout;
304
wait_timeout.tv_sec= timeout;
305
wait_timeout.tv_usec= 0;
307
assert(timeout >= 0 && timeout <= INT32_MAX);
308
assert(vio->sd != -1);
309
error= setsockopt(vio->sd, SOL_SOCKET, is_sndtimeo ? SO_SNDTIMEO : SO_RCVTIMEO,
311
(socklen_t)sizeof(struct timeval));
312
if (error == -1 && errno != ENOPROTOOPT)
314
perror("setsockopt");