Readme for Directory MTServer

Some sample code for multithreaded server programming under Windows NT
by
Dr. Thomas Becker
tmbecker@compuserve.com


Subdirectories of MTServer

  1. Include
  2. MTServerV1
  3. MTServerV2
  4. Client
  5. Timers

Directory Include

This directory contains three ready-to-use-class templates.

NamedPipeArray.h
NamedPipeArray.htm
Source and documentation of a class template that wraps an array of Win32 named pipe instances.

PassiveQueue.h
PassiveQueue.htm
Source and documentation of a class template that provides a simple thread-safe FIFO queue.

ActiveQueueWithTimeout.h
ActiveQueueWithTimeout.htm
Source and documentation of a class template that provides a thread-safe FIFO queue with variable timeout.

Directory MTServerV1

This directory contains a multithreaded server written as a console application that queues clients but does not time them out. A corresponding client program can be found in the client directory.

MTServerV1 uses the CNamedPipeArray class to communicate with clients and the CPassiveQueue class to queue them. A fixed number of working threads picks clients from the queue. The server reads one byte from the client end of the pipe. The default action is to send back the index of the pipe and the thread that served the client. Some characters cause special action; run the client program to see what these are.

The number of pipe instances, the length of the queue, and the number of worker threads can be set with preprocessor definitions at the beginning of file MTServerV1.cpp.

Building MTServerV1 with Visual C++ Version 4.x

Under Visual C++ 4.x, workspaces cannot just be moved to a different directory. Therefore, no .mdp file is provided here. The trick is to use the makefile:
  1. Choose "Open Workspace" from the "File" menu and choose "Makefiles (*.mak)" in the "Files of type" list box.
  2. Open MTServerV1.mak
That way, you will get a new workspace named MTServerV1, and all you have to do now is to build MTServerV1.exe.

Building MTServerV1 with Visual C++ Version 5.x

Under Visual C++ 5.x, workspaces can be moved to different directories. Therefore, the .dsw and .dsp files are provided here. Simple open the workspace MTServerV1.dsw and build MTServerV1.exe.

Directory MTServerV2

This directory contains a multithreaded server written as a console application that queues clients and times them out. A corresponding client program can be found in the client directory.

MTServerV2 uses the CNamedPipeArray class to communicate with clients and the CActiveQueueWithTimeout class to queue them. A fixed number of working threads picks clients from the queue. The server reads one byte from the client end of the pipe. The default action is to send back the index of the pipe and the thread that served the client. Some characters cause special action; run the client program to see what these are.

The number of pipe instances, the length of the queue, the number of worker threads, and the timeout value for all clients can be set with preprocessor definitions at the beginning of file MTServerV2.cpp.

Building MTServerV2

The procedure is the same as for MTServerV1. Under Visual C++ 4.x, open the makefile MTServerV2.mak with the "Open Workspace" (not "Open File"!) dialog to create a workspace, then build MTServerV2.exe. Under Visual C++ 5.x, just open the workspace MTServerV2.dsw and build MTServer.exe.

If you build the sample program MTServerV2 using your own workspace rather than the one provided here, then make sure you set _WIN32_WINNT to 0x0400. See the description of the Timers directory below for an explanation.

Directory Client

This directory contains a client program that can be used with the servers MTServerV1 and MTServerV2. The computer name in the pipe name is set to ".", for use on the same machine. If you want to connect to the server over the network, redefine the constant PIPE_NAME accordingly, or make it a program argument.

By default, the program connects to the server and then prompts the user to enter a character. The effect is described in the prompt.

If you compile the program with the preprocessor constant RAPID_FIRE defined, the program will make connections to the server and send ASCII 0 without user interaction. This can be used to stress-test the server.

Building the Client Program

The procedure is the same as for MTServerV1. Under Visual C++ 4.x, open the makefile client.mak with the "Open Workspace" (not "Open File"!) dialog to create a workspace, then build client.exe. Under Visual C++ 5.x, just open the workspace client.dsw and build client.exe.

Directory Timers

The class CActiveQueueWithTimeout (directory Include) uses waitable timers to achieve the timing out of queue elements. Waitable timers did not become available in Win32 until Visual C++ 5.0. To make it possible to use CActiveQueueWithTimeout with Visual C++ 4.x as well, we have provided a class CWaitableTimerSubst that provides the functionality of a waitable timer. To make it easier to switch between this substitute and real Win32 waitable timers, there is another class called CWaitableTimer. This is a thin wrapper around Win32 waitable timers that provides the same interface as the substitute class CWaitableTimerSubst.

Which of the two timer classes is used by CActiveQueueWithTimeout depends on the value of the preprocessor constant _WIN32_WINNT. If it is greater than or equal to 0x0400, then the real Win32 waitable timer is used.

If you build the sample program MTServerV2 using your own workspace rather than the one provided here (MTServerV2.dsw in the directory MTServerV2), then make sure you set _WIN32_WINNT to 0x0400. Else, you will be using the waitable timer substitute rather than the real thing.