~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/mysql_protocol/vio.cc

  • Committer: Eric Day
  • Date: 2009-11-10 21:50:22 UTC
  • mto: This revision was merged to the branch mainline in revision 1218.
  • Revision ID: eday@oddments.org-20091110215022-0b2nqmurv7b2l6wo
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.

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