~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/drizzle_protocol/viosocket.cc

  • Committer: Brian Aker
  • Date: 2010-02-07 01:33:54 UTC
  • Revision ID: brian@gaz-20100207013354-d2pg1n68u5c09pgo
Remove giant include header to its own file.

Show diffs side-by-side

added added

removed removed

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