2001-12-06 13:10:51 +01:00
|
|
|
/* Copyright (C) 2000 MySQL AB
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
2001-05-20 14:04:46 +02:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2001-12-06 13:10:51 +01:00
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
2001-05-20 14:04:46 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
Note that we can't have assertion on file descriptors; The reason for
|
|
|
|
this is that during mysql shutdown, another thread can close a file
|
|
|
|
we are working on. In this case we should just return read errors from
|
|
|
|
the file descriptior.
|
|
|
|
*/
|
|
|
|
|
2003-08-27 01:51:39 +02:00
|
|
|
#include "vio_priv.h"
|
2001-05-20 14:04:46 +02:00
|
|
|
|
2001-05-31 16:18:25 +02:00
|
|
|
int vio_errno(Vio *vio __attribute__((unused)))
|
2001-05-20 14:04:46 +02:00
|
|
|
{
|
2001-10-08 22:20:19 +02:00
|
|
|
return socket_errno; /* On Win32 this mapped to WSAGetLastError() */
|
2001-05-20 14:04:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-05-31 16:18:25 +02:00
|
|
|
int vio_read(Vio * vio, gptr buf, int size)
|
2001-05-20 14:04:46 +02:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
DBUG_ENTER("vio_read");
|
|
|
|
DBUG_PRINT("enter", ("sd=%d, buf=%p, size=%d", vio->sd, buf, size));
|
2002-11-14 20:16:30 +01:00
|
|
|
|
2001-05-20 14:04:46 +02:00
|
|
|
#ifdef __WIN__
|
|
|
|
r = recv(vio->sd, buf, size,0);
|
|
|
|
#else
|
|
|
|
errno=0; /* For linux */
|
|
|
|
r = read(vio->sd, buf, size);
|
|
|
|
#endif /* __WIN__ */
|
|
|
|
#ifndef DBUG_OFF
|
|
|
|
if (r < 0)
|
|
|
|
{
|
2001-06-05 02:38:10 +02:00
|
|
|
DBUG_PRINT("vio_error", ("Got error %d during read",errno));
|
2001-05-20 14:04:46 +02:00
|
|
|
}
|
|
|
|
#endif /* DBUG_OFF */
|
|
|
|
DBUG_PRINT("exit", ("%d", r));
|
|
|
|
DBUG_RETURN(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-05-31 16:18:25 +02:00
|
|
|
int vio_write(Vio * vio, const gptr buf, int size)
|
2001-05-20 14:04:46 +02:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
DBUG_ENTER("vio_write");
|
|
|
|
DBUG_PRINT("enter", ("sd=%d, buf=%p, size=%d", vio->sd, buf, size));
|
2002-11-14 20:16:30 +01:00
|
|
|
#ifdef __WIN__
|
|
|
|
r = send(vio->sd, buf, size,0);
|
2001-05-20 14:04:46 +02:00
|
|
|
#else
|
|
|
|
r = write(vio->sd, buf, size);
|
|
|
|
#endif /* __WIN__ */
|
|
|
|
#ifndef DBUG_OFF
|
|
|
|
if (r < 0)
|
|
|
|
{
|
2001-10-08 22:20:19 +02:00
|
|
|
DBUG_PRINT("vio_error", ("Got error on write: %d",socket_errno));
|
2001-05-20 14:04:46 +02:00
|
|
|
}
|
|
|
|
#endif /* DBUG_OFF */
|
|
|
|
DBUG_PRINT("exit", ("%d", r));
|
|
|
|
DBUG_RETURN(r);
|
|
|
|
}
|
|
|
|
|
2002-08-08 02:12:02 +02:00
|
|
|
int vio_blocking(Vio * vio __attribute__((unused)), my_bool set_blocking_mode,
|
|
|
|
my_bool *old_mode)
|
2001-05-20 14:04:46 +02:00
|
|
|
{
|
|
|
|
int r=0;
|
|
|
|
DBUG_ENTER("vio_blocking");
|
2002-08-08 02:12:02 +02:00
|
|
|
|
|
|
|
*old_mode= test(!(vio->fcntl_mode & O_NONBLOCK));
|
|
|
|
DBUG_PRINT("enter", ("set_blocking_mode: %d old_mode: %d",
|
|
|
|
(int) set_blocking_mode, (int) *old_mode));
|
2001-05-20 14:04:46 +02:00
|
|
|
|
2005-07-12 18:31:09 +02:00
|
|
|
#if !defined(__WIN__) && !defined(__EMX__)
|
2001-05-20 14:04:46 +02:00
|
|
|
#if !defined(NO_FCNTL_NONBLOCK)
|
|
|
|
if (vio->sd >= 0)
|
|
|
|
{
|
|
|
|
int old_fcntl=vio->fcntl_mode;
|
|
|
|
if (set_blocking_mode)
|
|
|
|
vio->fcntl_mode &= ~O_NONBLOCK; /* clear bit */
|
|
|
|
else
|
|
|
|
vio->fcntl_mode |= O_NONBLOCK; /* set bit */
|
|
|
|
if (old_fcntl != vio->fcntl_mode)
|
|
|
|
r = fcntl(vio->sd, F_SETFL, vio->fcntl_mode);
|
|
|
|
}
|
2002-08-26 11:28:49 +02:00
|
|
|
#else
|
|
|
|
r= set_blocking_mode ? 0 : 1;
|
2001-05-20 14:04:46 +02:00
|
|
|
#endif /* !defined(NO_FCNTL_NONBLOCK) */
|
|
|
|
#else /* !defined(__WIN__) && !defined(__EMX__) */
|
|
|
|
#ifndef __EMX__
|
|
|
|
if (vio->type != VIO_TYPE_NAMEDPIPE)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
ulong arg;
|
|
|
|
int old_fcntl=vio->fcntl_mode;
|
|
|
|
if (set_blocking_mode)
|
|
|
|
{
|
|
|
|
arg = 0;
|
|
|
|
vio->fcntl_mode &= ~O_NONBLOCK; /* clear bit */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
arg = 1;
|
|
|
|
vio->fcntl_mode |= O_NONBLOCK; /* set bit */
|
|
|
|
}
|
|
|
|
if (old_fcntl != vio->fcntl_mode)
|
2005-07-12 18:31:09 +02:00
|
|
|
r = ioctlsocket(vio->sd,FIONBIO,(void*) &arg);
|
2001-05-20 14:04:46 +02:00
|
|
|
}
|
2002-08-26 11:28:49 +02:00
|
|
|
#ifndef __EMX__
|
|
|
|
else
|
|
|
|
r= test(!(vio->fcntl_mode & O_NONBLOCK)) != set_blocking_mode;
|
|
|
|
#endif /* __EMX__ */
|
2001-05-20 14:04:46 +02:00
|
|
|
#endif /* !defined(__WIN__) && !defined(__EMX__) */
|
2002-06-11 10:20:31 +02:00
|
|
|
DBUG_PRINT("exit", ("%d", r));
|
2001-05-20 14:04:46 +02:00
|
|
|
DBUG_RETURN(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
my_bool
|
2001-05-31 16:18:25 +02:00
|
|
|
vio_is_blocking(Vio * vio)
|
2001-05-20 14:04:46 +02:00
|
|
|
{
|
|
|
|
my_bool r;
|
|
|
|
DBUG_ENTER("vio_is_blocking");
|
|
|
|
r = !(vio->fcntl_mode & O_NONBLOCK);
|
|
|
|
DBUG_PRINT("exit", ("%d", (int) r));
|
|
|
|
DBUG_RETURN(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-05-31 16:18:25 +02:00
|
|
|
int vio_fastsend(Vio * vio __attribute__((unused)))
|
2001-05-20 14:04:46 +02:00
|
|
|
{
|
|
|
|
int r=0;
|
|
|
|
DBUG_ENTER("vio_fastsend");
|
|
|
|
|
2005-01-25 00:31:51 +01:00
|
|
|
#if defined(IPTOS_THROUGHPUT) && !defined(__EMX__)
|
2001-05-20 14:04:46 +02:00
|
|
|
{
|
|
|
|
int tos = IPTOS_THROUGHPUT;
|
2005-01-25 00:31:51 +01:00
|
|
|
r= setsockopt(vio->sd, IPPROTO_IP, IP_TOS, (void *) &tos, sizeof(tos));
|
|
|
|
}
|
|
|
|
#endif /* IPTOS_THROUGHPUT && !__EMX__ */
|
|
|
|
if (!r)
|
|
|
|
{
|
|
|
|
#ifdef __WIN__
|
|
|
|
BOOL nodelay= 1;
|
|
|
|
r= setsockopt(vio->sd, IPPROTO_TCP, TCP_NODELAY, (const char*) &nodelay,
|
|
|
|
sizeof(nodelay));
|
|
|
|
#else
|
|
|
|
int nodelay = 1;
|
|
|
|
r= setsockopt(vio->sd, IPPROTO_TCP, TCP_NODELAY, (void*) &nodelay,
|
|
|
|
sizeof(nodelay));
|
|
|
|
#endif /* __WIN__ */
|
|
|
|
}
|
|
|
|
if (r)
|
|
|
|
{
|
|
|
|
DBUG_PRINT("warning", ("Couldn't set socket option for fast send"));
|
|
|
|
r= -1;
|
2001-05-20 14:04:46 +02:00
|
|
|
}
|
|
|
|
DBUG_PRINT("exit", ("%d", r));
|
|
|
|
DBUG_RETURN(r);
|
|
|
|
}
|
|
|
|
|
2001-05-31 16:18:25 +02:00
|
|
|
int vio_keepalive(Vio* vio, my_bool set_keep_alive)
|
2001-05-20 14:04:46 +02:00
|
|
|
{
|
|
|
|
int r=0;
|
|
|
|
uint opt = 0;
|
|
|
|
DBUG_ENTER("vio_keepalive");
|
|
|
|
DBUG_PRINT("enter", ("sd=%d, set_keep_alive=%d", vio->sd, (int)
|
|
|
|
set_keep_alive));
|
|
|
|
if (vio->type != VIO_TYPE_NAMEDPIPE)
|
|
|
|
{
|
|
|
|
if (set_keep_alive)
|
|
|
|
opt = 1;
|
|
|
|
r = setsockopt(vio->sd, SOL_SOCKET, SO_KEEPALIVE, (char *) &opt,
|
|
|
|
sizeof(opt));
|
|
|
|
}
|
|
|
|
DBUG_RETURN(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
my_bool
|
2001-05-31 16:18:25 +02:00
|
|
|
vio_should_retry(Vio * vio __attribute__((unused)))
|
2001-05-20 14:04:46 +02:00
|
|
|
{
|
2001-10-08 22:20:19 +02:00
|
|
|
int en = socket_errno;
|
2001-10-09 02:35:29 +02:00
|
|
|
return (en == SOCKET_EAGAIN || en == SOCKET_EINTR ||
|
|
|
|
en == SOCKET_EWOULDBLOCK);
|
2001-05-20 14:04:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-08-30 17:19:28 +02:00
|
|
|
my_bool
|
|
|
|
vio_was_interrupted(Vio * vio __attribute__((unused)))
|
|
|
|
{
|
|
|
|
int en = socket_errno;
|
|
|
|
return (en == SOCKET_EAGAIN || en == SOCKET_EINTR ||
|
|
|
|
en == SOCKET_EWOULDBLOCK || en == SOCKET_ETIMEDOUT);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-05-31 16:18:25 +02:00
|
|
|
int vio_close(Vio * vio)
|
2001-05-20 14:04:46 +02:00
|
|
|
{
|
2003-10-17 13:17:15 +02:00
|
|
|
int r=0;
|
2001-05-20 14:04:46 +02:00
|
|
|
DBUG_ENTER("vio_close");
|
|
|
|
#ifdef __WIN__
|
|
|
|
if (vio->type == VIO_TYPE_NAMEDPIPE)
|
|
|
|
{
|
|
|
|
#if defined(__NT__) && defined(MYSQL_SERVER)
|
|
|
|
CancelIo(vio->hPipe);
|
|
|
|
DisconnectNamedPipe(vio->hPipe);
|
|
|
|
#endif
|
|
|
|
r=CloseHandle(vio->hPipe);
|
|
|
|
}
|
2004-07-12 06:38:46 +02:00
|
|
|
else
|
2001-05-20 14:04:46 +02:00
|
|
|
#endif /* __WIN__ */
|
2004-07-12 06:38:46 +02:00
|
|
|
if (vio->type != VIO_CLOSED)
|
2001-05-20 14:04:46 +02:00
|
|
|
{
|
2004-07-12 06:38:46 +02:00
|
|
|
DBUG_ASSERT(vio->sd >= 0);
|
2001-05-20 14:04:46 +02:00
|
|
|
if (shutdown(vio->sd,2))
|
|
|
|
r= -1;
|
|
|
|
if (closesocket(vio->sd))
|
|
|
|
r= -1;
|
|
|
|
}
|
|
|
|
if (r)
|
|
|
|
{
|
2001-10-08 22:20:19 +02:00
|
|
|
DBUG_PRINT("vio_error", ("close() failed, error: %d",socket_errno));
|
2001-05-20 14:04:46 +02:00
|
|
|
/* FIXME: error handling (not critical for MySQL) */
|
|
|
|
}
|
|
|
|
vio->type= VIO_CLOSED;
|
|
|
|
vio->sd= -1;
|
|
|
|
DBUG_RETURN(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-05-31 16:18:25 +02:00
|
|
|
const char *vio_description(Vio * vio)
|
2001-05-20 14:04:46 +02:00
|
|
|
{
|
|
|
|
return vio->desc;
|
|
|
|
}
|
|
|
|
|
2001-05-31 16:18:25 +02:00
|
|
|
enum enum_vio_type vio_type(Vio* vio)
|
2001-05-20 14:04:46 +02:00
|
|
|
{
|
|
|
|
return vio->type;
|
|
|
|
}
|
|
|
|
|
2001-05-31 16:18:25 +02:00
|
|
|
my_socket vio_fd(Vio* vio)
|
2001-05-20 14:04:46 +02:00
|
|
|
{
|
|
|
|
return vio->sd;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-02-27 19:31:54 +01:00
|
|
|
my_bool vio_peer_addr(Vio * vio, char *buf, uint16 *port)
|
2001-05-20 14:04:46 +02:00
|
|
|
{
|
|
|
|
DBUG_ENTER("vio_peer_addr");
|
2002-06-11 10:20:31 +02:00
|
|
|
DBUG_PRINT("enter", ("sd: %d", vio->sd));
|
2001-05-20 14:04:46 +02:00
|
|
|
if (vio->localhost)
|
|
|
|
{
|
|
|
|
strmov(buf,"127.0.0.1");
|
2003-02-17 21:07:26 +01:00
|
|
|
*port= 0;
|
2001-05-20 14:04:46 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-12-01 12:13:16 +01:00
|
|
|
size_socket addrLen = sizeof(vio->remote);
|
|
|
|
if (getpeername(vio->sd, (struct sockaddr *) (&vio->remote),
|
2001-05-20 14:04:46 +02:00
|
|
|
&addrLen) != 0)
|
|
|
|
{
|
2002-06-11 10:20:31 +02:00
|
|
|
DBUG_PRINT("exit", ("getpeername gave error: %d", socket_errno));
|
2001-05-20 14:04:46 +02:00
|
|
|
DBUG_RETURN(1);
|
|
|
|
}
|
2001-06-05 02:38:10 +02:00
|
|
|
my_inet_ntoa(vio->remote.sin_addr,buf);
|
2003-02-17 21:07:26 +01:00
|
|
|
*port= ntohs(vio->remote.sin_port);
|
2001-05-20 14:04:46 +02:00
|
|
|
}
|
2002-06-11 10:20:31 +02:00
|
|
|
DBUG_PRINT("exit", ("addr: %s", buf));
|
2001-05-20 14:04:46 +02:00
|
|
|
DBUG_RETURN(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-15 13:42:13 +01:00
|
|
|
/*
|
|
|
|
Get in_addr for a TCP/IP connection
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
vio_in_addr()
|
|
|
|
vio vio handle
|
|
|
|
in put in_addr here
|
|
|
|
|
|
|
|
NOTES
|
|
|
|
one must call vio_peer_addr() before calling this one
|
|
|
|
*/
|
|
|
|
|
2001-05-31 16:18:25 +02:00
|
|
|
void vio_in_addr(Vio *vio, struct in_addr *in)
|
2001-05-20 14:04:46 +02:00
|
|
|
{
|
|
|
|
DBUG_ENTER("vio_in_addr");
|
|
|
|
if (vio->localhost)
|
2005-01-25 21:06:55 +01:00
|
|
|
bzero((char*) in, sizeof(*in));
|
2001-05-20 14:04:46 +02:00
|
|
|
else
|
|
|
|
*in=vio->remote.sin_addr;
|
|
|
|
DBUG_VOID_RETURN;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Return 0 if there is data to be read */
|
|
|
|
|
2001-05-31 16:18:25 +02:00
|
|
|
my_bool vio_poll_read(Vio *vio,uint timeout)
|
2001-05-20 14:04:46 +02:00
|
|
|
{
|
|
|
|
#ifndef HAVE_POLL
|
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
struct pollfd fds;
|
|
|
|
int res;
|
|
|
|
DBUG_ENTER("vio_poll");
|
|
|
|
fds.fd=vio->sd;
|
|
|
|
fds.events=POLLIN;
|
|
|
|
fds.revents=0;
|
|
|
|
if ((res=poll(&fds,1,(int) timeout*1000)) <= 0)
|
|
|
|
{
|
|
|
|
DBUG_RETURN(res < 0 ? 0 : 1); /* Don't return 1 on errors */
|
|
|
|
}
|
|
|
|
DBUG_RETURN(fds.revents & POLLIN ? 0 : 1);
|
|
|
|
#endif
|
|
|
|
}
|
2002-11-14 20:16:30 +01:00
|
|
|
|
2003-08-27 01:51:39 +02:00
|
|
|
|
|
|
|
void vio_timeout(Vio *vio __attribute__((unused)),
|
2005-05-05 17:17:57 +02:00
|
|
|
uint which __attribute__((unused)),
|
|
|
|
uint timeout __attribute__((unused)))
|
2003-08-27 01:51:39 +02:00
|
|
|
{
|
|
|
|
#ifdef __WIN__
|
|
|
|
ulong wait_timeout= (ulong) timeout * 1000;
|
2005-05-06 15:08:10 +02:00
|
|
|
(void) setsockopt(vio->sd, SOL_SOCKET,
|
|
|
|
which ? SO_SNDTIMEO : SO_RCVTIMEO, (char*) &wait_timeout,
|
|
|
|
sizeof(wait_timeout));
|
2003-08-27 01:51:39 +02:00
|
|
|
#endif /* __WIN__ */
|
|
|
|
}
|
2003-08-29 12:44:35 +02:00
|
|
|
|
|
|
|
|
2002-11-14 20:16:30 +01:00
|
|
|
#ifdef __WIN__
|
|
|
|
int vio_read_pipe(Vio * vio, gptr buf, int size)
|
|
|
|
{
|
|
|
|
DWORD length;
|
|
|
|
DBUG_ENTER("vio_read_pipe");
|
|
|
|
DBUG_PRINT("enter", ("sd=%d, buf=%p, size=%d", vio->sd, buf, size));
|
|
|
|
|
|
|
|
if (!ReadFile(vio->hPipe, buf, size, &length, NULL))
|
|
|
|
DBUG_RETURN(-1);
|
|
|
|
|
|
|
|
DBUG_PRINT("exit", ("%d", length));
|
|
|
|
DBUG_RETURN(length);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int vio_write_pipe(Vio * vio, const gptr buf, int size)
|
|
|
|
{
|
|
|
|
DWORD length;
|
|
|
|
DBUG_ENTER("vio_write_pipe");
|
|
|
|
DBUG_PRINT("enter", ("sd=%d, buf=%p, size=%d", vio->sd, buf, size));
|
|
|
|
|
|
|
|
if (!WriteFile(vio->hPipe, (char*) buf, size, &length, NULL))
|
|
|
|
DBUG_RETURN(-1);
|
|
|
|
|
|
|
|
DBUG_PRINT("exit", ("%d", length));
|
|
|
|
DBUG_RETURN(length);
|
|
|
|
}
|
|
|
|
|
|
|
|
int vio_close_pipe(Vio * vio)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
DBUG_ENTER("vio_close_pipe");
|
|
|
|
#if defined(__NT__) && defined(MYSQL_SERVER)
|
|
|
|
CancelIo(vio->hPipe);
|
|
|
|
DisconnectNamedPipe(vio->hPipe);
|
|
|
|
#endif
|
|
|
|
r=CloseHandle(vio->hPipe);
|
|
|
|
if (r)
|
|
|
|
{
|
|
|
|
DBUG_PRINT("vio_error", ("close() failed, error: %d",GetLastError()));
|
|
|
|
/* FIXME: error handling (not critical for MySQL) */
|
|
|
|
}
|
|
|
|
vio->type= VIO_CLOSED;
|
|
|
|
vio->sd= -1;
|
|
|
|
DBUG_RETURN(r);
|
|
|
|
}
|
|
|
|
|
2003-12-15 16:58:15 +01:00
|
|
|
|
|
|
|
void vio_ignore_timeout(Vio *vio __attribute__((unused)),
|
2005-06-08 01:25:06 +02:00
|
|
|
uint which __attribute__((unused)),
|
2003-12-15 16:58:15 +01:00
|
|
|
uint timeout __attribute__((unused)))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-11-14 20:16:30 +01:00
|
|
|
#ifdef HAVE_SMEM
|
|
|
|
|
|
|
|
int vio_read_shared_memory(Vio * vio, gptr buf, int size)
|
|
|
|
{
|
|
|
|
int length;
|
|
|
|
int remain_local;
|
|
|
|
char *current_postion;
|
|
|
|
|
|
|
|
DBUG_ENTER("vio_read_shared_memory");
|
|
|
|
DBUG_PRINT("enter", ("sd=%d, buf=%p, size=%d", vio->sd, buf, size));
|
|
|
|
|
|
|
|
remain_local = size;
|
|
|
|
current_postion=buf;
|
2003-10-17 13:17:15 +02:00
|
|
|
do
|
2002-11-14 20:16:30 +01:00
|
|
|
{
|
2003-10-17 13:17:15 +02:00
|
|
|
if (vio->shared_memory_remain == 0)
|
2002-11-14 20:16:30 +01:00
|
|
|
{
|
2004-12-14 15:24:19 +01:00
|
|
|
HANDLE events[2];
|
|
|
|
events[0]= vio->event_server_wrote;
|
|
|
|
events[1]= vio->event_conn_closed;
|
|
|
|
/*
|
|
|
|
WaitForMultipleObjects can return next values:
|
|
|
|
WAIT_OBJECT_0+0 - event from vio->event_server_wrote
|
|
|
|
WAIT_OBJECT_0+1 - event from vio->event_conn_closed. We can't read anything
|
|
|
|
WAIT_ABANDONED_0 and WAIT_TIMEOUT - fail. We can't read anything
|
|
|
|
*/
|
2004-12-23 12:04:40 +01:00
|
|
|
if (WaitForMultipleObjects(2, (HANDLE*)&events,FALSE,
|
2004-12-14 15:24:19 +01:00
|
|
|
vio->net->read_timeout*1000) != WAIT_OBJECT_0)
|
2002-11-14 20:16:30 +01:00
|
|
|
{
|
|
|
|
DBUG_RETURN(-1);
|
|
|
|
};
|
2004-12-14 15:24:19 +01:00
|
|
|
|
2002-11-14 20:16:30 +01:00
|
|
|
vio->shared_memory_pos = vio->handle_map;
|
|
|
|
vio->shared_memory_remain = uint4korr((ulong*)vio->shared_memory_pos);
|
|
|
|
vio->shared_memory_pos+=4;
|
|
|
|
}
|
|
|
|
|
|
|
|
length = size;
|
|
|
|
|
2003-10-17 13:17:15 +02:00
|
|
|
if (vio->shared_memory_remain < length)
|
2002-11-14 20:16:30 +01:00
|
|
|
length = vio->shared_memory_remain;
|
2003-10-17 13:17:15 +02:00
|
|
|
if (length > remain_local)
|
2002-11-14 20:16:30 +01:00
|
|
|
length = remain_local;
|
|
|
|
|
|
|
|
memcpy(current_postion,vio->shared_memory_pos,length);
|
|
|
|
|
|
|
|
vio->shared_memory_remain-=length;
|
|
|
|
vio->shared_memory_pos+=length;
|
|
|
|
current_postion+=length;
|
|
|
|
remain_local-=length;
|
|
|
|
|
2003-10-17 13:17:15 +02:00
|
|
|
if (!vio->shared_memory_remain)
|
2002-11-14 20:16:30 +01:00
|
|
|
if (!SetEvent(vio->event_client_read)) DBUG_RETURN(-1);
|
|
|
|
} while (remain_local);
|
|
|
|
length = size;
|
|
|
|
|
|
|
|
DBUG_PRINT("exit", ("%d", length));
|
|
|
|
DBUG_RETURN(length);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int vio_write_shared_memory(Vio * vio, const gptr buf, int size)
|
|
|
|
{
|
|
|
|
int length;
|
|
|
|
uint remain;
|
|
|
|
HANDLE pos;
|
|
|
|
int sz;
|
|
|
|
char *current_postion;
|
|
|
|
|
|
|
|
DBUG_ENTER("vio_write_shared_memory");
|
|
|
|
DBUG_PRINT("enter", ("sd=%d, buf=%p, size=%d", vio->sd, buf, size));
|
|
|
|
|
|
|
|
remain = size;
|
|
|
|
current_postion = buf;
|
2003-10-17 13:17:15 +02:00
|
|
|
while (remain != 0)
|
2002-11-14 20:16:30 +01:00
|
|
|
{
|
2004-12-23 12:04:40 +01:00
|
|
|
if (WaitForSingleObject(vio->event_server_read, vio->net->write_timeout*1000)
|
|
|
|
!= WAIT_OBJECT_0)
|
2002-11-14 20:16:30 +01:00
|
|
|
{
|
2003-10-17 13:17:15 +02:00
|
|
|
DBUG_RETURN(-1);
|
2002-11-14 20:16:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
sz = remain > shared_memory_buffer_length ? shared_memory_buffer_length: remain;
|
|
|
|
|
|
|
|
int4store(vio->handle_map,sz);
|
|
|
|
pos = vio->handle_map + 4;
|
|
|
|
memcpy(pos,current_postion,sz);
|
|
|
|
remain-=sz;
|
|
|
|
current_postion+=sz;
|
|
|
|
if (!SetEvent(vio->event_client_wrote)) DBUG_RETURN(-1);
|
|
|
|
}
|
|
|
|
length = size;
|
|
|
|
|
|
|
|
DBUG_PRINT("exit", ("%d", length));
|
|
|
|
DBUG_RETURN(length);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int vio_close_shared_memory(Vio * vio)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
DBUG_ENTER("vio_close_shared_memory");
|
2004-12-14 15:24:19 +01:00
|
|
|
if (vio->type != VIO_CLOSED)
|
2002-11-14 20:16:30 +01:00
|
|
|
{
|
2004-12-23 12:04:40 +01:00
|
|
|
/*
|
|
|
|
Set event_conn_closed for notification of both client and server that
|
|
|
|
connection is closed
|
|
|
|
*/
|
2004-12-14 15:24:19 +01:00
|
|
|
SetEvent(vio->event_conn_closed);
|
2004-12-23 12:04:40 +01:00
|
|
|
/*
|
|
|
|
Close all handlers. UnmapViewOfFile and CloseHandle return non-zero
|
|
|
|
result if they are success.
|
|
|
|
*/
|
|
|
|
r= UnmapViewOfFile(vio->handle_map) || CloseHandle(vio->event_server_wrote) ||
|
|
|
|
CloseHandle(vio->event_server_read) || CloseHandle(vio->event_client_wrote) ||
|
|
|
|
CloseHandle(vio->event_client_read) || CloseHandle(vio->handle_file_map);
|
2004-12-14 15:24:19 +01:00
|
|
|
if (!r)
|
|
|
|
{
|
|
|
|
DBUG_PRINT("vio_error", ("close() failed, error: %d",r));
|
|
|
|
/* FIXME: error handling (not critical for MySQL) */
|
|
|
|
}
|
2002-11-14 20:16:30 +01:00
|
|
|
}
|
|
|
|
vio->type= VIO_CLOSED;
|
|
|
|
vio->sd= -1;
|
2004-12-14 15:24:19 +01:00
|
|
|
DBUG_RETURN(!r);
|
2002-11-14 20:16:30 +01:00
|
|
|
}
|
2003-08-29 12:44:35 +02:00
|
|
|
#endif /* HAVE_SMEM */
|
|
|
|
#endif /* __WIN__ */
|