~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to vio/viosocket.c

  • Committer: Brian Aker
  • Date: 2008-10-06 06:47:29 UTC
  • Revision ID: brian@tangent.org-20081006064729-2i9mhjkzyvow9xsm
RemoveĀ uint.

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 "config.h"
25
 
#include "vio.h"
26
 
#include <drizzled/util/test.h>
27
 
 
 
23
#include "vio_priv.h"
28
24
#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
 
namespace drizzle_protocol
48
 
{
49
 
 
50
 
 
51
 
int drizzleclient_vio_errno(Vio *vio)
52
 
{
53
 
  (void)vio;
 
25
 
 
26
int vio_errno(Vio *vio __attribute__((unused)))
 
27
{
54
28
  return errno;
55
29
}
56
30
 
57
31
 
58
 
size_t drizzleclient_vio_read(Vio * vio, unsigned char* buf, size_t size)
 
32
size_t vio_read(Vio * vio, unsigned char* buf, size_t size)
59
33
{
60
34
  size_t r;
61
35
 
62
 
  /* Ensure nobody uses drizzleclient_vio_read_buff and drizzleclient_vio_read simultaneously */
 
36
  /* Ensure nobody uses vio_read_buff and vio_read simultaneously */
63
37
  assert(vio->read_end == vio->read_pos);
64
38
  r= read(vio->sd, buf, size);
65
39
 
72
46
  reduce number of syscalls.
73
47
*/
74
48
 
75
 
size_t drizzleclient_vio_read_buff(Vio *vio, unsigned char* buf, size_t size)
 
49
size_t vio_read_buff(Vio *vio, unsigned char* buf, size_t size)
76
50
{
77
51
  size_t rc;
78
52
#define VIO_UNBUFFERED_READ_MIN_SIZE 2048
79
53
 
80
54
  if (vio->read_pos < vio->read_end)
81
55
  {
82
 
    rc= min((size_t) (vio->read_end - vio->read_pos), size);
 
56
    rc= cmin((size_t) (vio->read_end - vio->read_pos), size);
83
57
    memcpy(buf, vio->read_pos, rc);
84
58
    vio->read_pos+= rc;
85
59
    /*
86
60
      Do not try to read from the socket now even if rc < size:
87
 
      drizzleclient_vio_read can return -1 due to an error or non-blocking mode, and
 
61
      vio_read can return -1 due to an error or non-blocking mode, and
88
62
      the safest way to handle it is to move to a separate branch.
89
63
    */
90
64
  }
91
65
  else if (size < VIO_UNBUFFERED_READ_MIN_SIZE)
92
66
  {
93
 
    rc= drizzleclient_vio_read(vio, (unsigned char*) vio->read_buffer, VIO_READ_BUFFER_SIZE);
 
67
    rc= vio_read(vio, (unsigned char*) vio->read_buffer, VIO_READ_BUFFER_SIZE);
94
68
    if (rc != 0 && rc != (size_t) -1)
95
69
    {
96
70
      if (rc > size)
103
77
    }
104
78
  }
105
79
  else
106
 
    rc= drizzleclient_vio_read(vio, buf, size);
 
80
    rc= vio_read(vio, buf, size);
107
81
 
108
82
  return rc;
109
83
#undef VIO_UNBUFFERED_READ_MIN_SIZE
110
84
}
111
85
 
112
86
 
113
 
size_t drizzleclient_vio_write(Vio * vio, const unsigned char* buf, size_t size)
 
87
size_t vio_write(Vio * vio, const unsigned char* buf, size_t size)
114
88
{
115
89
  size_t r;
116
90
 
119
93
  return r;
120
94
}
121
95
 
122
 
int drizzleclient_vio_blocking(Vio * vio, bool set_blocking_mode, bool *old_mode)
 
96
int vio_blocking(Vio * vio, bool set_blocking_mode, bool *old_mode)
123
97
{
124
98
  int r=0;
125
99
 
126
 
  *old_mode= drizzled::test(!(vio->fcntl_mode & O_NONBLOCK));
 
100
  *old_mode= test(!(vio->fcntl_mode & O_NONBLOCK));
127
101
 
128
102
  if (vio->sd >= 0)
129
103
  {
146
120
}
147
121
 
148
122
bool
149
 
drizzleclient_vio_is_blocking(Vio * vio)
 
123
vio_is_blocking(Vio * vio)
150
124
{
151
125
  bool r;
152
126
  r = !(vio->fcntl_mode & O_NONBLOCK);
155
129
}
156
130
 
157
131
 
158
 
int drizzleclient_vio_fastsend(Vio * vio)
 
132
int vio_fastsend(Vio * vio __attribute__((unused)))
159
133
{
160
 
  (void)vio;
161
134
  int nodelay = 1;
162
135
  int error;
163
136
 
172
145
  return error;
173
146
}
174
147
 
175
 
int32_t drizzleclient_vio_keepalive(Vio* vio, bool set_keep_alive)
 
148
int32_t vio_keepalive(Vio* vio, bool set_keep_alive)
176
149
{
177
150
  int r= 0;
178
151
  uint32_t opt= 0;
192
165
 
193
166
 
194
167
bool
195
 
drizzleclient_vio_should_retry(Vio * vio)
 
168
vio_should_retry(Vio * vio __attribute__((unused)))
196
169
{
197
 
  (void)vio;
198
170
  int en = errno;
199
171
  return (en == EAGAIN || en == EINTR ||
200
172
          en == EWOULDBLOCK);
202
174
 
203
175
 
204
176
bool
205
 
drizzleclient_vio_was_interrupted(Vio *vio)
 
177
vio_was_interrupted(Vio *vio __attribute__((unused)))
206
178
{
207
 
  (void)vio;
208
179
  int en= errno;
209
180
  return (en == EAGAIN || en == EINTR ||
210
181
          en == EWOULDBLOCK || en == ETIMEDOUT);
211
182
}
212
183
 
213
184
 
214
 
int drizzleclient_vio_close(Vio * vio)
 
185
int vio_close(Vio * vio)
215
186
{
216
187
  int r=0;
217
188
 if (vio->type != VIO_CLOSED)
229
200
}
230
201
 
231
202
 
232
 
const char *drizzleclient_vio_description(Vio * vio)
 
203
const char *vio_description(Vio * vio)
233
204
{
234
205
  return vio->desc;
235
206
}
236
207
 
237
 
enum enum_vio_type drizzleclient_vio_type(Vio* vio)
 
208
enum enum_vio_type vio_type(Vio* vio)
238
209
{
239
210
  return vio->type;
240
211
}
241
212
 
242
 
int drizzleclient_vio_fd(Vio* vio)
 
213
int vio_fd(Vio* vio)
243
214
{
244
215
  return vio->sd;
245
216
}
246
217
 
247
 
bool drizzleclient_vio_peer_addr(Vio *vio, char *buf, uint16_t *port, size_t buflen)
 
218
bool vio_peer_addr(Vio *vio, char *buf, uint16_t *port, size_t buflen)
248
219
{
249
220
  int error;
250
221
  char port_buf[NI_MAXSERV];
257
228
  }
258
229
  vio->addrLen= (int)addrLen;
259
230
 
260
 
  if ((error= getnameinfo((struct sockaddr *)(&vio->remote),
 
231
  if ((error= getnameinfo((struct sockaddr *)(&vio->remote), 
261
232
                          addrLen,
262
233
                          buf, buflen,
263
234
                          port_buf, NI_MAXSERV, NI_NUMERICHOST|NI_NUMERICSERV)))
273
244
 
274
245
/* Return 0 if there is data to be read */
275
246
 
276
 
bool drizzleclient_vio_poll_read(Vio *vio, int32_t timeout)
 
247
bool vio_poll_read(Vio *vio, int32_t timeout)
277
248
{
278
249
  struct pollfd fds;
279
250
  int res;
289
260
}
290
261
 
291
262
 
292
 
bool drizzleclient_vio_peek_read(Vio *vio, uint32_t *bytes)
 
263
bool vio_peek_read(Vio *vio, uint32_t *bytes)
293
264
{
294
265
  char buf[1024];
295
266
  ssize_t res= recv(vio->sd, &buf, sizeof(buf), MSG_PEEK);
300
271
  return false;
301
272
}
302
273
 
303
 
void drizzleclient_vio_timeout(Vio *vio, bool is_sndtimeo, int32_t timeout)
 
274
void vio_timeout(Vio *vio, bool is_sndtimeo, int32_t timeout)
304
275
{
305
276
  int error;
306
277
 
314
285
  error= setsockopt(vio->sd, SOL_SOCKET, is_sndtimeo ? SO_SNDTIMEO : SO_RCVTIMEO,
315
286
                    &wait_timeout,
316
287
                    (socklen_t)sizeof(struct timeval));
317
 
  if (error == -1 && errno != ENOPROTOOPT)
 
288
  if (error != 0)
318
289
  {
319
290
    perror("setsockopt");
320
291
    assert(error == 0);
321
292
  }
322
293
}
323
 
 
324
 
} /* namespace drizzle_protcol */