~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
575.2.2 by Monty Taylor
Moved vio stuff into libdrizzle.
23
#define DONT_MAP_VIO
24
#include <drizzled/global.h>
25
#include <libdrizzle/vio.h>
26
#include <string.h>
27
#include <stdint.h>
1 by brian
clean slate
28
29
/*
30
 * Helper to fill most of the Vio* with defaults.
31
 */
32
33
static void 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
34
                     int sd, uint32_t flags)
1 by brian
clean slate
35
{
36
#ifndef HAVE_VIO_READ_BUFF
37
  flags&= ~VIO_BUFFERED_READ;
38
#endif
212.6.13 by Mats Kindahl
Removing redundant use of casts in vio/ for memset()
39
  memset(vio, 0, sizeof(*vio));
1 by brian
clean slate
40
  vio->type	= type;
41
  vio->sd	= sd;
42
  if ((flags & VIO_BUFFERED_READ) &&
575.2.2 by Monty Taylor
Moved vio stuff into libdrizzle.
43
      !(vio->read_buffer= (char*)malloc(VIO_READ_BUFFER_SIZE)))
1 by brian
clean slate
44
    flags&= ~VIO_BUFFERED_READ;
45
  {
46
    vio->viodelete	=vio_delete;
47
    vio->vioerrno	=vio_errno;
48
    vio->read= (flags & VIO_BUFFERED_READ) ? vio_read_buff : vio_read;
49
    vio->write		=vio_write;
50
    vio->fastsend	=vio_fastsend;
51
    vio->viokeepalive	=vio_keepalive;
52
    vio->should_retry	=vio_should_retry;
53
    vio->was_interrupted=vio_was_interrupted;
54
    vio->vioclose	=vio_close;
55
    vio->peer_addr	=vio_peer_addr;
56
    vio->vioblocking	=vio_blocking;
57
    vio->is_blocking	=vio_is_blocking;
58
    vio->timeout	=vio_timeout;
59
  }
60
}
61
62
63
/* Reset initialized VIO to use with another transport type */
64
65
void vio_reset(Vio* vio, enum enum_vio_type type,
482 by Brian Aker
Remove uint.
66
               int sd, uint32_t flags)
1 by brian
clean slate
67
{
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.
68
  free(vio->read_buffer);
172 by Brian Aker
First pass of cleanup
69
  vio_init(vio, type, sd, flags);
1 by brian
clean slate
70
}
71
72
73
/* Open the socket or TCP/IP connection and read the fnctl() status */
74
482 by Brian Aker
Remove uint.
75
Vio *vio_new(int sd, enum enum_vio_type type, uint32_t flags)
1 by brian
clean slate
76
{
575.2.2 by Monty Taylor
Moved vio stuff into libdrizzle.
77
  Vio *vio = (Vio*) malloc(sizeof(Vio));
127 by brian
Removed DBUG statements from VIO
78
575.2.2 by Monty Taylor
Moved vio stuff into libdrizzle.
79
  if (vio != NULL)
1 by brian
clean slate
80
  {
172 by Brian Aker
First pass of cleanup
81
    vio_init(vio, type, sd, flags);
82
    sprintf(vio->desc, "TCP/IP (%d)", vio->sd);
1 by brian
clean slate
83
    /*
84
      We call fcntl() to set the flags and then immediately read them back
85
      to make sure that we and the system are in agreement on the state of
86
      things.
87
88
      An example of why we need to do this is FreeBSD (and apparently some
89
      other BSD-derived systems, like Mac OS X), where the system sometimes
90
      reports that the socket is set for non-blocking when it really will
91
      block.
92
    */
93
    fcntl(sd, F_SETFL, 0);
94
    vio->fcntl_mode= fcntl(sd, F_GETFL);
115 by Brian Aker
Cleanup in VIO to remove dead options
95
  }
127 by brian
Removed DBUG statements from VIO
96
  return vio;
115 by Brian Aker
Cleanup in VIO to remove dead options
97
}
1 by brian
clean slate
98
99
100
void vio_delete(Vio* vio)
101
{
102
  if (!vio)
103
    return; /* It must be safe to delete null pointers. */
104
105
  if (vio->type != VIO_CLOSED)
106
    vio->vioclose(vio);
481 by Brian Aker
Remove all of uchar.
107
  free((unsigned char*) vio->read_buffer);
108
  free((unsigned char*) vio);
1 by brian
clean slate
109
}
110
111
112
/*
113
  Cleanup memory allocated by vio or the
114
  components below it when application finish
115
116
*/
117
void vio_end(void)
118
{
119
}