Program 14-4 serverCPc: A Server Using a Completion Port in .NET

Create QR Code in .NET Program 14-4 serverCPc: A Server Using a Completion Port
Program 14-4 serverCPc: A Server Using a Completion Port
QR-Code Generator In VS .NET
Using Barcode generation for .NET framework Control to generate, create Quick Response Code image in .NET framework applications.
/* 14 ServerCP Multithreaded server Named pipe version, COMPLETION PORT example Usage: Server [UserName GroupName] */ #include "EvryThngh" #include "ClntSrvrh" /* Request and response messages defined here */ typedef struct { /* Completion port keys point to these structures, */ HANDLE hNp; /* which represent outstanding ReadFile */ REQUEST Req; /* and ConnectNamedPipe operations */ DWORD Type; /* 0 for ConnectNamedPipe; 1 for ReadFile */ OVERLAPPED Ov; } CP_KEY; static CP_KEY Key [MAX_CLIENTS_CP]; /* Available to all threads */ /* */ _tmain (int argc, LPTSTR argv []) { HANDLE hCp, hMonitor, hSrvrThread [MAX_CLIENTS]; DWORD iNp, iTh, MonitorId, ThreadId; THREAD_ARG ThArgs [MAX_SERVER_TH]; /* */ hCp = CreateIoCompletionPort (INVALID_HANDLE_VALUE, NULL, 0, MAX_SERVER_TH); /* Create an overlapped named pipe for every potential client, */ /* add to the completion port, and wait for a connection */ /* Assume that the maximum number of clients far exceeds */ /* the number of server threads */ for (iNp = 0; iNp < MAX_CLIENTS_CP; iNp++) { memset (&Key [iNp], 0, sizeof (CP_KEY)); Key [iNp]hNp = CreateNamedPipe (SERVER_PIPE, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, PIPE_READMODE_MESSAGE | PIPE_TYPE_MESSAGE | PIPE_WAIT, MAX_CLIENTS_CP, 0, 0, INFINITE, pNPSA);
QR Scanner In VS .NET
Using Barcode scanner for VS .NET Control to read, scan read, scan image in VS .NET applications.
CreateIoCompletionPort (Key [iNp]hNp, hCp, iNp, MAX_SERVER_TH + 2); Key [iNp]OvhEvent = CreateEvent (NULL, TRUE, FALSE, NULL); ConnectNamedPipe (Key [iNp]hNp, &Key [iNp]Ov); } /* Create server worker threads and a temp file name for each */ for (iTh = 0; iTh < MAX_SERVER_TH; iTh++) { ThArgs [iTh]hCompPort = hCp; ThArgs [iTh]ThreadNo = iTh; GetTempFileName (_T (""), _T ("CLP"), 0, ThArgs [iTh]TmpFileName); hSrvrThread [iTh] = (HANDLE)_beginthreadex (NULL, 0, Server, &ThArgs [iTh], 0, &ThreadId); } /* Wait for all the threads to terminate and clean up */ /* */ return 0; } static DWORD WINAPI Server (LPTHREAD_ARG pThArg) /* Server thread function There is a thread for every potential client */ { HANDLE hCp, hTmpFile = INVALID_HANDLE_VALUE; HANDLE hWrEvent = CreateEvent (NULL, TRUE, FALSE, NULL); DWORD nXfer, KeyIndex, ServerNumber; /* */ BOOL Success, Disconnect, Exit = FALSE; LPOVERLAPPED pOv; OVERLAPPED ovResp = {0, 0, 0, 0, hWrEvent}; /* For responses */ /* To prevent an overlapped operation from being queued on the CP, the event must have the low-order bit set This is strange, but it's the documented way to do it */ ovResphEvent = (HANDLE) ((DWORD) hWrEvent | 0x1); GetStartupInfo (&StartInfoCh); hCp = pThArg->hCompPort; ServerNumber = pThArg->ThreadNo; while (!ShutDown && !Exit) __try { Success = FALSE; /* Set only when everything has succeeded */ Disconnect = FALSE; GetQueuedCompletionStatus (hCp, &nXfer, &KeyIndex, &pOv, INFINITE); if (Key [KeyIndex]Type == 0) { /* A connection has completed */ /* Open the temporary results file for this connection */ hTmpFile = CreateFile (pThArg->TmpFileName, /* */); Key [KeyIndex]Type = 1; Disconnect = !ReadFile (Key [KeyIndex]hNp, &Key [KeyIndex]Req, RQ_SIZE, &nXfer, &Key [KeyIndex]Ov) && GetLastError () == ERROR_HANDLE_EOF; /* First read */ if (Disconnect) continue; Success = TRUE; } else { /* A read has completed Process the request */ ShutDown = ShutDown || (_tcscmp (Key [KeyIndex]ReqRecord, ShutRqst) == 0); if (ShutDown) continue;
Encode Barcode In .NET Framework
Using Barcode generation for Visual Studio .NET Control to generate, create bar code image in .NET applications.
/* Create a process to carry out the command */ /* */ /* Respond a line at a time It is convenient to use C library line-oriented routines at this point */ fp = _tfopen (pThArg->TmpFileName, _T ("r")); ResponseStatus = 0; /* Responses are not queued on the completion port as the low-order bit of the event is set */ while (_fgetts(ResponseRecord, MAX_RQRS_LEN, fp) != NULL) { WriteFile (Key [KeyIndex]hNp, &Response, RS_SIZE, &nXfer, &ovResp); WaitForSingleObject (hWrEvent, INFINITE); } fclose (fp); /* Erase temp file contents */ SetFilePointer (hTmpFile, 0, NULL, FILE_BEGIN); SetEndOfFile (hTmpFile); /* Send an end of response indicator */ ResponseStatus = 1; strcpy (ResponseRecord, ""); WriteFile (Key [KeyIndex]hNp, &Response, RS_SIZE, &nXfer, &ovResp); WaitForSingleObject (hWrEvent, INFINITE); /* End of main command loop Get next command */ Disconnect = !ReadFile (Key [KeyIndex]hNp, &Key [KeyIndex]Req, RQ_SIZE, &nXfer, &Key [KeyIndex]Ov) && GetLastError () == ERROR_HANDLE_EOF; /* Next read */ if (Disconnect) continue; Success = TRUE; } } __finally { if (Disconnect) { /* Issue another connect on this pipe */ Key [KeyIndex]Type = 0; DisconnectNamedPipe (Key [KeyIndex]hNp); ConnectNamedPipe (Key [KeyIndex]hNp, &Key [KeyIndex]Ov); } if (!Success) { ReportError (_T ("Server failure"), 0, TRUE); Exit = TRUE; } } FlushFileBuffers (Key [KeyIndex]hNp); DisconnectNamedPipe (Key [KeyIndex]hNp); CloseHandle (hTmpFile); /* */ _endthreadex (0); return 0; /* Suppress a compiler warning message */ }
Reading Barcode In .NET Framework
Using Barcode recognizer for VS .NET Control to read, scan read, scan image in .NET framework applications.
Summary
QR Generator In Visual C#.NET
Using Barcode creation for .NET framework Control to generate, create Denso QR Bar Code image in VS .NET applications.
Windows has three methods for performing asynchronous I/O Using threads is the most general and simplest technique and, unlike the other two, works with Windows 9x Each thread is responsible for a sequence of one or more sequential, blocking I/O operations Furthermore, each thread should have its own file or pipe handle Overlapped I/O allows a single thread to perform asynchronous operations on a single file handle, but there must be an event handle, rather than a thread and file handle pair, for each operation Wait specifically for each I/O operation to complete and then perform any required cleanup or sequencing operations Extended I/O, on the other hand, automatically invokes the completion code, and it does not require additional events The one indispensable advantage provided by overlapped I/O is the ability to create I/O completion ports, but, as mentioned previously and illustrated by a program, atouMTCPc, on the book's Web site, even that advantage is somewhat constrained by the ability to use semaphores to limit the number of active threads in a worker thread pool The inability to remove handles from a completion port is an additional limitation UNIX supports threads through Pthreads, as discussed previously System V UNIX limits asynchronous I/O to streams and cannot be used for file or pipe operations BSD Version 43 uses a combination of signals (SIGIO) to indicate an event on a file descriptor and select a function to determine the ready state of file descriptors The file descriptors must be set in the O_ASYNC mode This approach works only with terminals and network communication
Print QR Code JIS X 0510 In .NET Framework
Using Barcode printer for ASP.NET Control to generate, create QR Code JIS X 0510 image in ASP.NET applications.
QR Code Printer In Visual Basic .NET
Using Barcode generator for .NET Control to generate, create QR Code image in .NET framework applications.
EAN128 Drawer In Visual Studio .NET
Using Barcode encoder for .NET Control to generate, create EAN128 image in .NET applications.
Making Code 128 Code Set B In .NET
Using Barcode generation for Visual Studio .NET Control to generate, create Code 128 Code Set A image in .NET framework applications.
Generate Code 3/9 In Visual Basic .NET
Using Barcode generation for .NET Control to generate, create Code 3/9 image in .NET framework applications.
Paint Barcode In Java
Using Barcode generator for Java Control to generate, create bar code image in Java applications.
UPC Symbol Creator In Java
Using Barcode encoder for Java Control to generate, create UCC - 12 image in Java applications.
Creating Barcode In Visual C#
Using Barcode maker for Visual Studio .NET Control to generate, create barcode image in VS .NET applications.