~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to vio/viosocket.c

  • Committer: Monty Taylor
  • Date: 2008-10-10 23:04:21 UTC
  • mto: (509.1.1 codestyle)
  • mto: This revision was merged to the branch mainline in revision 511.
  • Revision ID: monty@inaugust.com-20081010230421-zohe1eppxievpw8d
RemovedĀ O_NOFOLLOW

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