~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to vio/viosocket.c

Removed time.h and sys/time.h from global.h.

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