1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
/* Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
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; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */
#include "vio_priv.h"
#if defined(_WIN32) && defined(HAVE_SMEM)
size_t vio_read_shared_memory(Vio *vio, uchar *buf, size_t size)
{
size_t length;
size_t remain_local;
char *current_position;
HANDLE events[2];
DWORD timeout;
DBUG_ENTER("vio_read_shared_memory");
remain_local= size;
current_position= buf;
timeout= vio->read_timeout >= 0 ? vio->read_timeout : INFINITE;
events[0]= vio->event_server_wrote;
events[1]= vio->event_conn_closed;
do
{
if (vio->shared_memory_remain == 0)
{
DWORD wait_status;
wait_status= WaitForMultipleObjects(array_elements(events), events,
FALSE, timeout);
/*
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
*/
if (wait_status != WAIT_OBJECT_0)
{
/*
If wait_status is WAIT_TIMEOUT, set error code to indicate a
timeout error. If vio->event_conn_closed was set, use an EOF
condition (return value of zero) to indicate that the operation
has been aborted.
*/
if (wait_status == WAIT_TIMEOUT)
SetLastError(SOCKET_ETIMEDOUT);
else if (wait_status == (WAIT_OBJECT_0 + 1))
DBUG_RETURN(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_position, vio->shared_memory_pos, length);
vio->shared_memory_remain-= length;
vio->shared_memory_pos+= length;
current_position+= length;
remain_local-= length;
if (!vio->shared_memory_remain)
{
if (!SetEvent(vio->event_client_read))
DBUG_RETURN(-1);
}
} while (remain_local);
length= size;
DBUG_RETURN(length);
}
size_t vio_write_shared_memory(Vio *vio, const uchar *buf, size_t size)
{
size_t length, remain, sz;
HANDLE pos;
const uchar *current_position;
HANDLE events[2];
DWORD timeout;
DBUG_ENTER("vio_write_shared_memory");
remain= size;
current_position= buf;
timeout= vio->write_timeout >= 0 ? vio->write_timeout : INFINITE;
events[0]= vio->event_server_read;
events[1]= vio->event_conn_closed;
while (remain != 0)
{
DWORD wait_status;
wait_status= WaitForMultipleObjects(array_elements(events), events,
FALSE, timeout);
if (wait_status != WAIT_OBJECT_0)
{
/* Set error code to indicate a timeout error or disconnect. */
if (wait_status == WAIT_TIMEOUT)
SetLastError(SOCKET_ETIMEDOUT);
else
SetLastError(ERROR_GRACEFUL_DISCONNECT);
DBUG_RETURN((size_t) -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_position, sz);
remain-= sz;
current_position+= sz;
if (!SetEvent(vio->event_client_wrote))
DBUG_RETURN((size_t) -1);
}
length= size;
DBUG_RETURN(length);
}
my_bool vio_is_connected_shared_memory(Vio *vio)
{
return (WaitForSingleObject(vio->event_conn_closed, 0) != WAIT_OBJECT_0);
}
/**
Close shared memory and DBUG_PRINT any errors that happen on closing.
@return Zero if all closing functions succeed, and nonzero otherwise.
*/
int vio_close_shared_memory(Vio * vio)
{
int error_count= 0;
DBUG_ENTER("vio_close_shared_memory");
if (vio->type != VIO_CLOSED)
{
/*
Set event_conn_closed for notification of both client and server that
connection is closed
*/
SetEvent(vio->event_conn_closed);
/*
Close all handlers. UnmapViewOfFile and CloseHandle return non-zero
result if they are success.
*/
if (UnmapViewOfFile(vio->handle_map) == 0)
{
error_count++;
DBUG_PRINT("vio_error", ("UnmapViewOfFile() failed"));
}
if (CloseHandle(vio->event_server_wrote) == 0)
{
error_count++;
DBUG_PRINT("vio_error", ("CloseHandle(vio->esw) failed"));
}
if (CloseHandle(vio->event_server_read) == 0)
{
error_count++;
DBUG_PRINT("vio_error", ("CloseHandle(vio->esr) failed"));
}
if (CloseHandle(vio->event_client_wrote) == 0)
{
error_count++;
DBUG_PRINT("vio_error", ("CloseHandle(vio->ecw) failed"));
}
if (CloseHandle(vio->event_client_read) == 0)
{
error_count++;
DBUG_PRINT("vio_error", ("CloseHandle(vio->ecr) failed"));
}
if (CloseHandle(vio->handle_file_map) == 0)
{
error_count++;
DBUG_PRINT("vio_error", ("CloseHandle(vio->hfm) failed"));
}
if (CloseHandle(vio->event_conn_closed) == 0)
{
error_count++;
DBUG_PRINT("vio_error", ("CloseHandle(vio->ecc) failed"));
}
}
vio->type= VIO_CLOSED;
vio->mysql_socket= MYSQL_INVALID_SOCKET;
DBUG_RETURN(error_count);
}
#endif /* #if defined(_WIN32) && defined(HAVE_SMEM) */
|