~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to vio/viosocket.c

  • Committer: Monty Taylor
  • Date: 2008-08-04 19:37:18 UTC
  • mto: (261.2.2 codestyle)
  • mto: This revision was merged to the branch mainline in revision 262.
  • Revision ID: monty@inaugust.com-20080804193718-f0rz13uli4429ozb
Changed gettext_noop() to N_()

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
  the file descriptior.
21
21
*/
22
22
 
23
 
#define DONT_MAP_VIO
24
 
#include <drizzled/global.h>
25
 
#include <libdrizzle/vio.h>
26
 
#include <sys/socket.h>
27
 
#include <drizzled/util/test.h>
28
 
#include <string.h>
29
 
 
30
 
#include <netinet/tcp.h>
31
 
#include <sys/poll.h>
32
 
 
33
 
#include <netdb.h>
 
23
#include "vio_priv.h"
34
24
 
35
25
int vio_errno(Vio *vio __attribute__((unused)))
36
26
{
37
 
  return errno;
 
27
  return socket_errno;          /* On Win32 this mapped to WSAGetLastError() */
38
28
}
39
29
 
40
30
 
41
 
size_t vio_read(Vio * vio, unsigned char* buf, size_t size)
 
31
size_t vio_read(Vio * vio, uchar* buf, size_t size)
42
32
{
43
33
  size_t r;
44
34
 
55
45
  reduce number of syscalls.
56
46
*/
57
47
 
58
 
size_t vio_read_buff(Vio *vio, unsigned char* buf, size_t size)
 
48
size_t vio_read_buff(Vio *vio, uchar* buf, size_t size)
59
49
{
60
50
  size_t rc;
61
51
#define VIO_UNBUFFERED_READ_MIN_SIZE 2048
62
52
 
63
53
  if (vio->read_pos < vio->read_end)
64
54
  {
65
 
    rc= cmin((size_t) (vio->read_end - vio->read_pos), size);
 
55
    rc= min((size_t) (vio->read_end - vio->read_pos), size);
66
56
    memcpy(buf, vio->read_pos, rc);
67
57
    vio->read_pos+= rc;
68
58
    /*
73
63
  }
74
64
  else if (size < VIO_UNBUFFERED_READ_MIN_SIZE)
75
65
  {
76
 
    rc= vio_read(vio, (unsigned char*) vio->read_buffer, VIO_READ_BUFFER_SIZE);
 
66
    rc= vio_read(vio, (uchar*) vio->read_buffer, VIO_READ_BUFFER_SIZE);
77
67
    if (rc != 0 && rc != (size_t) -1)
78
68
    {
79
69
      if (rc > size)
93
83
}
94
84
 
95
85
 
96
 
size_t vio_write(Vio * vio, const unsigned char* buf, size_t size)
 
86
size_t vio_write(Vio * vio, const uchar* buf, size_t size)
97
87
{
98
88
  size_t r;
99
89
 
141
131
int vio_fastsend(Vio * vio __attribute__((unused)))
142
132
{
143
133
  int nodelay = 1;
144
 
  int error;
145
 
 
146
 
  error= setsockopt(vio->sd, IPPROTO_TCP, TCP_NODELAY,
147
 
                    &nodelay, sizeof(nodelay));
148
 
  if (error != 0)
149
 
  {
150
 
    perror("setsockopt");
151
 
    assert(error == 0);
152
 
  }
153
 
 
154
 
  return error;
 
134
  int r;
 
135
 
 
136
  r= setsockopt(vio->sd, IPPROTO_TCP, TCP_NODELAY,
 
137
                &nodelay, sizeof(nodelay));
 
138
  assert(r == 0);
 
139
 
 
140
  return r;
155
141
}
156
142
 
157
143
int32_t vio_keepalive(Vio* vio, bool set_keep_alive)
163
149
    opt= 1;
164
150
 
165
151
  r= setsockopt(vio->sd, SOL_SOCKET, SO_KEEPALIVE, (char *) &opt, sizeof(opt));
166
 
  if (r != 0)
167
 
  {
168
 
    perror("setsockopt");
169
 
    assert(r == 0);
170
 
  }
171
152
 
172
153
  return r;
173
154
}
176
157
bool
177
158
vio_should_retry(Vio * vio __attribute__((unused)))
178
159
{
179
 
  int en = errno;
180
 
  return (en == EAGAIN || en == EINTR ||
181
 
          en == EWOULDBLOCK);
 
160
  int en = socket_errno;
 
161
  return (en == SOCKET_EAGAIN || en == SOCKET_EINTR ||
 
162
          en == SOCKET_EWOULDBLOCK);
182
163
}
183
164
 
184
165
 
185
166
bool
186
167
vio_was_interrupted(Vio *vio __attribute__((unused)))
187
168
{
188
 
  int en= errno;
189
 
  return (en == EAGAIN || en == EINTR ||
190
 
          en == EWOULDBLOCK || en == ETIMEDOUT);
 
169
  int en= socket_errno;
 
170
  return (en == SOCKET_EAGAIN || en == SOCKET_EINTR ||
 
171
          en == SOCKET_EWOULDBLOCK || en == SOCKET_ETIMEDOUT);
191
172
}
192
173
 
193
174
 
199
180
    assert(vio->sd >= 0);
200
181
    if (shutdown(vio->sd, SHUT_RDWR))
201
182
      r= -1;
202
 
    if (close(vio->sd))
 
183
    if (closesocket(vio->sd))
203
184
      r= -1;
204
185
  }
205
186
  vio->type= VIO_CLOSED;
219
200
  return vio->type;
220
201
}
221
202
 
222
 
int vio_fd(Vio* vio)
 
203
my_socket vio_fd(Vio* vio)
223
204
{
224
205
  return vio->sd;
225
206
}
253
234
 
254
235
/* Return 0 if there is data to be read */
255
236
 
256
 
bool vio_poll_read(Vio *vio, int32_t timeout)
 
237
bool vio_poll_read(Vio *vio,uint timeout)
257
238
{
258
239
  struct pollfd fds;
259
240
  int res;
280
261
  return false;
281
262
}
282
263
 
283
 
void vio_timeout(Vio *vio, bool is_sndtimeo, int32_t timeout)
 
264
void vio_timeout(Vio *vio, uint which, uint timeout)
284
265
{
285
 
  int error;
 
266
  int r;
286
267
 
287
268
  /* POSIX specifies time as struct timeval. */
288
269
  struct timeval wait_timeout;
289
270
  wait_timeout.tv_sec= timeout;
290
271
  wait_timeout.tv_usec= 0;
291
272
 
292
 
  assert(timeout >= 0 && timeout <= INT32_MAX);
293
 
  assert(vio->sd != -1);
294
 
  error= setsockopt(vio->sd, SOL_SOCKET, is_sndtimeo ? SO_SNDTIMEO : SO_RCVTIMEO,
295
 
                    &wait_timeout,
296
 
                    (socklen_t)sizeof(struct timeval));
297
 
  if (error != 0)
298
 
  {
299
 
    perror("setsockopt");
300
 
    assert(error == 0);
301
 
  }
 
273
  r= setsockopt(vio->sd, SOL_SOCKET, which ? SO_SNDTIMEO : SO_RCVTIMEO,
 
274
                IF_WIN(const char*, const void*)&wait_timeout,
 
275
                sizeof(wait_timeout));
 
276
  assert(r == 0);
302
277
}