~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/drizzle_protocol/viosocket.cc

  • Committer: Lee Bieber
  • Date: 2010-01-30 23:42:02 UTC
  • mto: This revision was merged to the branch mainline in revision 1282.
  • Revision ID: lbieber@lee-biebers-macbook-pro.local-20100130234202-sxmqfteqwiq15ptg
add target to japanese tests

Show diffs side-by-side

added added

removed removed

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