| 1 | /* $Id: asyncapi.cpp,v 1.8 2000-05-18 22:54:20 sandervl Exp $ */ | 
|---|
| 2 |  | 
|---|
| 3 | /* | 
|---|
| 4 | * | 
|---|
| 5 | * Win32 SOCK32 for OS/2 (Async apis) | 
|---|
| 6 | * | 
|---|
| 7 | * Copyright (C) 2000 Sander van Leeuwen (sandervl@xs4all.nl) | 
|---|
| 8 | * | 
|---|
| 9 | * Parts based on Wine code: (dlls\winsock\async.c) | 
|---|
| 10 | * (C) 1993,1994,1996,1997 John Brezak, Erik Bos, Alex Korobka. | 
|---|
| 11 | * (C) 1999 Marcus Meissner | 
|---|
| 12 | * | 
|---|
| 13 | * Project Odin Software License can be found in LICENSE.TXT | 
|---|
| 14 | * | 
|---|
| 15 | */ | 
|---|
| 16 | #define INCL_BASE | 
|---|
| 17 | #include <os2wrap.h> | 
|---|
| 18 | #include <stdio.h> | 
|---|
| 19 | #include <stdlib.h> | 
|---|
| 20 | #include <string.h> | 
|---|
| 21 | #include <odin.h> | 
|---|
| 22 | #include <odinwrap.h> | 
|---|
| 23 | #include <os2sel.h> | 
|---|
| 24 | #include <misc.h> | 
|---|
| 25 | #include <win32api.h> | 
|---|
| 26 | #include "wsock32.h" | 
|---|
| 27 | #include "asyncthread.h" | 
|---|
| 28 | #include "wsastruct.h" | 
|---|
| 29 |  | 
|---|
| 30 | #define DBG_LOCALLOG    DBG_async | 
|---|
| 31 | #include "dbglocal.h" | 
|---|
| 32 |  | 
|---|
| 33 | //kso: dirty fix to make this compile! not permanent! | 
|---|
| 34 | BOOL WINAPI QueryPerformanceCounter(LARGE_INTEGER *p); | 
|---|
| 35 | #define LowPart u.LowPart | 
|---|
| 36 |  | 
|---|
| 37 |  | 
|---|
| 38 | ODINDEBUGCHANNEL(WSOCK32-ASYNC) | 
|---|
| 39 |  | 
|---|
| 40 | //****************************************************************************** | 
|---|
| 41 | //NOTE: Must execute RemoveFromQueue before returning | 
|---|
| 42 | //****************************************************************************** | 
|---|
| 43 | void ASYNCCNV WSAsyncThreadProc(void *pparm) | 
|---|
| 44 | { | 
|---|
| 45 | PASYNCTHREADPARM pThreadParm = (PASYNCTHREADPARM)pparm; | 
|---|
| 46 | LPARAM           lParam; | 
|---|
| 47 | int              size = 0; | 
|---|
| 48 | int              fail = NO_ERROR; | 
|---|
| 49 |  | 
|---|
| 50 | if(!pThreadParm->fCancelled) | 
|---|
| 51 | { | 
|---|
| 52 | switch(pThreadParm->request) | 
|---|
| 53 | { | 
|---|
| 54 | case ASYNC_GETHOSTBYNAME: | 
|---|
| 55 | case ASYNC_GETHOSTBYADDR: | 
|---|
| 56 | { | 
|---|
| 57 | struct hostent  *ret; | 
|---|
| 58 |  | 
|---|
| 59 | if(pThreadParm->request == ASYNC_GETHOSTBYNAME) { | 
|---|
| 60 | ret = gethostbyname(pThreadParm->u.gethostbyname.name); | 
|---|
| 61 |  | 
|---|
| 62 | free(pThreadParm->u.gethostbyname.name); | 
|---|
| 63 | pThreadParm->u.gethostbyname.name = 0; | 
|---|
| 64 | } | 
|---|
| 65 | else { | 
|---|
| 66 | ret = gethostbyaddr(pThreadParm->u.gethostbyaddr.addr, | 
|---|
| 67 | pThreadParm->u.gethostbyaddr.len, | 
|---|
| 68 | pThreadParm->u.gethostbyaddr.type); | 
|---|
| 69 |  | 
|---|
| 70 | free(pThreadParm->u.gethostbyaddr.addr); | 
|---|
| 71 | pThreadParm->u.gethostbyaddr.addr = 0; | 
|---|
| 72 | } | 
|---|
| 73 | if (ret != NULL) { | 
|---|
| 74 | size = WS_copy_he((struct ws_hostent *)pThreadParm->buf, | 
|---|
| 75 | pThreadParm->buf, | 
|---|
| 76 | pThreadParm->buflen, | 
|---|
| 77 | ret); | 
|---|
| 78 | if(size < 0) { | 
|---|
| 79 | fail = WSAENOBUFS; | 
|---|
| 80 | size = -size; | 
|---|
| 81 | } | 
|---|
| 82 | } | 
|---|
| 83 | else    fail = wsaHerrno(); | 
|---|
| 84 | break; | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | case ASYNC_GETPROTOBYNAME: | 
|---|
| 88 | case ASYNC_GETPROTOBYNUMBER: | 
|---|
| 89 | { | 
|---|
| 90 | struct protoent *ret; | 
|---|
| 91 |  | 
|---|
| 92 | if(pThreadParm->request == ASYNC_GETPROTOBYNAME) { | 
|---|
| 93 | ret = getprotobyname(pThreadParm->u.getprotobyname.name); | 
|---|
| 94 |  | 
|---|
| 95 | free(pThreadParm->u.getprotobyname.name); | 
|---|
| 96 | pThreadParm->u.getprotobyname.name = 0; | 
|---|
| 97 | } | 
|---|
| 98 | else    ret = getprotobynumber(pThreadParm->u.getprotobynumber.number); | 
|---|
| 99 |  | 
|---|
| 100 | if (ret != NULL) { | 
|---|
| 101 | size = WS_copy_pe((struct ws_protoent *)pThreadParm->buf, | 
|---|
| 102 | pThreadParm->buf, | 
|---|
| 103 | pThreadParm->buflen, | 
|---|
| 104 | ret); | 
|---|
| 105 | if(size < 0) { | 
|---|
| 106 | fail = WSAENOBUFS; | 
|---|
| 107 | size = -size; | 
|---|
| 108 | } | 
|---|
| 109 | } | 
|---|
| 110 | else    fail = WSANO_DATA; | 
|---|
| 111 |  | 
|---|
| 112 | break; | 
|---|
| 113 | } | 
|---|
| 114 |  | 
|---|
| 115 | case ASYNC_GETSERVBYNAME: | 
|---|
| 116 | case ASYNC_GETSERVBYPORT: | 
|---|
| 117 | { | 
|---|
| 118 | struct servent  *ret; | 
|---|
| 119 |  | 
|---|
| 120 | if(pThreadParm->request == ASYNC_GETSERVBYNAME) { | 
|---|
| 121 | ret = getservbyname(pThreadParm->u.getservbyname.name, pThreadParm->u.getservbyname.proto); | 
|---|
| 122 |  | 
|---|
| 123 | free(pThreadParm->u.getservbyname.name); | 
|---|
| 124 | pThreadParm->u.getservbyname.name = 0; | 
|---|
| 125 |  | 
|---|
| 126 | if(pThreadParm->u.getservbyname.proto) { | 
|---|
| 127 | free(pThreadParm->u.getservbyname.proto); | 
|---|
| 128 | pThreadParm->u.getservbyname.proto = 0; | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | } | 
|---|
| 132 | else { | 
|---|
| 133 | ret = getservbyport(pThreadParm->u.getservbyport.port, pThreadParm->u.getservbyport.proto); | 
|---|
| 134 | if(pThreadParm->u.getservbyport.proto) { | 
|---|
| 135 | free(pThreadParm->u.getservbyport.proto); | 
|---|
| 136 | pThreadParm->u.getservbyport.proto = 0; | 
|---|
| 137 | } | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 | if (ret != NULL) { | 
|---|
| 141 | size = WS_copy_se((struct ws_servent *)pThreadParm->buf, | 
|---|
| 142 | pThreadParm->buf, | 
|---|
| 143 | pThreadParm->buflen, | 
|---|
| 144 | ret); | 
|---|
| 145 | if(size < 0) { | 
|---|
| 146 | fail = WSAENOBUFS; | 
|---|
| 147 | size = -size; | 
|---|
| 148 | } | 
|---|
| 149 | } | 
|---|
| 150 | else    fail = WSANO_DATA; | 
|---|
| 151 | break; | 
|---|
| 152 | } | 
|---|
| 153 | } | 
|---|
| 154 | } | 
|---|
| 155 | lParam = (fail << 16) | size; | 
|---|
| 156 |  | 
|---|
| 157 | if(!pThreadParm->fCancelled) { | 
|---|
| 158 | dprintf(("WSAsyncThreadProc %x %x %x %x", pThreadParm->hwnd, pThreadParm->msg, pThreadParm->hAsyncTaskHandle, lParam)); | 
|---|
| 159 | PostMessageA(pThreadParm->hwnd, pThreadParm->msg, | 
|---|
| 160 | (WPARAM)pThreadParm->hAsyncTaskHandle, lParam); | 
|---|
| 161 | } | 
|---|
| 162 | pThreadParm->fActive = FALSE; | 
|---|
| 163 | RemoveFromQueue(pThreadParm); | 
|---|
| 164 | } | 
|---|
| 165 | //****************************************************************************** | 
|---|
| 166 | //****************************************************************************** | 
|---|
| 167 | LHANDLE WSAAsyncRequest(AsyncRequestType requesttype, HWND hwnd, int msg, char *buf, | 
|---|
| 168 | int buflen, PVOID param1, PVOID param2, PVOID param3, | 
|---|
| 169 | PVOID param4) | 
|---|
| 170 | { | 
|---|
| 171 | PASYNCTHREADPARM pThreadParm; | 
|---|
| 172 | LHANDLE          hAsyncRequest = 0; | 
|---|
| 173 | LPSTR            tempname  = 0; | 
|---|
| 174 | LPSTR            tempproto = 0; | 
|---|
| 175 | LPSTR            tempaddr  = 0; | 
|---|
| 176 |  | 
|---|
| 177 | if(!fWSAInitialized) | 
|---|
| 178 | { | 
|---|
| 179 | WSASetLastError(WSANOTINITIALISED); | 
|---|
| 180 | } | 
|---|
| 181 | else | 
|---|
| 182 | if(WSAIsBlocking()) | 
|---|
| 183 | { | 
|---|
| 184 | WSASetLastError(WSAEINPROGRESS);        // blocking call in progress | 
|---|
| 185 | } | 
|---|
| 186 | else | 
|---|
| 187 | if(!IsWindow(hwnd)) | 
|---|
| 188 | { | 
|---|
| 189 | WSASetLastError(WSAEINVAL);             // invalid parameter | 
|---|
| 190 | } | 
|---|
| 191 | else | 
|---|
| 192 | if(buf == NULL || buflen == 0) { | 
|---|
| 193 | WSASetLastError(WSAENOBUFS);            // invalid parameter | 
|---|
| 194 | } | 
|---|
| 195 | else | 
|---|
| 196 | { | 
|---|
| 197 | pThreadParm = (PASYNCTHREADPARM)malloc(sizeof(ASYNCTHREADPARM)); | 
|---|
| 198 | if(pThreadParm == NULL) { | 
|---|
| 199 | dprintf(("WSAAsyncRequest: malloc failure!")); | 
|---|
| 200 | DebugInt3(); | 
|---|
| 201 | WSASetLastError(WSAEFAULT); | 
|---|
| 202 | return 0; | 
|---|
| 203 | } | 
|---|
| 204 | memset(pThreadParm, 0, sizeof(*pThreadParm)); | 
|---|
| 205 | pThreadParm->request= requesttype; | 
|---|
| 206 | pThreadParm->hwnd   = hwnd; | 
|---|
| 207 | pThreadParm->msg    = msg; | 
|---|
| 208 | pThreadParm->buf    = buf; | 
|---|
| 209 | pThreadParm->buflen = buflen; | 
|---|
| 210 |  | 
|---|
| 211 | switch(requesttype) { | 
|---|
| 212 | case ASYNC_GETHOSTBYNAME: | 
|---|
| 213 | tempname = (LPSTR)malloc(strlen((char *)param1)+1); | 
|---|
| 214 | if(tempname == NULL) | 
|---|
| 215 | { | 
|---|
| 216 | dprintf(("WSAAsyncGetHostByName: malloc failure!")); | 
|---|
| 217 | DebugInt3(); | 
|---|
| 218 | WSASetLastError(WSAEFAULT); | 
|---|
| 219 | return 0; | 
|---|
| 220 | } | 
|---|
| 221 | pThreadParm->u.gethostbyname.name   = tempname; | 
|---|
| 222 | strcpy(pThreadParm->u.gethostbyname.name, (char *)param1); | 
|---|
| 223 | break; | 
|---|
| 224 |  | 
|---|
| 225 | case ASYNC_GETHOSTBYADDR: | 
|---|
| 226 | tempaddr = (LPSTR)malloc(strlen((char *)param1)+1); | 
|---|
| 227 | if(tempaddr == NULL) | 
|---|
| 228 | { | 
|---|
| 229 | dprintf(("WSAAsyncGetHostByAddr: malloc failure!")); | 
|---|
| 230 | DebugInt3(); | 
|---|
| 231 | WSASetLastError(WSAEFAULT); | 
|---|
| 232 | return 0; | 
|---|
| 233 | } | 
|---|
| 234 |  | 
|---|
| 235 | pThreadParm->u.gethostbyaddr.len  = (int)param2; | 
|---|
| 236 | pThreadParm->u.gethostbyaddr.type = (int)param3; | 
|---|
| 237 | pThreadParm->u.gethostbyaddr.addr = tempaddr; | 
|---|
| 238 | strcpy(pThreadParm->u.gethostbyaddr.addr, (char *)param1); | 
|---|
| 239 | break; | 
|---|
| 240 |  | 
|---|
| 241 | case ASYNC_GETPROTOBYNAME: | 
|---|
| 242 | tempname = (LPSTR)malloc(strlen((char *)param1)+1); | 
|---|
| 243 | if(tempname == NULL) | 
|---|
| 244 | { | 
|---|
| 245 | dprintf(("WSAAsyncGetProtoByName: malloc failure!")); | 
|---|
| 246 | DebugInt3(); | 
|---|
| 247 | WSASetLastError(WSAEFAULT); | 
|---|
| 248 | return 0; | 
|---|
| 249 | } | 
|---|
| 250 | pThreadParm->u.getprotobyname.name = tempname; | 
|---|
| 251 | strcpy(pThreadParm->u.getprotobyname.name, (char *)param1); | 
|---|
| 252 | break; | 
|---|
| 253 |  | 
|---|
| 254 | case ASYNC_GETPROTOBYNUMBER: | 
|---|
| 255 | pThreadParm->u.getprotobynumber.number = (int)param1; | 
|---|
| 256 | break; | 
|---|
| 257 |  | 
|---|
| 258 | case ASYNC_GETSERVBYNAME: | 
|---|
| 259 | tempname    = (LPSTR)malloc(strlen((char *)param1)+1); | 
|---|
| 260 | tempproto   = NULL; | 
|---|
| 261 | if(param2) { | 
|---|
| 262 | tempproto = (LPSTR)malloc(strlen((char *)param2)+1); | 
|---|
| 263 | } | 
|---|
| 264 | if(tempname == NULL || (param2 && !tempproto)) | 
|---|
| 265 | { | 
|---|
| 266 | dprintf(("WSAAsyncGetServByName: malloc failure!")); | 
|---|
| 267 | DebugInt3(); | 
|---|
| 268 | WSASetLastError(WSAEFAULT); | 
|---|
| 269 | return 0; | 
|---|
| 270 | } | 
|---|
| 271 | pThreadParm->u.getservbyname.name   = tempname; | 
|---|
| 272 | strcpy(pThreadParm->u.getservbyname.name, (char *)param1); | 
|---|
| 273 | pThreadParm->u.getservbyname.proto   = tempproto; | 
|---|
| 274 | if(param2) | 
|---|
| 275 | strcpy(pThreadParm->u.getservbyname.proto, (char *)param2); | 
|---|
| 276 | break; | 
|---|
| 277 |  | 
|---|
| 278 | case ASYNC_GETSERVBYPORT: | 
|---|
| 279 | if(param2) { | 
|---|
| 280 | tempproto = (LPSTR)malloc(strlen((char *)param2)+1); | 
|---|
| 281 | } | 
|---|
| 282 | if(param2 && !tempproto) | 
|---|
| 283 | { | 
|---|
| 284 | dprintf(("WSAAsyncGetServByPort: malloc failure!")); | 
|---|
| 285 | DebugInt3(); | 
|---|
| 286 | WSASetLastError(WSAEFAULT); | 
|---|
| 287 | return 0; | 
|---|
| 288 | } | 
|---|
| 289 | pThreadParm->u.getservbyport.port   = (int)param1; | 
|---|
| 290 | pThreadParm->u.getservbyport.proto  = tempproto; | 
|---|
| 291 | if(param2) | 
|---|
| 292 | strcpy(pThreadParm->u.getservbyport.proto, (char *)param2); | 
|---|
| 293 | break; | 
|---|
| 294 | } | 
|---|
| 295 | hAsyncRequest = (LHANDLE)QueueAsyncJob(WSAsyncThreadProc, pThreadParm); | 
|---|
| 296 | if(hAsyncRequest == 0) { | 
|---|
| 297 | free(pThreadParm); | 
|---|
| 298 | if(tempname)    free(tempname); | 
|---|
| 299 | if(tempaddr)    free(tempaddr); | 
|---|
| 300 | if(tempproto)   free(tempproto); | 
|---|
| 301 |  | 
|---|
| 302 | dprintf(("WSAAsyncRequest: QueueAsyncJob failure!")); | 
|---|
| 303 | DebugInt3(); | 
|---|
| 304 | WSASetLastError(WSAEFAULT); | 
|---|
| 305 | return 0; | 
|---|
| 306 | } | 
|---|
| 307 | return hAsyncRequest; | 
|---|
| 308 | } | 
|---|
| 309 | return 0; | 
|---|
| 310 | } | 
|---|
| 311 | //****************************************************************************** | 
|---|
| 312 | //****************************************************************************** | 
|---|
| 313 | ODINFUNCTION5(LHANDLE,WSAAsyncGetHostByName, | 
|---|
| 314 | HWND,hWnd, | 
|---|
| 315 | u_int,wMsg, | 
|---|
| 316 | const char *,name, | 
|---|
| 317 | char *,buf, | 
|---|
| 318 | int,buflen) | 
|---|
| 319 | { | 
|---|
| 320 | dprintf(("WSAAsyncGetHostByName %s", name)); | 
|---|
| 321 |  | 
|---|
| 322 | return WSAAsyncRequest(ASYNC_GETHOSTBYNAME, hWnd, wMsg, buf, buflen, | 
|---|
| 323 | (PVOID)name, 0, 0, 0); | 
|---|
| 324 | } | 
|---|
| 325 | //****************************************************************************** | 
|---|
| 326 | //****************************************************************************** | 
|---|
| 327 | ODINFUNCTION7(LHANDLE,WSAAsyncGetHostByAddr, | 
|---|
| 328 | HWND,hWnd, | 
|---|
| 329 | u_int,wMsg, | 
|---|
| 330 | const char *,addr, | 
|---|
| 331 | int,len, | 
|---|
| 332 | int,type, | 
|---|
| 333 | char *,buf, | 
|---|
| 334 | int,buflen) | 
|---|
| 335 | { | 
|---|
| 336 | dprintf(("WSAAsyncGetHostByAddr %s", addr)); | 
|---|
| 337 |  | 
|---|
| 338 | return WSAAsyncRequest(ASYNC_GETHOSTBYADDR, hWnd, wMsg, buf, buflen, | 
|---|
| 339 | (PVOID)addr, (PVOID)len, (PVOID)type, 0); | 
|---|
| 340 | } | 
|---|
| 341 | //****************************************************************************** | 
|---|
| 342 | //****************************************************************************** | 
|---|
| 343 | ODINFUNCTION5(LHANDLE,WSAAsyncGetProtoByName, | 
|---|
| 344 | HWND,hWnd, | 
|---|
| 345 | u_int,wMsg, | 
|---|
| 346 | const char *,name, | 
|---|
| 347 | char *,buf, | 
|---|
| 348 | int,buflen) | 
|---|
| 349 | { | 
|---|
| 350 | dprintf(("WSAAsyncGetProtoByName %s", name)); | 
|---|
| 351 |  | 
|---|
| 352 | return WSAAsyncRequest(ASYNC_GETPROTOBYNAME, hWnd, wMsg, buf, buflen, | 
|---|
| 353 | (PVOID)name, 0, 0, 0); | 
|---|
| 354 | } | 
|---|
| 355 | //****************************************************************************** | 
|---|
| 356 | //****************************************************************************** | 
|---|
| 357 | ODINFUNCTION5(LHANDLE,WSAAsyncGetProtoByNumber, | 
|---|
| 358 | HWND,hWnd, | 
|---|
| 359 | u_int,wMsg, | 
|---|
| 360 | int,number, | 
|---|
| 361 | char *,buf, | 
|---|
| 362 | int,buflen) | 
|---|
| 363 | { | 
|---|
| 364 | dprintf(("WSAAsyncGetProtoByNumber %d", number)); | 
|---|
| 365 |  | 
|---|
| 366 | return WSAAsyncRequest(ASYNC_GETPROTOBYNUMBER, hWnd, wMsg, buf, buflen, | 
|---|
| 367 | (PVOID)number, 0, 0, 0); | 
|---|
| 368 | } | 
|---|
| 369 | //****************************************************************************** | 
|---|
| 370 | //****************************************************************************** | 
|---|
| 371 | ODINFUNCTION6(LHANDLE,WSAAsyncGetServByName, | 
|---|
| 372 | HWND,hWnd, | 
|---|
| 373 | u_int,wMsg, | 
|---|
| 374 | const char *,name, | 
|---|
| 375 | const char *,proto, | 
|---|
| 376 | char *,buf, | 
|---|
| 377 | int,buflen) | 
|---|
| 378 | { | 
|---|
| 379 | dprintf(("WSAAsyncGetServByName %s", name)); | 
|---|
| 380 |  | 
|---|
| 381 | return WSAAsyncRequest(ASYNC_GETSERVBYNAME, hWnd, wMsg, buf, buflen, | 
|---|
| 382 | (PVOID)name, (PVOID)proto, 0, 0); | 
|---|
| 383 | } | 
|---|
| 384 | //****************************************************************************** | 
|---|
| 385 | //****************************************************************************** | 
|---|
| 386 | ODINFUNCTION6(LHANDLE,WSAAsyncGetServByPort, | 
|---|
| 387 | HWND,hWnd, | 
|---|
| 388 | u_int,wMsg, | 
|---|
| 389 | int,port, | 
|---|
| 390 | const char *,proto, | 
|---|
| 391 | char *,buf, | 
|---|
| 392 | int,buflen) | 
|---|
| 393 | { | 
|---|
| 394 | dprintf(("WSAAsyncGetServByPort %d %s", port, proto)); | 
|---|
| 395 |  | 
|---|
| 396 | return WSAAsyncRequest(ASYNC_GETSERVBYPORT, hWnd, wMsg, buf, buflen, | 
|---|
| 397 | (PVOID)port, (PVOID)proto, 0, 0); | 
|---|
| 398 | } | 
|---|
| 399 | //****************************************************************************** | 
|---|
| 400 | //****************************************************************************** | 
|---|
| 401 | void AsyncNotifyEvent(PASYNCTHREADPARM pThreadParm, ULONG event, ULONG socket_error) | 
|---|
| 402 | { | 
|---|
| 403 | pThreadParm->u.asyncselect.lEventsPending &= ~event; | 
|---|
| 404 |  | 
|---|
| 405 | event = WSAMAKESELECTREPLY(event, socket_error); | 
|---|
| 406 |  | 
|---|
| 407 | dprintf(("AsyncNotifyEvent %x %x %x %x", pThreadParm->u.asyncselect.s, pThreadParm->hwnd, pThreadParm->msg, event)); | 
|---|
| 408 | PostMessageA(pThreadParm->hwnd, pThreadParm->msg, (WPARAM)pThreadParm->u.asyncselect.s, | 
|---|
| 409 | (LPARAM)event); | 
|---|
| 410 | } | 
|---|
| 411 | //****************************************************************************** | 
|---|
| 412 | #define  nr(i)          ((i != -1) ? 1 : 0) | 
|---|
| 413 | #define  ready(i)       ((i != -1) && (sockets[i] != -1)) | 
|---|
| 414 | //NOTE: Must execute RemoveFromQueue before returning | 
|---|
| 415 | //****************************************************************************** | 
|---|
| 416 | void ASYNCCNV WSAsyncSelectThreadProc(void *pparm) | 
|---|
| 417 | { | 
|---|
| 418 | PASYNCTHREADPARM pThreadParm = (PASYNCTHREADPARM)pparm; | 
|---|
| 419 | SOCKET           sockets[3]; | 
|---|
| 420 | SOCKET           s = pThreadParm->u.asyncselect.s; | 
|---|
| 421 | int              noread, nowrite, noexcept, state, sockoptlen, sockoptval; | 
|---|
| 422 | int              tmp, i, lEventsPending, ret, bytesread; | 
|---|
| 423 |  | 
|---|
| 424 | while(TRUE) | 
|---|
| 425 | { | 
|---|
| 426 | asyncloopstart: | 
|---|
| 427 | i      = 0; | 
|---|
| 428 | noread = nowrite = noexcept = -1; | 
|---|
| 429 |  | 
|---|
| 430 | //break if user cancelled request | 
|---|
| 431 | if(pThreadParm->u.asyncselect.lEvents == 0) { | 
|---|
| 432 | break; | 
|---|
| 433 | } | 
|---|
| 434 |  | 
|---|
| 435 | lEventsPending = pThreadParm->u.asyncselect.lEventsPending; | 
|---|
| 436 | //block if no events are pending | 
|---|
| 437 | if(lEventsPending == 0) | 
|---|
| 438 | { | 
|---|
| 439 | //wait for events to be enabled | 
|---|
| 440 | pThreadParm->u.asyncselect.asyncSem->wait(); | 
|---|
| 441 | //reset event semaphore | 
|---|
| 442 | pThreadParm->u.asyncselect.asyncSem->reset(); | 
|---|
| 443 | continue; | 
|---|
| 444 | } | 
|---|
| 445 |  | 
|---|
| 446 | if(lEventsPending & (FD_READ | FD_CLOSE | FD_ACCEPT)) { | 
|---|
| 447 | noread = i++; | 
|---|
| 448 | sockets[noread] = s; | 
|---|
| 449 | } | 
|---|
| 450 | if((lEventsPending & FD_CONNECT) || | 
|---|
| 451 | ((lEventsPending & FD_WRITE) && | 
|---|
| 452 | (!(ioctl(s, FIOBSTATUS, (char *)&tmp, sizeof(tmp)) & SS_CANTSENDMORE)))) | 
|---|
| 453 | { | 
|---|
| 454 | nowrite = i++; | 
|---|
| 455 | sockets[nowrite] = s; | 
|---|
| 456 | } | 
|---|
| 457 | if(lEventsPending & FD_OOB) { | 
|---|
| 458 | noexcept = i++; | 
|---|
| 459 | sockets[noexcept] = s; | 
|---|
| 460 | } | 
|---|
| 461 |  | 
|---|
| 462 | ////        dprintf(("WSAsyncSelectThreadProc %x rds=%d, wrs=%d, oos =%d, pending = %x", pThreadParm->u.asyncselect.s, noread, nowrite, noexcept, lEventsPending)); | 
|---|
| 463 |  | 
|---|
| 464 | pThreadParm->fWaitSelect = TRUE; | 
|---|
| 465 | ret = select((int *)sockets, nr(noread), nr(nowrite), nr(noexcept), -1); | 
|---|
| 466 | pThreadParm->fWaitSelect = FALSE; | 
|---|
| 467 | if(ret == SOCKET_ERROR) { | 
|---|
| 468 | int selecterr = sock_errno(); | 
|---|
| 469 | ////            dprintf(("WSAsyncSelectThreadProc %x rds=%d, wrs=%d, oos =%d, pending = %x select returned %x", pThreadParm->u.asyncselect.s, noread, nowrite, noexcept, lEventsPending, selecterr)); | 
|---|
| 470 | if(selecterr && selecterr < SOCBASEERR) { | 
|---|
| 471 | selecterr += SOCBASEERR; | 
|---|
| 472 | } | 
|---|
| 473 | switch(selecterr) | 
|---|
| 474 | { | 
|---|
| 475 | case SOCEINTR: | 
|---|
| 476 | ////                    state = ioctl(s, FIOBSTATUS, (char *)&tmp, sizeof(tmp)); | 
|---|
| 477 | ////                    dprintf(("SOCEINTR; state = %x", state)); | 
|---|
| 478 | goto asyncloopstart;    //so_cancel was called | 
|---|
| 479 |  | 
|---|
| 480 | case SOCECONNRESET: | 
|---|
| 481 | case SOCEPIPE: | 
|---|
| 482 | if(lEventsPending & FD_CLOSE) { | 
|---|
| 483 | dprintf(("FD_CLOSE; broken connection")); | 
|---|
| 484 | AsyncNotifyEvent(pThreadParm, FD_CLOSE, WSAECONNRESET); | 
|---|
| 485 | } | 
|---|
| 486 |  | 
|---|
| 487 | //remote connection broken (so can't receive data anymore) | 
|---|
| 488 | //but can still send | 
|---|
| 489 | pThreadParm->u.asyncselect.lEventsPending &= ~(FD_READ | FD_ACCEPT); | 
|---|
| 490 | goto asyncloopstart; | 
|---|
| 491 |  | 
|---|
| 492 | case SOCEINVAL: | 
|---|
| 493 | if(lEventsPending & FD_CLOSE) { | 
|---|
| 494 | dprintf(("FD_CLOSE; SOCEINVAL")); | 
|---|
| 495 | AsyncNotifyEvent(pThreadParm, FD_CLOSE, selecterr); | 
|---|
| 496 | } | 
|---|
| 497 | break; | 
|---|
| 498 | default: | 
|---|
| 499 | dprintf(("WSAsyncSelectThreadProc: select SOCKET_ERROR %x", selecterr)); | 
|---|
| 500 | break; //something bad happened | 
|---|
| 501 | } | 
|---|
| 502 | break; | 
|---|
| 503 | } | 
|---|
| 504 |  | 
|---|
| 505 | if(ready(nowrite)) | 
|---|
| 506 | { | 
|---|
| 507 | state = ioctl(s, FIOBSTATUS, (char *)&tmp, sizeof(tmp)); | 
|---|
| 508 |  | 
|---|
| 509 | if(lEventsPending & FD_CONNECT) { | 
|---|
| 510 | if(state & SS_ISCONNECTED) { | 
|---|
| 511 | AsyncNotifyEvent(pThreadParm, FD_CONNECT, NO_ERROR); | 
|---|
| 512 | } | 
|---|
| 513 | else { | 
|---|
| 514 | sockoptlen = sizeof(int); | 
|---|
| 515 |  | 
|---|
| 516 | ret = getsockopt(s, SOL_SOCKET, SO_ERROR, | 
|---|
| 517 | (char *) &sockoptval, &sockoptlen); | 
|---|
| 518 | //SvL: WSeB returns SOCECONNREFUSED, Warp 4 0x3d | 
|---|
| 519 | if(sockoptval == SOCECONNREFUSED || sockoptval == (SOCECONNREFUSED - SOCBASEERR)) { | 
|---|
| 520 | AsyncNotifyEvent(pThreadParm, FD_CONNECT, WSAECONNREFUSED); | 
|---|
| 521 | } | 
|---|
| 522 | } | 
|---|
| 523 | } | 
|---|
| 524 | else | 
|---|
| 525 | if(!(state & SS_CANTSENDMORE) && (lEventsPending & FD_WRITE)) { | 
|---|
| 526 | AsyncNotifyEvent(pThreadParm, FD_WRITE, NO_ERROR); | 
|---|
| 527 | } | 
|---|
| 528 | } | 
|---|
| 529 |  | 
|---|
| 530 | if(ready(noread)) | 
|---|
| 531 | { | 
|---|
| 532 | state = ioctl(s, FIONREAD, (CHAR *) &bytesread, sizeof(bytesread)); | 
|---|
| 533 | if(state == SOCKET_ERROR) | 
|---|
| 534 | { | 
|---|
| 535 | if(lEventsPending & FD_CLOSE) | 
|---|
| 536 | { | 
|---|
| 537 | dprintf(("FD_CLOSE; ioctl; socket error")); | 
|---|
| 538 | AsyncNotifyEvent(pThreadParm, FD_CLOSE, NO_ERROR); | 
|---|
| 539 | //remote connection broken (so can't receive data anymore) | 
|---|
| 540 | //but can still send | 
|---|
| 541 | pThreadParm->u.asyncselect.lEventsPending &= ~(FD_READ | FD_ACCEPT); | 
|---|
| 542 | continue; | 
|---|
| 543 | } | 
|---|
| 544 | else { | 
|---|
| 545 | dprintf(("WSAsyncSelectThreadProc: ioctl SOCKET_ERROR!")); | 
|---|
| 546 | break; //todo: correct??? | 
|---|
| 547 | } | 
|---|
| 548 | } | 
|---|
| 549 | if(lEventsPending & FD_ACCEPT) | 
|---|
| 550 | { | 
|---|
| 551 | sockoptlen = sizeof(sockoptlen); | 
|---|
| 552 |  | 
|---|
| 553 | ret = getsockopt(s, SOL_SOCKET, SO_OPTIONS, | 
|---|
| 554 | (char *) &sockoptval, &sockoptlen); | 
|---|
| 555 | if(ret == SOCKET_ERROR) { | 
|---|
| 556 | dprintf(("WSAsyncSelectThreadProc: getsockopt SOCKET_ERROR!")); | 
|---|
| 557 | break; | 
|---|
| 558 | } | 
|---|
| 559 | if((sockoptval & SO_ACCEPTCONN) == SO_ACCEPTCONN) { | 
|---|
| 560 | AsyncNotifyEvent(pThreadParm, FD_ACCEPT, NO_ERROR); | 
|---|
| 561 | } | 
|---|
| 562 | } | 
|---|
| 563 | if((lEventsPending & FD_READ) && bytesread > 0) { | 
|---|
| 564 | AsyncNotifyEvent(pThreadParm, FD_READ, NO_ERROR); | 
|---|
| 565 | } | 
|---|
| 566 | #if 0 | 
|---|
| 567 | //SvL: This generates FD_CLOSE messages when the connection is just fine | 
|---|
| 568 | //     (recv executed in another thread when select returns) | 
|---|
| 569 | else | 
|---|
| 570 | if((lEventsPending & FD_CLOSE) && (state == 0 && bytesread == 0)) { | 
|---|
| 571 | dprintf(("FD_CLOSE; state == 0 && bytesread == 0")); | 
|---|
| 572 | AsyncNotifyEvent(pThreadParm, FD_CLOSE, NO_ERROR); | 
|---|
| 573 | } | 
|---|
| 574 | #endif | 
|---|
| 575 | } | 
|---|
| 576 | if(ready(noexcept)) | 
|---|
| 577 | { | 
|---|
| 578 | if(lEventsPending & FD_OOB) { | 
|---|
| 579 | AsyncNotifyEvent(pThreadParm, FD_OOB, NO_ERROR); | 
|---|
| 580 | } | 
|---|
| 581 | } | 
|---|
| 582 | if((pThreadParm->u.asyncselect.lEventsPending & (FD_ACCEPT|FD_CLOSE|FD_CONNECT)) == | 
|---|
| 583 | (lEventsPending & (FD_ACCEPT|FD_CLOSE|FD_CONNECT))) { | 
|---|
| 584 | DosSleep(10); | 
|---|
| 585 | } | 
|---|
| 586 | } | 
|---|
| 587 | //remove it first, then delete semaphore object | 
|---|
| 588 | pThreadParm->fActive = FALSE; | 
|---|
| 589 | RemoveFromQueue(pThreadParm); | 
|---|
| 590 | delete pThreadParm->u.asyncselect.asyncSem; | 
|---|
| 591 | pThreadParm->u.asyncselect.asyncSem = 0; | 
|---|
| 592 | } | 
|---|
| 593 | //****************************************************************************** | 
|---|
| 594 | //****************************************************************************** | 
|---|
| 595 | ODINFUNCTION4(int,WSAAsyncSelect, | 
|---|
| 596 | SOCKET,s, | 
|---|
| 597 | HWND,hWnd, | 
|---|
| 598 | u_int,wMsg, | 
|---|
| 599 | long,lEvent) | 
|---|
| 600 | { | 
|---|
| 601 | PASYNCTHREADPARM pThreadParm; | 
|---|
| 602 | int              nonblock = 1; | 
|---|
| 603 | int              ret; | 
|---|
| 604 |  | 
|---|
| 605 | if(!fWSAInitialized) | 
|---|
| 606 | { | 
|---|
| 607 | WSASetLastError(WSANOTINITIALISED); | 
|---|
| 608 | return SOCKET_ERROR; | 
|---|
| 609 | } | 
|---|
| 610 | else | 
|---|
| 611 | if(WSAIsBlocking()) | 
|---|
| 612 | { | 
|---|
| 613 | WSASetLastError(WSAEINPROGRESS);        // blocking call in progress | 
|---|
| 614 | return SOCKET_ERROR; | 
|---|
| 615 | } | 
|---|
| 616 | else | 
|---|
| 617 | if(hWnd && !IsWindow(hWnd)) | 
|---|
| 618 | { | 
|---|
| 619 | WSASetLastError(WSAEINVAL);             // invalid parameter | 
|---|
| 620 | return SOCKET_ERROR; | 
|---|
| 621 | } | 
|---|
| 622 | //Set socket to non-blocking mode | 
|---|
| 623 | ret = ioctl(s, FIONBIO, (char *) &nonblock, sizeof(nonblock)); | 
|---|
| 624 | if(ret == SOCKET_ERROR) { | 
|---|
| 625 | WSASetLastError(wsaErrno()); | 
|---|
| 626 | return SOCKET_ERROR; | 
|---|
| 627 | } | 
|---|
| 628 | if(FindAndSetAsyncEvent(s, hWnd, wMsg, lEvent) == TRUE) { | 
|---|
| 629 | //found and changed active async event | 
|---|
| 630 | WSASetLastError(NO_ERROR); | 
|---|
| 631 | return NO_ERROR; | 
|---|
| 632 | } | 
|---|
| 633 | pThreadParm = (PASYNCTHREADPARM)malloc(sizeof(ASYNCTHREADPARM)); | 
|---|
| 634 | if(pThreadParm == NULL) { | 
|---|
| 635 | dprintf(("WSAAsyncSelect: malloc failure!")); | 
|---|
| 636 | DebugInt3(); | 
|---|
| 637 | WSASetLastError(WSAEFAULT); | 
|---|
| 638 | return SOCKET_ERROR; | 
|---|
| 639 | } | 
|---|
| 640 | memset(pThreadParm, 0, sizeof(*pThreadParm)); | 
|---|
| 641 | pThreadParm->request= ASYNC_SELECT; | 
|---|
| 642 | pThreadParm->hwnd   = hWnd; | 
|---|
| 643 | pThreadParm->msg    = wMsg; | 
|---|
| 644 | pThreadParm->u.asyncselect.lEvents        = lEvent; | 
|---|
| 645 | pThreadParm->u.asyncselect.lEventsPending = lEvent; | 
|---|
| 646 | pThreadParm->u.asyncselect.s              = s; | 
|---|
| 647 | pThreadParm->u.asyncselect.asyncSem       = new VSemaphore; | 
|---|
| 648 | if(pThreadParm->u.asyncselect.asyncSem == NULL) { | 
|---|
| 649 | dprintf(("WSAAsyncSelect: VSemaphore alloc failure!")); | 
|---|
| 650 | DebugInt3(); | 
|---|
| 651 | WSASetLastError(WSAEFAULT); | 
|---|
| 652 | return SOCKET_ERROR; | 
|---|
| 653 | } | 
|---|
| 654 | if(QueueAsyncJob(WSAsyncSelectThreadProc, pThreadParm) == 0) { | 
|---|
| 655 | delete pThreadParm->u.asyncselect.asyncSem; | 
|---|
| 656 | free(pThreadParm); | 
|---|
| 657 | dprintf(("WSAAsyncSelect: QueueAsyncJob failure!")); | 
|---|
| 658 | DebugInt3(); | 
|---|
| 659 | WSASetLastError(WSAEFAULT); | 
|---|
| 660 | return SOCKET_ERROR; | 
|---|
| 661 | } | 
|---|
| 662 | WSASetLastError(NO_ERROR); | 
|---|
| 663 | return NO_ERROR; | 
|---|
| 664 | } | 
|---|
| 665 | //****************************************************************************** | 
|---|
| 666 | //****************************************************************************** | 
|---|