19
19
we are working on. In this case we should just return read errors from
20
20
the file descriptior.
22
24
#include "config.h"
24
26
#include <string.h>
25
#include <drizzled/util/test.h>
26
#include <sys/socket.h>
28
#include <sys/types.h>
29
#include <netinet/tcp.h>
30
#include <netinet/in.h>
43
namespace drizzle_plugin
56
We call fcntl() to set the flags and then immediately read them back
57
to make sure that we and the system are in agreement on the state of
60
An example of why we need to do this is FreeBSD (and apparently some
61
other BSD-derived systems, like Mac OS X), where the system sometimes
62
reports that the socket is set for non-blocking when it really will
65
fcntl(sd, F_SETFL, 0);
66
fcntl_mode= fcntl(sd, F_GETFL);
81
if (shutdown(sd, SHUT_RDWR))
92
size_t Vio::read(unsigned char* buf, size_t size)
96
/* Ensure nobody uses vio_read_buff and vio_read simultaneously */
97
assert(read_end == read_pos);
98
r= ::read(sd, buf, size);
103
size_t Vio::write(const unsigned char* buf, size_t size)
107
r = ::write(sd, buf, size);
112
int Vio::blocking(bool set_blocking_mode, bool *old_mode)
116
// make sure ptr is not NULL:
117
if (NULL != old_mode)
118
*old_mode= drizzled::test(!(fcntl_mode & O_NONBLOCK));
122
int old_fcntl=fcntl_mode;
123
if (set_blocking_mode)
124
fcntl_mode &= ~O_NONBLOCK; /* clear bit */
126
fcntl_mode |= O_NONBLOCK; /* set bit */
127
if (old_fcntl != fcntl_mode)
129
r= fcntl(sd, F_SETFL, fcntl_mode);
132
fcntl_mode= old_fcntl;
145
error= setsockopt(sd, IPPROTO_TCP, TCP_NODELAY,
146
&nodelay, sizeof(nodelay));
149
perror("setsockopt");
155
int32_t Vio::keepalive(bool set_keep_alive)
163
r= setsockopt(sd, SOL_SOCKET, SO_KEEPALIVE, (char *) &opt, sizeof(opt));
166
perror("setsockopt");
173
bool Vio::should_retry() const
176
return (en == EAGAIN || en == EINTR ||
180
bool Vio::was_interrupted() const
183
return (en == EAGAIN || en == EINTR ||
184
en == EWOULDBLOCK || en == ETIMEDOUT);
187
bool Vio::peer_addr(char *buf, uint16_t *port, size_t buflen) const
190
char port_buf[NI_MAXSERV];
191
socklen_t al = sizeof(remote);
193
if (getpeername(sd, (struct sockaddr *) (&remote),
199
if ((error= getnameinfo((struct sockaddr *)(&remote),
202
port_buf, NI_MAXSERV, NI_NUMERICHOST|NI_NUMERICSERV)))
207
*port= (uint16_t)strtol(port_buf, (char **)NULL, 10);
212
void Vio::timeout(bool is_sndtimeo, int32_t t)
216
/* POSIX specifies time as struct timeval. */
217
struct timeval wait_timeout;
218
wait_timeout.tv_sec= t;
219
wait_timeout.tv_usec= 0;
221
assert(t >= 0 && t <= INT32_MAX);
223
error= setsockopt(sd, SOL_SOCKET, is_sndtimeo ? SO_SNDTIMEO : SO_RCVTIMEO,
225
(socklen_t)sizeof(struct timeval));
226
if (error == -1 && errno != ENOPROTOOPT)
228
perror("setsockopt");
233
int Vio::get_errno() const
238
int Vio::get_fd() const
244
char *Vio::get_read_pos() const
249
char *Vio::get_read_end() const
254
} /* namespace drizzle_plugin */
34
* Helper to fill most of the Vio* with defaults.
37
static void drizzleclient_vio_init(Vio* vio, enum enum_vio_type type,
38
int sd, uint32_t flags)
40
memset(vio, 0, sizeof(*vio));
43
if ((flags & VIO_BUFFERED_READ) &&
44
!(vio->read_buffer= (char*)malloc(VIO_READ_BUFFER_SIZE)))
45
flags&= ~VIO_BUFFERED_READ;
47
vio->viodelete =drizzleclient_vio_delete;
48
vio->vioerrno =drizzleclient_vio_errno;
49
vio->read= (flags & VIO_BUFFERED_READ) ? drizzleclient_vio_read_buff : drizzleclient_vio_read;
50
vio->write =drizzleclient_vio_write;
51
vio->fastsend =drizzleclient_vio_fastsend;
52
vio->viokeepalive =drizzleclient_vio_keepalive;
53
vio->should_retry =drizzleclient_vio_should_retry;
54
vio->was_interrupted=drizzleclient_vio_was_interrupted;
55
vio->vioclose =drizzleclient_vio_close;
56
vio->peer_addr =drizzleclient_vio_peer_addr;
57
vio->vioblocking =drizzleclient_vio_blocking;
58
vio->is_blocking =drizzleclient_vio_is_blocking;
59
vio->timeout =drizzleclient_vio_timeout;
64
/* Reset initialized VIO to use with another transport type */
66
void drizzleclient_vio_reset(Vio* vio, enum enum_vio_type type,
67
int sd, uint32_t flags)
69
free(vio->read_buffer);
70
drizzleclient_vio_init(vio, type, sd, flags);
74
/* Open the socket or TCP/IP connection and read the fnctl() status */
76
Vio *drizzleclient_vio_new(int sd, enum enum_vio_type type, uint32_t flags)
78
Vio *vio = (Vio*) malloc(sizeof(Vio));
82
drizzleclient_vio_init(vio, type, sd, flags);
83
sprintf(vio->desc, "TCP/IP (%d)", vio->sd);
85
We call fcntl() to set the flags and then immediately read them back
86
to make sure that we and the system are in agreement on the state of
89
An example of why we need to do this is FreeBSD (and apparently some
90
other BSD-derived systems, like Mac OS X), where the system sometimes
91
reports that the socket is set for non-blocking when it really will
94
fcntl(sd, F_SETFL, 0);
95
vio->fcntl_mode= fcntl(sd, F_GETFL);
101
void drizzleclient_vio_delete(Vio* vio)
104
return; /* It must be safe to delete null pointers. */
106
if (vio->type != VIO_CLOSED)
108
free((unsigned char*) vio->read_buffer);
109
free((unsigned char*) vio);
114
Cleanup memory allocated by vio or the
115
components below it when application finish
118
void drizzleclient_vio_end(void)