~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/drizzle_protocol/vio.cc

  • Committer: lbieber
  • Date: 2010-10-02 19:48:35 UTC
  • mfrom: (1730.6.19 drizzle-make-lcov)
  • Revision ID: lbieber@orisndriz08-20101002194835-q5zd9qc4lvx1xnfo
Merge Hartmut - clean up lex, now require flex to build, also "make lcov" improvements

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
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
*/
 
22
 
 
23
#include "config.h"
 
24
 
 
25
#define DONT_MAP_VIO
 
26
#include "vio.h"
 
27
 
 
28
#include <fcntl.h>
 
29
 
 
30
#include <cstdio>
 
31
#include <cstring>
 
32
#include <cstdlib>
 
33
#include <memory>
 
34
 
 
35
using namespace std;
 
36
 
 
37
namespace drizzle_protocol
 
38
{
 
39
 
 
40
/*
 
41
 * Helper to fill most of the Vio* with defaults.
 
42
 */
 
43
 
 
44
static void drizzleclient_vio_init(Vio* vio, enum enum_vio_type type,
 
45
                     int sd, uint32_t flags)
 
46
{
 
47
  memset(vio, 0, sizeof(*vio));
 
48
  vio->type     = type;
 
49
  vio->sd       = sd;
 
50
  if ((flags & VIO_BUFFERED_READ) &&
 
51
      !(vio->read_buffer= (char*)malloc(VIO_READ_BUFFER_SIZE)))
 
52
    flags&= ~VIO_BUFFERED_READ;
 
53
  {
 
54
    vio->viodelete      =drizzleclient_vio_delete;
 
55
    vio->vioerrno       =drizzleclient_vio_errno;
 
56
    vio->read= (flags & VIO_BUFFERED_READ) ? drizzleclient_vio_read_buff : drizzleclient_vio_read;
 
57
    vio->write          =drizzleclient_vio_write;
 
58
    vio->fastsend       =drizzleclient_vio_fastsend;
 
59
    vio->viokeepalive   =drizzleclient_vio_keepalive;
 
60
    vio->should_retry   =drizzleclient_vio_should_retry;
 
61
    vio->was_interrupted=drizzleclient_vio_was_interrupted;
 
62
    vio->vioclose       =drizzleclient_vio_close;
 
63
    vio->peer_addr      =drizzleclient_vio_peer_addr;
 
64
    vio->vioblocking    =drizzleclient_vio_blocking;
 
65
    vio->is_blocking    =drizzleclient_vio_is_blocking;
 
66
    vio->timeout        =drizzleclient_vio_timeout;
 
67
  }
 
68
}
 
69
 
 
70
 
 
71
/* Reset initialized VIO to use with another transport type */
 
72
 
 
73
void drizzleclient_vio_reset(Vio* vio, enum enum_vio_type type,
 
74
               int sd, uint32_t flags)
 
75
{
 
76
  free(vio->read_buffer);
 
77
  drizzleclient_vio_init(vio, type, sd, flags);
 
78
}
 
79
 
 
80
 
 
81
/* Open the socket or TCP/IP connection and read the fnctl() status */
 
82
 
 
83
Vio *drizzleclient_vio_new(int sd, enum enum_vio_type type, uint32_t flags)
 
84
{
 
85
  Vio *vio = (Vio*) malloc(sizeof(Vio));
 
86
 
 
87
  if (vio != NULL)
 
88
  {
 
89
    drizzleclient_vio_init(vio, type, sd, flags);
 
90
    sprintf(vio->desc, "TCP/IP (%d)", vio->sd);
 
91
    /*
 
92
      We call fcntl() to set the flags and then immediately read them back
 
93
      to make sure that we and the system are in agreement on the state of
 
94
      things.
 
95
 
 
96
      An example of why we need to do this is FreeBSD (and apparently some
 
97
      other BSD-derived systems, like Mac OS X), where the system sometimes
 
98
      reports that the socket is set for non-blocking when it really will
 
99
      block.
 
100
    */
 
101
    fcntl(sd, F_SETFL, 0);
 
102
    vio->fcntl_mode= fcntl(sd, F_GETFL);
 
103
  }
 
104
  return vio;
 
105
}
 
106
 
 
107
 
 
108
void drizzleclient_vio_delete(Vio* vio)
 
109
{
 
110
  if (!vio)
 
111
    return; /* It must be safe to delete null pointers. */
 
112
 
 
113
  if (vio->type != VIO_CLOSED)
 
114
    vio->vioclose(vio);
 
115
  free((unsigned char*) vio->read_buffer);
 
116
  free((unsigned char*) vio);
 
117
}
 
118
 
 
119
 
 
120
/*
 
121
  Cleanup memory allocated by vio or the
 
122
  components below it when application finish
 
123
 
 
124
*/
 
125
void drizzleclient_vio_end(void)
 
126
{ }
 
127
 
 
128
} /* namespace drizzle_protocol */