~drizzle-trunk/drizzle/development

971.7.10 by Eric Day
Duplicated oldlibdrizzle module, one for Drizzle protocol and one for MySQL, per Brian's request from merge proposal. Port options are now --drizzle-protocol-port and --mysql-protocol-port.
1
/* Copyright (C) 2000 MySQL AB
2
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.
6
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.
11
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 */
15
16
/*
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
20
  the file descriptior.
21
*/
22
23
#define DONT_MAP_VIO
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
24
#include "config.h"
971.7.10 by Eric Day
Duplicated oldlibdrizzle module, one for Drizzle protocol and one for MySQL, per Brian's request from merge proposal. Port options are now --drizzle-protocol-port and --mysql-protocol-port.
25
#include "vio.h"
26
#include <drizzled/util/test.h>
27
28
#include <sys/socket.h>
29
#include <string.h>
30
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
31
#include <sys/types.h>
971.7.10 by Eric Day
Duplicated oldlibdrizzle module, one for Drizzle protocol and one for MySQL, per Brian's request from merge proposal. Port options are now --drizzle-protocol-port and --mysql-protocol-port.
32
#include <netinet/tcp.h>
33
#include <netinet/in.h>
34
#include <sys/poll.h>
35
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
36
#include <unistd.h>
37
#include <fcntl.h>
971.7.10 by Eric Day
Duplicated oldlibdrizzle module, one for Drizzle protocol and one for MySQL, per Brian's request from merge proposal. Port options are now --drizzle-protocol-port and --mysql-protocol-port.
38
#include <netdb.h>
39
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
40
#include <cassert>
41
#include <cstdio>
971.7.10 by Eric Day
Duplicated oldlibdrizzle module, one for Drizzle protocol and one for MySQL, per Brian's request from merge proposal. Port options are now --drizzle-protocol-port and --mysql-protocol-port.
42
#include <algorithm>
43
44
using namespace std;
45
46
int drizzleclient_vio_errno(Vio *vio)
47
{
48
  (void)vio;
49
  return errno;
50
}
51
52
53
size_t drizzleclient_vio_read(Vio * vio, unsigned char* buf, size_t size)
54
{
55
  size_t r;
56
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);
60
61
  return r;
