Ignore:
Timestamp:
Nov 22, 1999, 9:18:04 AM (26 years ago)
Author:
phaller
Message:

Fix: socket experiment with PMWSOCK

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wsock32/async.cpp

    r1761 r1807  
    1 /* $Id: async.cpp,v 1.15 1999-11-16 22:03:56 phaller Exp $ */
     1/* $Id: async.cpp,v 1.16 1999-11-22 08:18:01 phaller Exp $ */
    22
    33/*
     
    99 * Copyright 1999 Patrick Haller
    1010 *
     11 *
     12 * Remark:
     13 * - do NOT call WSASetLastError in the async worker thread, since there we don't
     14 *   know anything about the caller thread.
    1115 *
    1216 * Idea is to separate requests:
     
    5054
    5155
     56#define ERROR_SUCCESS 0
     57
     58
    5259typedef enum tagSocketStatus
    5360{
     
    6168  int hSocket; /* operating system socket descriptor */
    6269  int iStatus;
     70
     71  int iEventMaskAllowed; // bits of valid socket events are set to 1
    6372} SOCKETASYNC, *PSOCKETASYNC;
    6473
     
    116125  ULONG  ulType;                 // type of request
    117126  ULONG  ulState;                // state of request
     127  PWSOCKTHREADDATA pwstd;        // save pointer of caller thread's wsock data
    118128
    119129  HWND   hwnd;                   // window handle to post message to
     
    445455    pRequestHead->pPrev = pNew;
    446456    pRequestHead        = pNew;
     457  }
     458
     459  // queue debug
     460  {
     461    PASYNCREQUEST pTemp;
     462
     463    dprintf(("WSOCK32: WSAAsyncWorker pRequest  type      state     hwnd      message\n"));
     464    for (pTemp = pRequestHead;
     465         pTemp;
     466         pTemp = pTemp->pNext)
     467      dprintf(("WSOCK32: WSAAsyncWorker %08xh %08xh %08xh %08xh %08xh",
     468               pTemp,
     469               pTemp->ulType,
     470               pTemp->ulState,
     471               pTemp->hwnd,
     472               pTemp->ulMessage));
    447473  }
    448474
     
    596622  PASYNCREQUEST pNew = new ASYNCREQUEST();
    597623
    598   if (pNew == NULL) // check for proper allocation
    599    return pNew;
    600 
    601   // fill the structure
    602   pNew->pPrev          = NULL;
    603   pNew->pNext          = NULL;
    604   pNew->ulType         = ulType;
    605   pNew->ulState        = RS_WAITING;
    606   pNew->hwnd           = hwnd;
    607   pNew->ulMessage      = ulMessage;
    608   pNew->pBuffer        = pBuffer;
    609   pNew->ulBufferLength = ulBufferLength;
    610   pNew->ul1            = ul1;
    611   pNew->ul2            = ul2;
    612   pNew->ul3            = ul3;
     624  if (pNew != NULL) // check for proper allocation
     625  {
     626    // fill the structure
     627    pNew->pPrev          = NULL;
     628    pNew->pNext          = NULL;
     629    pNew->ulType         = ulType;
     630    pNew->ulState        = RS_WAITING;
     631    pNew->hwnd           = hwnd;
     632    pNew->ulMessage      = ulMessage;
     633    pNew->pBuffer        = pBuffer;
     634    pNew->ulBufferLength = ulBufferLength;
     635    pNew->ul1            = ul1;
     636    pNew->ul2            = ul2;
     637    pNew->ul3            = ul3;
     638
     639    // save caller thread's wsock data
     640    pNew->pwstd          = iQueryWsockThreadData();
     641  }
     642
     643  dprintf(("WSOCK32: WSAAsyncWorker:createRequest = %08xh\n",
     644           pNew));
    613645
    614646  return pNew;
     
    643675  {
    644676    rc = WSAEINVAL;
    645     WSASetLastError(rc); // same as Winsock return codes
     677    //WSASetLastError(rc); // same as Winsock return codes
    646678  }
    647679  else
     
    655687      // build error return code
    656688      rc = iTranslateSockErrToWSock(sock_errno());
    657       WSASetLastError(rc);
     689      //WSASetLastError(rc);
    658690    }
    659691    else
     
    706738  {
    707739    rc = WSAEINVAL;
    708     WSASetLastError(rc); // same as Winsock return codes
     740    //WSASetLastError(rc); // same as Winsock return codes
    709741  }
    710742  else
     
    716748      // build error return code
    717749      rc = iTranslateSockErrToWSock(sock_errno());
    718       WSASetLastError(rc);
     750      //WSASetLastError(rc);
    719751    }
    720752    else
     
    767799  {
    768800    rc = WSAEINVAL;
    769     WSASetLastError(rc); // same as Winsock return codes
     801    //WSASetLastError(rc); // same as Winsock return codes
    770802  }
    771803  else
     
    777809      // build error return code
    778810      rc = iTranslateSockErrToWSock(sock_errno());
    779       WSASetLastError(rc);
     811      //WSASetLastError(rc);
    780812    }
    781813    else
     
    826858  {
    827859    rc = WSAEINVAL;
    828     WSASetLastError(rc); // same as Winsock return codes
     860    //WSASetLastError(rc); // same as Winsock return codes
    829861  }
    830862  else
     
    836868      // build error return code
    837869      rc = iTranslateSockErrToWSock(sock_errno());
    838       WSASetLastError(rc);
     870      //WSASetLastError(rc);
    839871    }
    840872    else
     
    885917  {
    886918    rc = WSAEINVAL;
    887     WSASetLastError(rc); // same as Winsock return codes
     919    //WSASetLastError(rc); // same as Winsock return codes
    888920  }
    889921  else
     
    896928      // build error return code
    897929      rc = iTranslateSockErrToWSock(sock_errno());
    898       WSASetLastError(rc);
     930      //WSASetLastError(rc);
    899931    }
    900932    else
     
    946978  {
    947979    rc = WSAEINVAL;
    948     WSASetLastError(rc); // same as Winsock return codes
     980    //WSASetLastError(rc); // same as Winsock return codes
    949981  }
    950982  else
     
    957989      // build error return code
    958990      rc = iTranslateSockErrToWSock(sock_errno());
    959       WSASetLastError(rc);
     991      //WSASetLastError(rc);
    960992    }
    961993    else
     
    10181050  //@@@PH to do
    10191051  // 1. automatically set socket to non-blocking mode
     1052  // 2. loop until WSAAsyncSelect(s,hwnd,0,0) ?
     1053
    10201054
    10211055  //@@@PH how to implement other events?
     
    10961130
    10971131  // post result
     1132  irc = (LPARAM)((lParam << 16) | usResult);
     1133  dprintf(("WSOCK32:asyncSelect() posting %08xh hwnd%08xh, msg=%08xh, wparam=%08xh, lparam=%08xh\n",
     1134           pRequest,
     1135           pRequest->hwnd,
     1136           pRequest->ulMessage,
     1137           sockWin,
     1138           irc));
     1139
    10981140  PostMessageA(pRequest->hwnd,
    10991141               pRequest->ulMessage,
    11001142               (WPARAM)sockWin,
    1101                (LPARAM)((lParam << 16) | usResult));
     1143               (LPARAM)irc);
    11021144
    11031145  // M$ says, if PostMessageA fails, spin as long as window exists
     
    11441186  // OK, servicing request
    11451187  pRequest->ulState = RS_BUSY;
    1146   fBlocking         = TRUE;
    11471188
    11481189  switch(pRequest->ulType)
     
    11671208
    11681209  pRequest->ulState = RS_DONE;
    1169   fBlocking         = FALSE;
    11701210  return rc;
    11711211}
     
    12051245
    12061246    // wait for semaphore
     1247    fBlocking         = FALSE;
    12071248    rc = DosWaitEventSem(hevRequest, SEM_INDEFINITE_WAIT);
     1249    fBlocking         = TRUE;
    12081250    rc = DosResetEventSem(hevRequest, &ulPostCount);
    12091251  }
     
    12591301                                                    (ULONG)buflen,
    12601302                                                    (ULONG)name);
    1261   wsaWorker->pushRequest(pRequest);
    1262   return (HANDLE)pRequest;
     1303  if (pRequest != NULL)
     1304  {
     1305    // success
     1306    wsaWorker->pushRequest(pRequest);
     1307    WSASetLastError(ERROR_SUCCESS);
     1308    return (HANDLE)pRequest;
     1309  }
     1310  else
     1311  {
     1312    // error
     1313    WSASetLastError(WSAENOBUFS);
     1314    return 0;
     1315  }
    12631316}
    12641317
     
    12921345                                                    (ULONG)len,
    12931346                                                    (ULONG)type);
    1294   wsaWorker->pushRequest(pRequest);
    1295   return (HANDLE)pRequest;
     1347  if (pRequest != NULL)
     1348  {
     1349    // success
     1350    wsaWorker->pushRequest(pRequest);
     1351    WSASetLastError(ERROR_SUCCESS);
     1352    return (HANDLE)pRequest;
     1353  }
     1354  else
     1355  {
     1356    // error
     1357    WSASetLastError(WSAENOBUFS);
     1358    return 0;
     1359  }
    12961360}
    12971361
     
    13241388                                                    (ULONG)name,
    13251389                                                    (ULONG)proto);
    1326   wsaWorker->pushRequest(pRequest);
    1327   return (HANDLE)pRequest;
     1390  if (pRequest != NULL)
     1391  {
     1392    // success
     1393    wsaWorker->pushRequest(pRequest);
     1394    WSASetLastError(ERROR_SUCCESS);
     1395    return (HANDLE)pRequest;
     1396  }
     1397  else
     1398  {
     1399    // error
     1400    WSASetLastError(WSAENOBUFS);
     1401    return 0;
     1402  }
    13281403}
    13291404
     
    13561431                                                    (ULONG)port,
    13571432                                                    (ULONG)proto);
    1358   wsaWorker->pushRequest(pRequest);
    1359   return (HANDLE)pRequest;
     1433  if (pRequest != NULL)
     1434  {
     1435    // success
     1436    wsaWorker->pushRequest(pRequest);
     1437    WSASetLastError(ERROR_SUCCESS);
     1438    return (HANDLE)pRequest;
     1439  }
     1440  else
     1441  {
     1442    // error
     1443    WSASetLastError(WSAENOBUFS);
     1444    return 0;
     1445  }
    13601446}
    13611447
     
    13861472                                                    (ULONG)buflen,
    13871473                                                    (ULONG)name);
    1388   wsaWorker->pushRequest(pRequest);
    1389   return (HANDLE)pRequest;
     1474  if (pRequest != NULL)
     1475  {
     1476    // success
     1477    wsaWorker->pushRequest(pRequest);
     1478    WSASetLastError(ERROR_SUCCESS);
     1479    return (HANDLE)pRequest;
     1480  }
     1481  else
     1482  {
     1483    // error
     1484    WSASetLastError(WSAENOBUFS);
     1485    return 0;
     1486  }
    13901487}
    13911488
     
    14151512                                                    (ULONG)buflen,
    14161513                                                    (ULONG)number);
    1417   wsaWorker->pushRequest(pRequest);
    1418   return (HANDLE)pRequest;
     1514  if (pRequest != NULL)
     1515  {
     1516    // success
     1517    wsaWorker->pushRequest(pRequest);
     1518    WSASetLastError(ERROR_SUCCESS);
     1519    return (HANDLE)pRequest;
     1520  }
     1521  else
     1522  {
     1523    // error
     1524    WSASetLastError(WSAENOBUFS);
     1525    return 0;
     1526  }
    14191527}
    14201528
     
    14401548  rc = wsaWorker->cancelAsyncRequest(pRequest);
    14411549  if (rc == TRUE)
    1442     return 0; // success
     1550  {
     1551    WSASetLastError(ERROR_SUCCESS);
     1552    return ERROR_SUCCESS; // success
     1553  }
    14431554  else
    14441555  {
     1556    // error
    14451557    WSASetLastError(WSAEINVAL);
    14461558    return (SOCKET_ERROR);
     
    14851597                                  long,         lEvent)
    14861598{
     1599  // @@@PH 1999/11/21 implementation is completely wrong!
     1600  // handle async select in a completely different way:
     1601  // one thread per socket!
    14871602  PASYNCREQUEST pRequest = wsaWorker->createRequest(WSAASYNC_SELECT,
    14881603                                                    (HWND) hwnd,
     
    14921607                                                    (ULONG)s,
    14931608                                                    (ULONG)lEvent);
    1494   wsaWorker->pushRequest(pRequest);
    1495   // return (HANDLE)pRequest;
    1496   // AH: WINE returns 0 on this call
    1497   return 0;
    1498 }
    1499 
     1609  if (pRequest != NULL) // request is OK ?
     1610  {
     1611    wsaWorker->pushRequest(pRequest);
     1612
     1613    // WSAAsyncSelect() can only fail if the flags specified don't match
     1614    WSASetLastError(ERROR_SUCCESS);
     1615    return ERROR_SUCCESS;
     1616  }
     1617  else
     1618  {
     1619    // error !
     1620    WSASetLastError(WSAEINVAL);
     1621    return SOCKET_ERROR;
     1622  }
     1623}
     1624
Note: See TracChangeset for help on using the changeset viewer.