~drizzle-trunk/drizzle/development

971.7.10 by Eric Day
Duplicated oldlibdrizzle module, one for Drizzle protocol and one for MySQL, per Brian's request from merge proposal. Port options are now --drizzle-protocol-port and --mysql-protocol-port.
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
1802.10.2 by Monty Taylor
Update all of the copyright headers to include the correct address.
14
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
971.7.10 by Eric Day
Duplicated oldlibdrizzle module, one for Drizzle protocol and one for MySQL, per Brian's request from merge proposal. Port options are now --drizzle-protocol-port and --mysql-protocol-port.
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
*/
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
22
#include "config.h"
971.7.10 by Eric Day
Duplicated oldlibdrizzle module, one for Drizzle protocol and one for MySQL, per Brian's request from merge proposal. Port options are now --drizzle-protocol-port and --mysql-protocol-port.
23
#include "vio.h"
24
#include <string.h>
1337.4.1 by Eric Day
Removed unused functions and extra complexity in MySQL module. Not rewriting any code here (yet?), just reshuffling to make it easier to manage.
25
#include <drizzled/util/test.h>
26
#include <sys/socket.h>
27
#include <string.h>
28
#include <sys/types.h>
29
#include <netinet/tcp.h>
30
#include <netinet/in.h>
31
#include <sys/poll.h>
32
#include <unistd.h>
33
#include <fcntl.h>
34
#include <netdb.h>
35
#include <algorithm>
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
36
#include <cstdlib>
37
#include <cassert>
38
#include <cstdio>
39
#include <fcntl.h>
971.7.10 by Eric Day
Duplicated oldlibdrizzle module, one for Drizzle protocol and one for MySQL, per Brian's request from merge proposal. Port options are now --drizzle-protocol-port and --mysql-protocol-port.
40
1337.4.1 by Eric Day
Removed unused functions and extra complexity in MySQL module. Not rewriting any code here (yet?), just reshuffling to make it easier to manage.
41
using namespace std;
42
1878.6.1 by Thomi Richards
Converted the vio_st struct to the Vio class.
43
Vio::Vio(int nsd)
44
: closed(false),
45
sd(nsd),
46
fcntl_mode(0),
47
read_pos(NULL),
48
read_end(NULL)
49
{
50
  closed= false;
51
  sd= nsd;
52
53
  /*
54
    We call fcntl() to set the flags and then immediately read them back
55
    to make sure that we and the system are in agreement on the state of
56
    things.
57
58
    An example of why we need to do this is FreeBSD (and apparently some
59
    other BSD-derived systems, like Mac OS X), where the system sometimes
60
    reports that the socket is set for non-blocking when it really will
61
    block.
62
  */
63
  fcntl(sd, F_SETFL, 0);
64
  fcntl_mode= fcntl(sd, F_GETFL);
65
66
  memset(&local, 0, sizeof(local));
67
  memset(&remote, 0, sizeof(remote));
68
}
69
70
Vio::~Vio()
71
{
72
 if (!closed)
73
    close();
74
}
75
76
int Vio::close()
77
{
78
  int r=0;
79
  if (!closed)
80
  {
81
    assert(sd >= 0);
82
    if (shutdown(sd, SHUT_RDWR))
83
      r= -1;
84
    if (::close(sd))
85
      r= -1;
86
  }
87
  closed= true;
88
  sd=   -1;
89
90
  return r;
91
}
92
93
size_t Vio::read(unsigned char* buf, size_t size)
1337.4.1 by Eric Day
Removed unused functions and extra complexity in MySQL module. Not rewriting any code here (yet?), just reshuffling to make it easier to manage.
94
{
95
  size_t r;
96
97
  /* Ensure nobody uses vio_read_buff and vio_read simultaneously */
1878.6.1 by Thomi Richards
Converted the vio_st struct to the Vio class.
98
  assert(read_end == read_pos);
99
  r= ::read(sd, buf, size);
1337.4.1 by Eric Day
Removed unused functions and extra complexity in MySQL module. Not rewriting any code here (yet?), just reshuffling to make it easier to manage.
100
101
  return r;
102
}
103
1878.6.1 by Thomi Richards
Converted the vio_st struct to the Vio class.
104
size_t Vio::write(const unsigned char* buf, size_t size)
1337.4.1 by Eric Day
Removed unused functions and extra complexity in MySQL module. Not rewriting any code here (yet?), just reshuffling to make it easier to manage.
105
{
106
  size_t r;
107
1878.6.1 by Thomi Richards
Converted the vio_st struct to the Vio class.
108
  r = ::write(sd, buf, size);
1337.4.1 by Eric Day
Removed unused functions and extra complexity in MySQL module. Not rewriting any code here (yet?), just reshuffling to make it easier to manage.
109
110
  return r;
111
}
112
1878.6.1 by Thomi Richards
Converted the vio_st struct to the Vio class.
113
int Vio::blocking(bool set_blocking_mode, bool *old_mode)
1337.4.1 by Eric Day
Removed unused functions and extra complexity in MySQL module. Not rewriting any code here (yet?), just reshuffling to make it easier to manage.
114
{
115
  int r=0;
116
1878.6.1 by Thomi Richards
Converted the vio_st struct to the Vio class.
117
  // make sure ptr is not NULL:
118
  if (NULL != old_mode)
119
    *old_mode= drizzled::test(!(fcntl_mode & O_NONBLOCK));
1337.4.1 by Eric Day
Removed unused functions and extra complexity in MySQL module. Not rewriting any code here (yet?), just reshuffling to make it easier to manage.
120
1878.6.1 by Thomi Richards
Converted the vio_st struct to the Vio class.
121
  if (sd >= 0)
1337.4.1 by Eric Day
Removed unused functions and extra complexity in MySQL module. Not rewriting any code here (yet?), just reshuffling to make it easier to manage.
122
  {
1878.6.1 by Thomi Richards
Converted the vio_st struct to the Vio class.
123
    int old_fcntl=fcntl_mode;
1337.4.1 by Eric Day
Removed unused functions and extra complexity in MySQL module. Not rewriting any code here (yet?), just reshuffling to make it easier to manage.
124
    if (set_blocking_mode)
1878.6.1 by Thomi Richards
Converted the vio_st struct to the Vio class.
125
      fcntl_mode &= ~O_NONBLOCK; /* clear bit */
1337.4.1 by Eric Day
Removed unused functions and extra complexity in MySQL module. Not rewriting any code here (yet?), just reshuffling to make it easier to manage.
126
    else
1878.6.1 by Thomi Richards
Converted the vio_st struct to the Vio class.
127
      fcntl_mode |= O_NONBLOCK; /* set bit */
128
    if (old_fcntl != fcntl_mode)
1337.4.1 by Eric Day
Removed unused functions and extra complexity in MySQL module. Not rewriting any code here (yet?), just reshuffling to make it easier to manage.
129
    {
1878.6.1 by Thomi Richards
Converted the vio_st struct to the Vio class.
130
      r= fcntl(sd, F_SETFL, fcntl_mode);
1337.4.1 by Eric Day
Removed unused functions and extra complexity in MySQL module. Not rewriting any code here (yet?), just reshuffling to make it easier to manage.
131
      if (r == -1)
132
      {
1878.6.1 by Thomi Richards
Converted the vio_st struct to the Vio class.
133
        fcntl_mode= old_fcntl;
1337.4.1 by Eric Day
Removed unused functions and extra complexity in MySQL module. Not rewriting any code here (yet?), just reshuffling to make it easier to manage.
134
      }
135
    }
136
  }
137
138
  return r;
139
}
140
1878.6.1 by Thomi Richards
Converted the vio_st struct to the Vio class.
141
int Vio::fastsend()
1337.4.1 by Eric Day
Removed unused functions and extra complexity in MySQL module. Not rewriting any code here (yet?), just reshuffling to make it easier to manage.
142
{
143
  int nodelay = 1;
144
  int error;
145
1878.6.1 by Thomi Richards
Converted the vio_st struct to the Vio class.
146
  error= setsockopt(sd, IPPROTO_TCP, TCP_NODELAY,
1337.4.1 by Eric Day
Removed unused functions and extra complexity in MySQL module. Not rewriting any code here (yet?), just reshuffling to make it easier to manage.
147
                    &nodelay, sizeof(nodelay));
148
  if (error != 0)
149
  {
150
    perror("setsockopt");
151
  }
152
153
  return error;
154
}
155
1878.6.1 by Thomi Richards
Converted the vio_st struct to the Vio class.
156
int32_t Vio::keepalive(bool set_keep_alive)
1337.4.1 by Eric Day
Removed unused functions and extra complexity in MySQL module. Not rewriting any code here (yet?), just reshuffling to make it easier to manage.
157
{
158
  int r= 0;
159
  uint32_t opt= 0;
160
161
  if (set_keep_alive)
162
    opt= 1;
163
1878.6.1 by Thomi Richards
Converted the vio_st struct to the Vio class.
164
  r= setsockopt(sd, SOL_SOCKET, SO_KEEPALIVE, (char *) &opt, sizeof(opt));
1337.4.1 by Eric Day
Removed unused functions and extra complexity in MySQL module. Not rewriting any code here (yet?), just reshuffling to make it easier to manage.
165
  if (r != 0)
166
  {
167
    perror("setsockopt");
168
    assert(r == 0);
169
  }
170
171
  return r;
172
}
173
1878.6.1 by Thomi Richards
Converted the vio_st struct to the Vio class.
174
bool Vio::should_retry() const
1337.4.1 by Eric Day
Removed unused functions and extra complexity in MySQL module. Not rewriting any code here (yet?), just reshuffling to make it easier to manage.
175
{
176
  int en = errno;
177
  return (en == EAGAIN || en == EINTR ||
1878.6.1 by Thomi Richards
Converted the vio_st struct to the Vio class.
178
          en == EWOULDBLOCK);
1337.4.1 by Eric Day
Removed unused functions and extra complexity in MySQL module. Not rewriting any code here (yet?), just reshuffling to make it easier to manage.
179
}
180
1878.6.1 by Thomi Richards
Converted the vio_st struct to the Vio class.
181
bool Vio::was_interrupted() const
1337.4.1 by Eric Day
Removed unused functions and extra complexity in MySQL module. Not rewriting any code here (yet?), just reshuffling to make it easier to manage.
182
{
183
  int en= errno;
184
  return (en == EAGAIN || en == EINTR ||
1878.6.1 by Thomi Richards
Converted the vio_st struct to the Vio class.
185
          en == EWOULDBLOCK || en == ETIMEDOUT);
186
}
187
188
bool Vio::peer_addr(char *buf, uint16_t *port, size_t buflen) const
1337.4.1 by Eric Day
Removed unused functions and extra complexity in MySQL module. Not rewriting any code here (yet?), just reshuffling to make it easier to manage.
189
{
190
  int error;
191
  char port_buf[NI_MAXSERV];
1878.6.1 by Thomi Richards
Converted the vio_st struct to the Vio class.
192
  socklen_t al = sizeof(remote);
1337.4.1 by Eric Day
Removed unused functions and extra complexity in MySQL module. Not rewriting any code here (yet?), just reshuffling to make it easier to manage.
193
1878.6.1 by Thomi Richards
Converted the vio_st struct to the Vio class.
194
  if (getpeername(sd, (struct sockaddr *) (&remote),
195
                  &al) != 0)
1337.4.1 by Eric Day
Removed unused functions and extra complexity in MySQL module. Not rewriting any code here (yet?), just reshuffling to make it easier to manage.
196
  {
197
    return true;
198
  }
199
1878.6.1 by Thomi Richards
Converted the vio_st struct to the Vio class.
200
  if ((error= getnameinfo((struct sockaddr *)(&remote),
201
                          al,
1337.4.1 by Eric Day
Removed unused functions and extra complexity in MySQL module. Not rewriting any code here (yet?), just reshuffling to make it easier to manage.
202
                          buf, buflen,
203
                          port_buf, NI_MAXSERV, NI_NUMERICHOST|NI_NUMERICSERV)))
204
  {
205
    return true;
206
  }
207
208
  *port= (uint16_t)strtol(port_buf, (char **)NULL, 10);
209
210
  return false;
211
}
212
1878.6.1 by Thomi Richards
Converted the vio_st struct to the Vio class.
213
void Vio::timeout(bool is_sndtimeo, int32_t t)
1337.4.1 by Eric Day
Removed unused functions and extra complexity in MySQL module. Not rewriting any code here (yet?), just reshuffling to make it easier to manage.
214
{
215
  int error;
216
217
  /* POSIX specifies time as struct timeval. */
218
  struct timeval wait_timeout;
1878.6.1 by Thomi Richards
Converted the vio_st struct to the Vio class.
219
  wait_timeout.tv_sec= t;
1337.4.1 by Eric Day
Removed unused functions and extra complexity in MySQL module. Not rewriting any code here (yet?), just reshuffling to make it easier to manage.
220
  wait_timeout.tv_usec= 0;
221
1878.6.1 by Thomi Richards
Converted the vio_st struct to the Vio class.
222
  assert(t >= 0 && t <= INT32_MAX);
223
  assert(sd != -1);
224
  error= setsockopt(sd, SOL_SOCKET, is_sndtimeo ? SO_SNDTIMEO : SO_RCVTIMEO,
1337.4.1 by Eric Day
Removed unused functions and extra complexity in MySQL module. Not rewriting any code here (yet?), just reshuffling to make it easier to manage.
225
                    &wait_timeout,
226
                    (socklen_t)sizeof(struct timeval));
227
  if (error == -1 && errno != ENOPROTOOPT)
228
  {
229
    perror("setsockopt");
230
    assert(error == 0);
231
  }
232
}
233
1878.6.1 by Thomi Richards
Converted the vio_st struct to the Vio class.
234
int Vio::get_errno() const
235
{
236
  return errno;
237
}
238
239
int Vio::get_fd() const
240
{
241
  return sd;
242
}
243
244
245
char *Vio::get_read_pos() const
246
{
247
  return read_pos;
248
}
249
250
char *Vio::get_read_end() const
251
{
252
  return read_end;
253
}
254