home *** CD-ROM | disk | FTP | other *** search
- // WorkerThread.c -> Sample ISAPI Extension demonstrating a worker thread
-
- /*
- IIS maintains a pool of threads to handle incoming HTTP requests. When all of
- these threads are in use, new requests will be rejected. If all the pool threads
- are in a wait state (for instance, running ISAPI dlls that are waiting for a query
- on a remote database to complete), IIS may reject incoming requests even if there
- is plenty of CPU power to handle them.
-
- One way of avoiding this situation is to offload processing of these types of
- requests to a worker thread, releasing the IIS thread back to the pool so that it
- can be used for another request. This basic sample demonstrates how to implement
- this in an ISAPI dll.
- */
-
-
- #define _WIN32_WINNT 0x400
-
-
- #include <windows.h>
- #include <httpext.h>
- #include <stdio.h>
-
-
- DWORD WINAPI WorkerFunction( LPVOID );
-
-
- BOOL WINAPI GetExtensionVersion(HSE_VERSION_INFO *pVer)
- {
- pVer->dwExtensionVersion = MAKELONG(HSE_VERSION_MINOR, HSE_VERSION_MAJOR);
-
- lstrcpyn( pVer->lpszExtensionDesc,
- "ISAPI Worker Thread Extension Sample",
- HSE_MAX_EXT_DLL_NAME_LEN );
-
- return TRUE;
- }
-
- DWORD WINAPI HttpExtensionProc(EXTENSION_CONTROL_BLOCK *pECB)
- {
- DWORD dwThreadID;
-
- // Create a thread to handle extended processing. It will be passed the address
- // of a function ("WorkerFunction") to run, and the address of the ECB associated
- // with this session.
-
- CreateThread(NULL, // Pointer to thread security attributes
- 0, // Initial thread stack size, in bytes
- WorkerFunction, // Pointer to thread function
- pECB, // The ECB is the argument for the new thread
- 0, // Creation flags
- &dwThreadID // Pointer to returned thread identifier
- );
-
-
- // Return HSE_STATUS_PENDING to release IIS pool thread without losing connection
-
- return HSE_STATUS_PENDING;
- }
-
-
- DWORD WINAPI WorkerFunction(LPVOID vECB)
- {
- EXTENSION_CONTROL_BLOCK *pECB;
- DWORD dwSize;
- char szHeader[] = "Content-type: text/html\r\n\r\n";
- char szContent[]= "<html> <form method=get action=WorkerThread.dll><h1>Worker Thread Sample</h1><hr>"
- "<input type=submit value=\"Send Request\"> </form></html>";
-
-
- // Initialize local ECB pointer to void pointer passed to thread
-
- pECB = vECB;
-
-
- // Send outgoing header
-
- pECB->ServerSupportFunction( pECB->ConnID,
- HSE_REQ_SEND_RESPONSE_HEADER,
- NULL,
- NULL,
- (LPDWORD)szHeader );
-
-
- // Simulate extended processing for 5 seconds
-
- Sleep(5000);
-
-
- // Send content
-
- dwSize = strlen(szContent);
-
- pECB->WriteClient( pECB->ConnID,
- szContent,
- &dwSize,
- 0 );
-
-
- // Inform server that the request has been satisfied, and the connection may now be dropped
-
- pECB->ServerSupportFunction( pECB->ConnID,
- HSE_REQ_DONE_WITH_SESSION,
- NULL,
- NULL,
- NULL );
-
- return 0;
- }