home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 9 / IOPROG_9.ISO / contrib / iis4 / iis4_07.cab / WorkerThread.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-09-05  |  3.4 KB  |  109 lines

  1. // WorkerThread.c  -> Sample ISAPI Extension demonstrating a worker thread
  2.  
  3. /*        
  4.     IIS maintains a pool of threads to handle incoming HTTP requests.  When all of
  5.     these threads are in use, new requests will be rejected.  If all the pool threads
  6.     are in a wait state (for instance, running ISAPI dlls that are waiting for a query
  7.     on a remote database to complete), IIS may reject incoming requests even if there
  8.     is plenty of CPU power to handle them.
  9.     
  10.     One way of avoiding this situation is to offload processing of these types of
  11.     requests to a worker thread, releasing the IIS thread back to the pool so that it
  12.     can be used for another request.  This basic sample demonstrates how to implement
  13.     this in an ISAPI dll.
  14. */
  15.  
  16.  
  17. #define _WIN32_WINNT 0x400
  18.  
  19.  
  20. #include <windows.h>
  21. #include <httpext.h>
  22. #include <stdio.h>
  23.  
  24.  
  25. DWORD WINAPI WorkerFunction( LPVOID ); 
  26.  
  27.  
  28. BOOL WINAPI GetExtensionVersion(HSE_VERSION_INFO *pVer)
  29. {
  30.     pVer->dwExtensionVersion = MAKELONG(HSE_VERSION_MINOR, HSE_VERSION_MAJOR);
  31.     
  32.     lstrcpyn( pVer->lpszExtensionDesc, 
  33.               "ISAPI Worker Thread Extension Sample", 
  34.               HSE_MAX_EXT_DLL_NAME_LEN );
  35.  
  36.     return TRUE;
  37. }
  38.  
  39. DWORD WINAPI HttpExtensionProc(EXTENSION_CONTROL_BLOCK *pECB)
  40. {
  41.     DWORD dwThreadID;
  42.  
  43.     // Create a thread to handle extended processing. It will be passed the address
  44.     // of a function ("WorkerFunction") to run, and the address of the ECB associated
  45.     // with this session.
  46.  
  47.     CreateThread(NULL,              // Pointer to thread security attributes 
  48.                  0,                 // Initial thread stack size, in bytes 
  49.                  WorkerFunction,    // Pointer to thread function 
  50.                  pECB,              // The ECB is the argument for the new thread
  51.                  0,                 // Creation flags 
  52.                  &dwThreadID        // Pointer to returned thread identifier 
  53.                  );
  54.  
  55.  
  56.     // Return HSE_STATUS_PENDING to release IIS pool thread without losing connection
  57.  
  58.     return HSE_STATUS_PENDING;
  59. }
  60.  
  61.  
  62. DWORD WINAPI WorkerFunction(LPVOID vECB)
  63. {
  64.     EXTENSION_CONTROL_BLOCK *pECB;
  65.     DWORD dwSize;
  66.     char szHeader[] =   "Content-type: text/html\r\n\r\n";
  67.     char szContent[]=   "<html> <form method=get action=WorkerThread.dll><h1>Worker Thread Sample</h1><hr>"
  68.                         "<input type=submit value=\"Send Request\"> </form></html>";
  69.  
  70.  
  71.     // Initialize local ECB pointer to void pointer passed to thread
  72.  
  73.     pECB = vECB;
  74.     
  75.  
  76.     // Send outgoing header
  77.     
  78.     pECB->ServerSupportFunction( pECB->ConnID, 
  79.                                  HSE_REQ_SEND_RESPONSE_HEADER,
  80.                                  NULL, 
  81.                                  NULL, 
  82.                                  (LPDWORD)szHeader );
  83.     
  84.  
  85.     // Simulate extended processing for 5 seconds
  86.     
  87.     Sleep(5000);
  88.  
  89.  
  90.     // Send content
  91.     
  92.     dwSize = strlen(szContent);
  93.  
  94.     pECB->WriteClient( pECB->ConnID, 
  95.                        szContent, 
  96.                        &dwSize, 
  97.                        0 );
  98.  
  99.     
  100.     // Inform server that the request has been satisfied, and the connection may now be dropped
  101.  
  102.     pECB->ServerSupportFunction( pECB->ConnID, 
  103.                                  HSE_REQ_DONE_WITH_SESSION, 
  104.                                  NULL, 
  105.                                  NULL, 
  106.                                  NULL );
  107.  
  108.     return 0;
  109. }