~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to vio/viosocket.c

  • Committer: Lee
  • Date: 2008-10-30 22:02:01 UTC
  • mto: (572.1.2 devel)
  • mto: This revision was merged to the branch mainline in revision 573.
  • Revision ID: lbieber@lbieber-desktop-20081030220201-elb6qprbzpn7c5a4
add my name to the AUTHORS file

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