~ubuntu-branches/ubuntu/saucy/drizzle/saucy-proposed

« back to all changes in this revision

Viewing changes to plugin/mysql_protocol/vio.cc

  • Committer: Package Import Robot
  • Author(s): Clint Byrum
  • Date: 2012-06-19 10:46:49 UTC
  • mfrom: (1.1.6)
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20120619104649-e2l0ggd4oz3um0f4
Tags: upstream-7.1.36-stable
ImportĀ upstreamĀ versionĀ 7.1.36-stable

Show diffs side-by-side

added added

removed removed

Lines of Context:
40
40
 
41
41
using namespace std;
42
42
 
43
 
namespace drizzle_plugin
44
 
{
 
43
namespace drizzle_plugin {
45
44
 
46
45
Vio::Vio(int nsd) :
47
 
  closed(false),
48
 
  sd(nsd),
49
 
  fcntl_mode(0),
50
 
  local(),
51
 
  remote(),
52
 
  read_pos(NULL),
53
 
  read_end(NULL)
 
46
  sd(nsd)
54
47
{
55
48
  /*
56
49
    We call fcntl() to set the flags and then immediately read them back
68
61
 
69
62
Vio::~Vio()
70
63
{
71
 
 if (!closed)
72
 
    close();
 
64
  close();
73
65
}
74
66
 
75
67
int Vio::close()
76
68
{
77
 
  int r=0;
78
 
  if (!closed)
 
69
  int r= 0;
 
70
  if (sd != -1)
79
71
  {
80
72
    assert(sd >= 0);
81
73
    if (shutdown(sd, SHUT_RDWR))
82
74
      r= -1;
83
75
    if (::close(sd))
84
76
      r= -1;
 
77
    sd=   -1;
85
78
  }
86
 
  closed= true;
87
 
  sd=   -1;
88
 
 
89
79
  return r;
90
80
}
91
81
 
92
82
size_t Vio::read(unsigned char* buf, size_t size)
93
83
{
94
 
  size_t r;
95
 
 
96
 
  /* Ensure nobody uses vio_read_buff and vio_read simultaneously */
97
 
  assert(read_end == read_pos);
98
 
  r= ::read(sd, buf, size);
99
 
 
100
 
  return r;
 
84
  return ::read(sd, buf, size);
101
85
}
102
86
 
103
87
size_t Vio::write(const unsigned char* buf, size_t size)
104
88
{
105
 
  size_t r;
106
 
 
107
 
  r = ::write(sd, buf, size);
108
 
 
109
 
  return r;
 
89
  return ::write(sd, buf, size);
110
90
}
111
91
 
112
92
int Vio::blocking(bool set_blocking_mode, bool *old_mode)
140
120
int Vio::fastsend()
141
121
{
142
122
  int nodelay = 1;
143
 
  int error;
144
 
 
145
 
  error= setsockopt(sd, IPPROTO_TCP, TCP_NODELAY,
146
 
                    &nodelay, sizeof(nodelay));
147
 
  if (error != 0)
148
 
  {
 
123
  int error= setsockopt(sd, IPPROTO_TCP, TCP_NODELAY, &nodelay, sizeof(nodelay));
 
124
  if (error)
149
125
    perror("setsockopt");
150
 
  }
151
 
 
152
126
  return error;
153
127
}
154
128
 
155
129
int32_t Vio::keepalive(bool set_keep_alive)
156
130
{
157
 
  int r= 0;
158
 
  uint32_t opt= 0;
159
 
 
160
 
  if (set_keep_alive)
161
 
    opt= 1;
162
 
 
163
 
  r= setsockopt(sd, SOL_SOCKET, SO_KEEPALIVE, (char *) &opt, sizeof(opt));
164
 
  if (r != 0)
 
131
  uint32_t opt= set_keep_alive;
 
132
  int r= setsockopt(sd, SOL_SOCKET, SO_KEEPALIVE, (char *) &opt, sizeof(opt));
 
133
  if (r)
165
134
  {
166
135
    perror("setsockopt");
167
 
    assert(r == 0);
 
136
    assert(false);
168
137
  }
169
 
 
170
138
  return r;
171
139
}
172
140
 
173
141
bool Vio::should_retry() const
174
142
{
175
143
  int en = errno;
176
 
  return (en == EAGAIN || en == EINTR ||
177
 
          en == EWOULDBLOCK);
 
144
  return en == EAGAIN || en == EINTR || en == EWOULDBLOCK;
178
145
}
179
146
 
180
147
bool Vio::was_interrupted() const
181
148
{
182
149
  int en= errno;
183
 
  return (en == EAGAIN || en == EINTR ||
184
 
          en == EWOULDBLOCK || en == ETIMEDOUT);
 
150
  return en == EAGAIN || en == EINTR || en == EWOULDBLOCK || en == ETIMEDOUT;
185
151
}
186
152
 
187
 
bool Vio::peer_addr(char *buf, uint16_t *port, size_t buflen) const
 
153
bool Vio::peer_addr(char *buf, size_t buflen, uint16_t& port) const
188
154
{
189
 
  int error;
190
155
  char port_buf[NI_MAXSERV];
 
156
  sockaddr_storage remote;
191
157
  socklen_t al = sizeof(remote);
192
158
 
193
 
  if (getpeername(sd, (struct sockaddr *) (&remote),
194
 
                  &al) != 0)
195
 
  {
196
 
    return true;
197
 
  }
198
 
 
199
 
  if ((error= getnameinfo((struct sockaddr *)(&remote),
200
 
                          al,
201
 
                          buf, buflen,
202
 
                          port_buf, NI_MAXSERV, NI_NUMERICHOST|NI_NUMERICSERV)))
203
 
  {
204
 
    return true;
205
 
  }
206
 
 
207
 
  *port= (uint16_t)strtol(port_buf, (char **)NULL, 10);
 
159
  if (getpeername(sd, (sockaddr *) (&remote), &al) != 0)
 
160
    return true;
 
161
 
 
162
  if (getnameinfo((struct sockaddr *)(&remote), al, buf, buflen, port_buf, NI_MAXSERV, NI_NUMERICHOST|NI_NUMERICSERV))
 
163
  {
 
164
    return true;
 
165
  }
 
166
 
 
167
  port= (uint16_t)strtol(port_buf, (char **)NULL, 10);
208
168
 
209
169
  return false;
210
170
}
211
171
 
212
172
void Vio::timeout(bool is_sndtimeo, int32_t t)
213
173
{
214
 
  int error;
215
 
 
216
 
  /* POSIX specifies time as struct timeval. */
217
 
  struct timeval wait_timeout;
 
174
  timeval wait_timeout;
218
175
  wait_timeout.tv_sec= t;
219
176
  wait_timeout.tv_usec= 0;
220
177
 
221
178
  assert(t >= 0 && t <= INT32_MAX);
222
179
  assert(sd != -1);
223
 
  error= setsockopt(sd, SOL_SOCKET, is_sndtimeo ? SO_SNDTIMEO : SO_RCVTIMEO,
224
 
                    &wait_timeout,
 
180
  int error= setsockopt(sd, SOL_SOCKET, is_sndtimeo ? SO_SNDTIMEO : SO_RCVTIMEO, &wait_timeout,
225
181
                    (socklen_t)sizeof(struct timeval));
226
182
  if (error == -1 && errno != ENOPROTOOPT)
227
183
  {
240
196
  return sd;
241
197
}
242
198
 
243
 
 
244
 
char *Vio::get_read_pos() const
245
 
{
246
 
  return read_pos;
247
 
}
248
 
 
249
 
char *Vio::get_read_end() const
250
 
{
251
 
  return read_end;
252
 
}
253
 
 
254
199
} /* namespace drizzle_plugin */