62
}
63
64
65
/*
66
  Buffered read: if average read size is small it may
67
  reduce number of syscalls.
68
*/
69
70
size_t drizzleclient_vio_read_buff(Vio *vio, unsigned char* buf, size_t size)
71
{
72
  size_t rc;
73
#define VIO_UNBUFFERED_READ_MIN_SIZE 2048
74
75
  if (vio->read_pos < vio->read_end)
76
  {
77
    rc= min((size_t) (vio->read_end - vio->read_pos), size);
78
    memcpy(buf, vio->read_pos, rc);
79
    vio->read_pos+= rc;
80
    /*
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.
84
    */
85
  }
86
  else if (size < VIO_UNBUFFERED_READ_MIN_SIZE)
87
  {
88
    rc= drizzleclient_vio_read(vio, (unsigned char*) vio->read_buffer, VIO_READ_BUFFER_SIZE);
89
    if (rc != 0 && rc != (size_t) -1)
90
    {
91
      if (rc > size)
92
      {
93
        vio->read_pos= vio->read_buffer + size;
94
        vio->read_end= vio->read_buffer + rc;
95
        rc= size;
96
      }
97
      memcpy(buf, vio->read_buffer, rc);
98
    }
99
  }
100
  else
101
    rc= drizzleclient_vio_read(vio, buf, size);
102
103
  return rc;
104
#undef VIO_UNBUFFERED_READ_MIN_SIZE
105
}
106
107
108
size_t drizzleclient_vio_write(Vio * vio, const unsigned char* buf, size_t size)
109
{
110
  size_t r;
111
112
  r = write(vio->sd, buf, size);
113
114
  return r;
115
}
116
117
int drizzleclient_vio_blocking(Vio * vio, bool set_blocking_mode, bool *old_mode)
118
{
119
  int r=0;
120
121
  *old_mode= test(!(vio->fcntl_mode & O_NONBLOCK));
122
123
  if (vio->sd >= 0)
124
  {
125
    int old_fcntl=vio->fcntl_mode;
126
    if (set_blocking_mode)
127
      vio->fcntl_mode &= ~O_NONBLOCK; /* clear bit */
128
    else
129
      vio->fcntl_mode |= O_NONBLOCK; /* set bit */
130
    if (old_fcntl != vio->fcntl_mode)
131
    {
132
      r= fcntl(vio->sd, F_SETFL, vio->fcntl_mode);
133
      if (r == -1)
134
      {
135
        vio->fcntl_mode= old_fcntl;
136
      }
137
    }
138
  }
139
140
  return r;
141
}
142
143
bool
144
drizzleclient_vio_is_blocking(Vio * vio)
145
{
146
  bool r;
147
  r = !(vio->fcntl_mode & O_NONBLOCK);
148
149
  return r;
150
}
151
152
153
int drizzleclient_vio_fastsend(Vio * vio)
154
{
155
  (void)vio;
156
  int nodelay = 1;
157
  int error;
158
159
  error= setsockopt(vio->sd, IPPROTO_TCP, TCP_NODELAY,
160
                    &nodelay, sizeof(nodelay));
161
  if (error != 0)
162
  {
163
    perror("setsockopt");
164
    assert(error == 0);
165
  }
166
167
  return error;
168
}
169
170
int32_t drizzleclient_vio_keepalive(Vio* vio, bool set_keep_alive)
171
{
172
  int r= 0;
173
  uint32_t opt= 0;
174
175
  if (set_keep_alive)
176
    opt= 1;
177
178
  r= setsockopt(vio->sd, SOL_SOCKET, SO_KEEPALIVE, (char *) &opt, sizeof(opt));
179
  if (r != 0)
180
  {
181
    perror("setsockopt");
182
    assert(r == 0);
183
  }
184
185
  return r;
186
}
187
188
189
bool
190
drizzleclient_vio_should_retry(Vio * vio)
191
{
192
  (void)vio;
193
  int en = errno;
194
  return (en == EAGAIN || en == EINTR ||
195
	  en == EWOULDBLOCK);
196
}
197
198
199
bool
200
drizzleclient_vio_was_interrupted(Vio *vio)
201
{
202
  (void)vio;
203
  int en= errno;
204
  return (en == EAGAIN || en == EINTR ||
205
	  en == EWOULDBLOCK || en == ETIMEDOUT);
206
}
207
208
209
int drizzleclient_vio_close(Vio * vio)
210
{
211
  int r=0;
212
 if (vio->type != VIO_CLOSED)
213
  {
214
    assert(vio->sd >= 0);
215
    if (shutdown(vio->sd, SHUT_RDWR))
216
      r= -1;
217
    if (close(vio->sd))
218
      r= -1;
219
  }
220
  vio->type= VIO_CLOSED;
221
  vio->sd=   -1;
222
223
  return r;
224
}
225
226
227
const char *drizzleclient_vio_description(Vio * vio)
228
{
229
  return vio->desc;
230
}
231
232
enum enum_vio_type drizzleclient_vio_type(Vio* vio)
233
{
234
  return vio->type;
235
}
236
237
int drizzleclient_vio_fd(Vio* vio)
238
{
239
  return vio->sd;
240
}
241
242
bool drizzleclient_vio_peer_addr(Vio *vio, char *buf, uint16_t *port, size_t buflen)
243
{
244
  int error;
245
  char port_buf[NI_MAXSERV];
246
  socklen_t addrLen = sizeof(vio->remote);
247
248
  if (getpeername(vio->sd, (struct sockaddr *) (&vio->remote),
249
                  &addrLen) != 0)
250
  {
251
    return true;
252
  }
253
  vio->addrLen= (int)addrLen;
254
255
  if ((error= getnameinfo((struct sockaddr *)(&vio->remote),
256
                          addrLen,
257
                          buf, buflen,
258
                          port_buf, NI_MAXSERV, NI_NUMERICHOST|NI_NUMERICSERV)))
259
  {
260
    return true;
261
  }
262
263
  *port= (uint16_t)strtol(port_buf, (char **)NULL, 10);
264
265
  return false;
266
}
267
268
269
/* Return 0 if there is data to be read */
270
271
bool drizzleclient_vio_poll_read(Vio *vio, int32_t timeout)
272
{
273
  struct pollfd fds;
274
  int res;
275
276
  fds.fd=vio->sd;
277
  fds.events=POLLIN;
278
  fds.revents=0;
279
  if ((res=poll(&fds,1,(int) timeout*1000)) <= 0)
280
  {
281
    return res < 0 ? false : true;		/* Don't return 1 on errors */
282
  }
283
  return (fds.revents & (POLLIN | POLLERR | POLLHUP) ? false : true);
284
}
285
286
287
bool drizzleclient_vio_peek_read(Vio *vio, uint32_t *bytes)
288
{
289
  char buf[1024];
290
  ssize_t res= recv(vio->sd, &buf, sizeof(buf), MSG_PEEK);
291
292
  if (res < 0)
293
    return true;
294
  *bytes= (uint32_t)res;
295
  return false;
296
}
297
298
void drizzleclient_vio_timeout(Vio *vio, bool is_sndtimeo, int32_t timeout)
299
{
300
  int error;
301
302
  /* POSIX specifies time as struct timeval. */
303
  struct timeval wait_timeout;
304
  wait_timeout.tv_sec= timeout;
305
  wait_timeout.tv_usec= 0;
306
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,
310
                    &wait_timeout,
311
                    (socklen_t)sizeof(struct timeval));
312
  if (error == -1 && errno != ENOPROTOOPT)
313
  {
314
    perror("setsockopt");
315
    assert(error == 0);
316
  }
317
}