1
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
3
This library is free software; you can redistribute it and/or
4
modify it under the terms of the GNU Library General Public
5
License as published by the Free Software Foundation; either
6
version 2 of the License, or (at your option) any later version.
8
This library is distributed in the hope that it will be useful,
9
but WITHOUT ANY WARRANTY; without even the implied warranty of
10
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
Library General Public License for more details.
13
You should have received a copy of the GNU Library General Public
14
License along with this library; if not, write to the Free
15
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19
Note that we can't have assertion on file descriptors; The reason for
20
this is that during mysql shutdown, another thread can close a file
21
we are working on. In this case we should just return read errors from
27
#ifndef HAVE_VIO /* is Vio suppored by the Vio lib ? */
40
#include <sys/ioctl.h>
41
#define ioctlsocket(A,B,C) ioctl((A),(B),(void *)(C),sizeof(*(C)))
43
#endif /* defined(__EMX__) */
45
#if defined(MSDOS) || defined(__WIN__)
50
#define errno WSAGetLastError()
51
#define EINTR WSAEINTR
52
#define EAGAIN WSAEINPROGRESS
54
#define O_NONBLOCK 1 /* For emulation of fcntl() */
57
#define EWOULDBLOCK EAGAIN
66
my_socket sd; /* my_socket - real or imaginary */
68
my_bool localhost; /* Are we from localhost? */
69
int fcntl_mode; /* Buffered fcntl(sd,F_GETFL) */
70
struct sockaddr_in local; /* Local internet address */
71
struct sockaddr_in remote; /* Remote internet address */
72
enum enum_vio_type type; /* Type of connection */
73
char desc[30]; /* String description */
74
/* #ifdef EMBEDDED_LIBRARY */
77
char *packets, **last_packet;
78
char *where_in_packet, *end_of_packet;
84
/* Initialize the communication buffer */
86
Vio *vio_new(my_socket sd, enum enum_vio_type type, my_bool localhost)
89
vio = (Vio *) my_malloc (sizeof(*vio),MYF(MY_WME|MY_ZEROFILL));
92
init_alloc_root(&vio->root, 8192, 1024);
93
vio->root.min_malloc = sizeof(char *) + 4;
94
vio->last_packet = &vio->packets;
102
Vio *vio_new_win32pipe(HANDLE hPipe)
109
void vio_delete(Vio * vio)
113
if (vio->type != VIO_CLOSED) vio_close(vio);
114
free_root(&vio->root, MYF(0));
115
my_free((gptr)vio, MYF(0));
119
void vio_reset(Vio *vio)
121
free_root(&vio->root, MYF(MY_KEEP_PREALLOC));
122
vio->packets = vio->where_in_packet = vio->end_of_packet = 0;
123
vio->last_packet = &vio->packets;
126
int vio_errno(Vio *vio __attribute__((unused)))
128
return errno; /* On Win32 this mapped to WSAGetLastError() */
131
int vio_read(Vio * vio, gptr buf, int size)
134
if (vio->where_in_packet >= vio->end_of_packet)
136
dbug_assert(vio->packets);
137
vio->where_in_packet = vio->packets + sizeof(char *) + 4;
138
vio->end_of_packet = vio->where_in_packet +
139
uint4korr(vio->packets + sizeof(char *));
140
vio->packets = *(char **)vio->packets;
142
memcpy(buf, vio->where_in_packet, size);
143
vio->where_in_packet += size;
147
int vio_write(Vio * vio, const gptr buf, int size)
155
if ((packet = alloc_root(&vio->root, sizeof(char*) + 4 + size)))
157
*vio->last_packet = packet;
158
vio->last_packet = (char **)packet;
159
*((char **)packet) = 0; /* safety */
160
packet += sizeof(char *);
161
int4store(packet, size);
162
memcpy(packet + 4, buf, size);
169
int vio_blocking(Vio * vio, my_bool set_blocking_mode)
176
vio_is_blocking(Vio * vio)
182
int vio_fastsend(Vio * vio)
188
int vio_keepalive(Vio* vio, my_bool set_keep_alive)
196
vio_should_retry(Vio * vio __attribute__((unused)))
199
return en == EAGAIN || en == EINTR || en == EWOULDBLOCK;
203
int vio_close(Vio * vio)
210
const char *vio_description(Vio * vio)
212
return "embedded vio";
215
enum enum_vio_type vio_type(Vio* vio)
220
my_socket vio_fd(Vio* vio)
226
my_bool vio_peer_addr(Vio * vio, char *buf)
232
void vio_in_addr(Vio *vio, struct in_addr *in)
236
#endif /* HAVE_VIO */