@@ -98,60 +98,8 @@ socketio_acceptEx(struct w32_io* pio) {
98
98
struct acceptEx_context * context ;
99
99
100
100
debug3 ("acceptEx - io:%p" , pio );
101
- if (pio -> internal .context == NULL ) {
102
- GUID GuidAcceptEx = WSAID_ACCEPTEX ;
103
- GUID GuidGetAcceptExSockaddrs = WSAID_GETACCEPTEXSOCKADDRS ;
104
- DWORD dwBytes ;
105
-
106
- context =
107
- (struct acceptEx_context * )malloc (sizeof (struct acceptEx_context ));
108
- if (context == NULL ) {
109
- errno = ENOMEM ;
110
- debug ("acceptEx - ERROR:%d, io:%p" , errno , pio );
111
- return -1 ;
112
- }
113
- memset (context , 0 , sizeof (struct acceptEx_context ));
114
- if (SOCKET_ERROR == WSAIoctl (pio -> sock ,
115
- SIO_GET_EXTENSION_FUNCTION_POINTER ,
116
- & GuidAcceptEx , sizeof (GuidAcceptEx ),
117
- & context -> lpfnAcceptEx , sizeof (context -> lpfnAcceptEx ),
118
- & dwBytes , NULL , NULL ))
119
- {
120
- free (context );
121
- errno = errno_from_WSALastError ();
122
- debug ("acceptEx - Ioctl1 ERROR:%d, io:%p" , errno , pio );
123
- return -1 ;
124
- }
125
-
126
- if (SOCKET_ERROR == WSAIoctl (pio -> sock ,
127
- SIO_GET_EXTENSION_FUNCTION_POINTER ,
128
- & GuidGetAcceptExSockaddrs , sizeof (GuidGetAcceptExSockaddrs ),
129
- & context -> lpfnGuidGetAcceptExSockaddrs , sizeof (context -> lpfnGuidGetAcceptExSockaddrs ),
130
- & dwBytes , NULL , NULL ))
131
- {
132
- free (context );
133
- errno = errno_from_WSALastError ();
134
- debug ("acceptEx - Ioctl2 ERROR:%d, io:%p" , errno , pio );
135
- return -1 ;
136
- }
137
-
101
+ context = (struct acceptEx_context * )pio -> internal .context ;
138
102
139
-
140
- context -> accept_socket = INVALID_SOCKET ;
141
- pio -> internal .context = context ;
142
- }
143
- else
144
- context = (struct acceptEx_context * )pio -> internal .context ;
145
-
146
- /* init overlapped event */
147
- if (pio -> read_overlapped .hEvent == NULL ) {
148
- pio -> read_overlapped .hEvent = CreateEvent (NULL , TRUE, FALSE, NULL );
149
- if ((pio -> read_overlapped .hEvent ) == NULL ) {
150
- errno = ENOMEM ;
151
- debug ("acceptEx - CreateEvent() ERROR:%d, io:%p" , errno , pio );
152
- return -1 ;
153
- }
154
- }
155
103
ResetEvent (pio -> read_overlapped .hEvent );
156
104
157
105
/* create accepting socket */
@@ -257,10 +205,9 @@ socketio_WSARecv(struct w32_io* pio, BOOL* completed) {
257
205
pio -> read_details .pending = TRUE;
258
206
}
259
207
else {
260
- /* io has completed due to error, recv() will pick it up */
261
- debug ("WSARecv - WSARecv() ERROR:%d io:%p" , WSAGetLastError (), pio );
262
- pio -> read_details .error = WSAGetLastError ();
263
- return 0 ;
208
+ errno = errno_from_WSALastError ();
209
+ debug ("WSARecv - WSARecv() ERROR: io:%p %d" , pio , errno );
210
+ return -1 ;
264
211
}
265
212
}
266
213
@@ -334,11 +281,63 @@ socketio_getpeername(struct w32_io* pio, struct sockaddr* name, int* namelen) {
334
281
/* implements listen() */
335
282
int
336
283
socketio_listen (struct w32_io * pio , int backlog ) {
284
+ struct acceptEx_context * context ;
285
+
337
286
if (SOCKET_ERROR == listen (pio -> sock , backlog )) {
338
287
errno = errno_from_WSALastError ();
339
288
debug ("listen - listen() ERROR:%d io:%p" , errno , pio );
340
289
return -1 ;
341
290
}
291
+
292
+ /* prep for accept*/
293
+ {
294
+ GUID GuidAcceptEx = WSAID_ACCEPTEX ;
295
+ GUID GuidGetAcceptExSockaddrs = WSAID_GETACCEPTEXSOCKADDRS ;
296
+ DWORD dwBytes ;
297
+
298
+ context = (struct acceptEx_context * )malloc (sizeof (struct acceptEx_context ));
299
+ if (context == NULL ) {
300
+ errno = ENOMEM ;
301
+ debug ("listen - ERROR:%d, io:%p" , errno , pio );
302
+ return -1 ;
303
+ }
304
+ memset (context , 0 , sizeof (struct acceptEx_context ));
305
+ if (SOCKET_ERROR == WSAIoctl (pio -> sock ,
306
+ SIO_GET_EXTENSION_FUNCTION_POINTER ,
307
+ & GuidAcceptEx , sizeof (GuidAcceptEx ),
308
+ & context -> lpfnAcceptEx , sizeof (context -> lpfnAcceptEx ),
309
+ & dwBytes , NULL , NULL ))
310
+ {
311
+ free (context );
312
+ errno = errno_from_WSALastError ();
313
+ debug ("listen - Ioctl1 ERROR:%d, io:%p" , errno , pio );
314
+ return -1 ;
315
+ }
316
+
317
+ if (SOCKET_ERROR == WSAIoctl (pio -> sock ,
318
+ SIO_GET_EXTENSION_FUNCTION_POINTER ,
319
+ & GuidGetAcceptExSockaddrs , sizeof (GuidGetAcceptExSockaddrs ),
320
+ & context -> lpfnGuidGetAcceptExSockaddrs , sizeof (context -> lpfnGuidGetAcceptExSockaddrs ),
321
+ & dwBytes , NULL , NULL ))
322
+ {
323
+ free (context );
324
+ errno = errno_from_WSALastError ();
325
+ debug ("listen - Ioctl2 ERROR:%d, io:%p" , errno , pio );
326
+ return -1 ;
327
+ }
328
+
329
+ pio -> read_overlapped .hEvent = CreateEvent (NULL , TRUE, FALSE, NULL );
330
+ if ((pio -> read_overlapped .hEvent ) == NULL ) {
331
+ free (context );
332
+ errno = ENOMEM ;
333
+ debug ("listen - CreateEvent() ERROR:%d, io:%p" , errno , pio );
334
+ return -1 ;
335
+ }
336
+
337
+ context -> accept_socket = INVALID_SOCKET ;
338
+ pio -> internal .context = context ;
339
+ }
340
+
342
341
pio -> internal .state = SOCK_LISTENING ;
343
342
return 0 ;
344
343
}
@@ -694,13 +693,9 @@ socketio_accept(struct w32_io* pio, struct sockaddr* addr, int* addrlen) {
694
693
if (w32_io_is_blocking (pio )) {
695
694
/* block until accept io is complete */
696
695
while (FALSE == socketio_is_io_available (pio , TRUE))
697
- {
698
- if (-1 == wait_for_any_event (& pio -> read_overlapped .hEvent ,
699
- 1 , INFINITE ))
700
- {
696
+ if (-1 == wait_for_any_event (& pio -> read_overlapped .hEvent ,
697
+ 1 , INFINITE ))
701
698
return NULL ;
702
- }
703
- }
704
699
}
705
700
else {
706
701
/* if i/o is not ready */
@@ -911,19 +906,29 @@ socketio_is_io_available(struct w32_io* pio, BOOL rd) {
911
906
BOOL pending =
912
907
sock_listening ? pio -> read_details .pending : pio -> write_details .pending ;
913
908
914
- if (pending && WSAGetOverlappedResult (pio -> sock , overlapped ,
915
- & numBytes , FALSE, & flags ))
916
- return TRUE;
917
- else {
918
- if (pending && WSAGetLastError () != WSA_IO_INCOMPLETE ) {
919
- if (sock_listening )
920
- pio -> read_details .error = WSAGetLastError ();
921
- else
922
- pio -> write_details .error = WSAGetLastError ();
923
- return TRUE;
909
+ if (pending )
910
+ /* if there is an error to be picked up */
911
+ if (sock_listening ) {
912
+ if (pio -> read_details .error )
913
+ return TRUE;
924
914
}
925
- return FALSE;
926
- }
915
+ else {
916
+ if (pio -> write_details .error )
917
+ return TRUE;
918
+ }
919
+
920
+ if (WSAGetOverlappedResult (pio -> sock , overlapped ,
921
+ & numBytes , FALSE, & flags ))
922
+ return TRUE;
923
+ else if (WSAGetLastError () != WSA_IO_INCOMPLETE ) {
924
+ if (sock_listening )
925
+ pio -> read_details .error = WSAGetLastError ();
926
+ else
927
+ pio -> write_details .error = WSAGetLastError ();
928
+ return TRUE;
929
+ }
930
+
931
+ return FALSE;
927
932
}
928
933
else if (rd ) {
929
934
if (pio -> read_details .remaining || pio -> read_details .error )
@@ -937,7 +942,7 @@ socketio_is_io_available(struct w32_io* pio, BOOL rd) {
937
942
938
943
}
939
944
/*start async io (if needed) for accept and recv*/
940
- int
945
+ void
941
946
socketio_on_select (struct w32_io * pio , BOOL rd ) {
942
947
943
948
enum w32_io_sock_state sock_state = pio -> internal .state ;
@@ -946,20 +951,29 @@ socketio_on_select(struct w32_io* pio, BOOL rd) {
946
951
947
952
//nothing to do for writes (that includes connect)
948
953
if (!rd )
949
- return 0 ;
954
+ return ;
950
955
951
956
//listening socket - acceptEx if needed
952
957
if (sock_state == SOCK_LISTENING ) {
953
- if ((!pio -> read_details .pending ) && (socketio_acceptEx (pio ) != 0 ))
954
- return -1 ;
958
+ if (pio -> read_details .pending == FALSE)
959
+ if (socketio_acceptEx (pio ) != 0 ) {
960
+ /* set error, accept will pick it*/
961
+ pio -> read_details .error = errno ;
962
+ errno = 0 ;
963
+ pio -> read_details .pending = TRUE;
964
+ SetEvent (pio -> read_overlapped .hEvent );
965
+ return ;
966
+ }
955
967
}
956
968
else {
957
969
//connected socket - WSARecv if needed
958
- if ((!pio -> read_details .pending )
959
- && (!socketio_is_io_available (pio , rd ))
960
- && (socketio_WSARecv (pio , NULL ) != 0 ))
961
- return -1 ;
970
+ if ((!pio -> read_details .pending ) && (!socketio_is_io_available (pio , rd )))
971
+ if (socketio_WSARecv (pio , NULL ) != 0 ) {
972
+ /* set error, recv() will pick it */
973
+ pio -> read_details .error = errno ;
974
+ errno = 0 ;
975
+ return ;
976
+ }
962
977
}
963
978
964
- return 0 ;
965
979
}
0 commit comments