diff options
Diffstat (limited to 'vio/viosocket.c')
-rw-r--r-- | vio/viosocket.c | 184 |
1 files changed, 155 insertions, 29 deletions
diff --git a/vio/viosocket.c b/vio/viosocket.c index 6f5c4142a45..ac2f7939a4c 100644 --- a/vio/viosocket.c +++ b/vio/viosocket.c @@ -35,18 +35,6 @@ #define HANDLE void * #endif -void vio_delete(Vio* vio) -{ - /* It must be safe to delete null pointers. */ - /* This matches the semantics of C++'s delete operator. */ - if (vio) - { - if (vio->type != VIO_CLOSED) - vio_close(vio); - my_free((gptr) vio,MYF(0)); - } -} - int vio_errno(Vio *vio __attribute__((unused))) { return socket_errno; /* On Win32 this mapped to WSAGetLastError() */ @@ -58,14 +46,8 @@ int vio_read(Vio * vio, gptr buf, int size) int r; DBUG_ENTER("vio_read"); DBUG_PRINT("enter", ("sd=%d, buf=%p, size=%d", vio->sd, buf, size)); + #ifdef __WIN__ - if (vio->type == VIO_TYPE_NAMEDPIPE) - { - DWORD length; - if (!ReadFile(vio->hPipe, buf, size, &length, NULL)) - DBUG_RETURN(-1); - DBUG_RETURN(length); - } r = recv(vio->sd, buf, size,0); #else errno=0; /* For linux */ @@ -87,15 +69,8 @@ int vio_write(Vio * vio, const gptr buf, int size) int r; DBUG_ENTER("vio_write"); DBUG_PRINT("enter", ("sd=%d, buf=%p, size=%d", vio->sd, buf, size)); -#if defined( __WIN__) - if ( vio->type == VIO_TYPE_NAMEDPIPE) - { - DWORD length; - if (!WriteFile(vio->hPipe, (char*) buf, size, &length, NULL)) - DBUG_RETURN(-1); - DBUG_RETURN(length); - } - r = send(vio->sd, buf, size, 0); +#ifdef __WIN__ + r = send(vio->sd, buf, size,0); #else r = write(vio->sd, buf, size); #endif /* __WIN__ */ @@ -109,7 +84,6 @@ int vio_write(Vio * vio, const gptr buf, int size) DBUG_RETURN(r); } - int vio_blocking(Vio * vio __attribute__((unused)), my_bool set_blocking_mode, my_bool *old_mode) { @@ -334,3 +308,155 @@ my_bool vio_poll_read(Vio *vio,uint timeout) DBUG_RETURN(fds.revents & POLLIN ? 0 : 1); #endif } + +#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); +} + +#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; + do + { + if (vio->shared_memory_remain == 0) + { + if (WaitForSingleObject(vio->event_server_wrote,vio->net->read_timeout*1000) != WAIT_OBJECT_0) + { + DBUG_RETURN(-1); + }; + vio->shared_memory_pos = vio->handle_map; + vio->shared_memory_remain = uint4korr((ulong*)vio->shared_memory_pos); + vio->shared_memory_pos+=4; + } + + length = size; + + if (vio->shared_memory_remain < length) + length = vio->shared_memory_remain; + if (length > remain_local) + 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; + + if (!vio->shared_memory_remain) + 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; + while (remain != 0) + { + if (WaitForSingleObject(vio->event_server_read,vio->net->write_timeout*1000) != WAIT_OBJECT_0) + { + DBUG_RETURN(-1); + }; + + 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"); + 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); + if (r) + { + DBUG_PRINT("vio_error", ("close() failed, error: %d",r)); + /* FIXME: error handling (not critical for MySQL) */ + } + vio->type= VIO_CLOSED; + vio->sd= -1; + DBUG_RETURN(r); +} +#endif +#endif |