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