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