~drizzle-trunk/drizzle/development

1 by brian
clean slate
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
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
23
#include "config.h"
24
575.2.2 by Monty Taylor
Moved vio stuff into libdrizzle.
25
#define DONT_MAP_VIO
779.3.37 by Monty Taylor
Renmaed libdrizzle in the tree to libdrizzleclient to avoid namespace clashes
26
#include "vio.h"
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
27
28
#include <fcntl.h>
29
30
#include <cstdio>
31
#include <cstring>
32
#include <cstdlib>
33
#include <memory>
34
35
using namespace std;
1 by brian
clean slate
36
37
/*
38
 * Helper to fill most of the Vio* with defaults.
39
 */
40
840.1.19 by Monty Taylor
Renamed vio.
41
static void drizzleclient_vio_init(Vio* vio, enum enum_vio_type type,
268 by Brian Aker
Merging up a bunch of assert() and cleanup of my_sock typedef
42
                     int sd, uint32_t flags)
1 by brian
clean slate
43
{
212.6.13 by Mats Kindahl
Removing redundant use of casts in vio/ for memset()
44
  memset(vio, 0, sizeof(*vio));
1 by brian
clean slate
45
  vio->type	= type;
46
  vio->sd	= sd;
47
  if ((flags & VIO_BUFFERED_READ) &&
575.2.2 by Monty Taylor
Moved vio stuff into libdrizzle.
48
      !(vio->read_buffer= (char*)malloc(VIO_READ_BUFFER_SIZE)))
1 by brian
clean slate
49
    flags&= ~VIO_BUFFERED_READ;
50
  {
840.1.19 by Monty Taylor
Renamed vio.
51
    vio->viodelete	=drizzleclient_vio_delete;
52
    vio->vioerrno	=drizzleclient_vio_errno;
53
    vio->read= (flags & VIO_BUFFERED_READ) ? drizzleclient_vio_read_buff : drizzleclient_vio_read;
54
    vio->write		=drizzleclient_vio_write;
55
    vio->fastsend	=drizzleclient_vio_fastsend;
56
    vio->viokeepalive	=drizzleclient_vio_keepalive;
57
    vio->should_retry	=drizzleclient_vio_should_retry;
58
    vio->was_interrupted=drizzleclient_vio_was_interrupted;
59
    vio->vioclose	=drizzleclient_vio_close;
60
    vio->peer_addr	=drizzleclient_vio_peer_addr;
61
    vio->vioblocking	=drizzleclient_vio_blocking;
62
    vio->is_blocking	=drizzleclient_vio_is_blocking;
63
    vio->timeout	=drizzleclient_vio_timeout;
1 by brian
clean slate
64
  }
65
}
66
67
68
/* Reset initialized VIO to use with another transport type */
69
840.1.19 by Monty Taylor
Renamed vio.
70
void drizzleclient_vio_reset(Vio* vio, enum enum_vio_type type,
482 by Brian Aker
Remove uint.
71
               int sd, uint32_t flags)
1 by brian
clean slate
72
{
477 by Monty Taylor
Removed my_free(). It turns out that it had been def'd to ignore the flags passed to it in the second arg anyway. Gotta love that.
73
  free(vio->read_buffer);
840.1.19 by Monty Taylor
Renamed vio.
74
  drizzleclient_vio_init(vio, type, sd, flags);
1 by brian
clean slate
75
}
76
77
78
/* Open the socket or TCP/IP connection and read the fnctl() status */
79
840.1.19 by Monty Taylor
Renamed vio.
80
Vio *drizzleclient_vio_new(int sd, enum enum_vio_type type, uint32_t flags)
1 by brian
clean slate
81
{
575.2.2 by Monty Taylor
Moved vio stuff into libdrizzle.
82
  Vio *vio = (Vio*) malloc(sizeof(Vio));
127 by brian
Removed DBUG statements from VIO
83
575.2.2 by Monty Taylor
Moved vio stuff into libdrizzle.
84
  if (vio != NULL)
1 by brian
clean slate
85
  {
840.1.19 by Monty Taylor
Renamed vio.
86
    drizzleclient_vio_init(vio, type, sd, flags);
172 by Brian Aker
First pass of cleanup
87
    sprintf(vio->desc, "TCP/IP (%d)", vio->sd);
1 by brian
clean slate
88
    /*
89
      We call fcntl() to set the flags and then immediately read them back
90
      to make sure that we and the system are in agreement on the state of
91
      things.
92
93
      An example of why we need to do this is FreeBSD (and apparently some
94
      other BSD-derived systems, like Mac OS X), where the system sometimes
95
      reports that the socket is set for non-blocking when it really will
96
      block.
97
    */
98
    fcntl(sd, F_SETFL, 0);
99
    vio->fcntl_mode= fcntl(sd, F_GETFL);
115 by Brian Aker
Cleanup in VIO to remove dead options
100
  }
127 by brian
Removed DBUG statements from VIO
101
  return vio;
115 by Brian Aker
Cleanup in VIO to remove dead options
102
}
1 by brian
clean slate
103
104
840.1.19 by Monty Taylor
Renamed vio.
105
void drizzleclient_vio_delete(Vio* vio)
1 by brian
clean slate
106
{
107
  if (!vio)
108
    return; /* It must be safe to delete null pointers. */
109
110
  if (vio->type != VIO_CLOSED)
111
    vio->vioclose(vio);
481 by Brian Aker
Remove all of uchar.
112
  free((unsigned char*) vio->read_buffer);
1166.4.2 by Diego Medina
Removed changes to vio.cc
113
  free((unsigned char*) vio);
1 by brian
clean slate
114
}
115
116
117
/*
118
  Cleanup memory allocated by vio or the
119
  components below it when application finish
120
121
*/
840.1.19 by Monty Taylor
Renamed vio.
122
void drizzleclient_vio_end(void)
1 by brian
clean slate
123
{
124
}