Mailslots
A Windows mailslot, like a named pipe, has a name that unrelated processes can use for communication. Mailslots are a broadcast mechanism, based on datagrams (described in Chapter 12), and behave differently from named pipes, making them useful in some important but limited situations. Here are the significant characteristics of mailslots.
- A mailslot is one-directional.
- A mailslot can have multiple writers and multiple readers, but frequently it will be one-to-many of one form or the other.
- A writer (client) does not know for certain that all, some, or any readers (servers) actually received the message.
- Mailslots can be located over a network domain.
- Message lengths are limited.
Using a mailslot requires the following operations.
- Each server creates a mailslot handle with CreateMailslot.
- The server then waits to receive a mailslot message with a ReadFile call.
- A write-only client should open the mailslot with CreateFile and write messages with WriteFile. The open will fail (name not found) if there are no waiting readers.
A client's message can be read by all servers; all of them receive the same message.There is one further possibility. The client, in performing the CreateFile, can specify a name of this form:
\\*\mailslot\mailslotname
In this way, the * acts as a wildcard, and the client can locate every server in the name domain, a networked group of systems assigned a common name by the network administrator.
Using Mailslots
The preceding client/server command processor suggests several ways that mailslots might be useful. Here is one scenario that will solve the server location problem in the preceding client/server system (Program 11-2 and 11-3).The application server, acting as a mailslot client, periodically broadcasts its name and a named pipe name. Any application client that wants to find a server can receive this name by being a mailslot server. In a similar manner, the command line server can periodically broadcast its status, including information such as utilization, to the clients. This situation could be described as a single writer (the mailslot client) and multiple readers (the mailslot servers). If there were multiple mailslot clients (that is, multiple application servers), there would be a many-to-many situation.Alternatively, a single reader could receive messages from numerous writers, perhaps giving their statusthat is, there would be multiple writers and a single reader. This usage, for example, in a bulletin board application, justifies the term mailslot. These first two usesname and status broadcastcan be combined so that a client can select the most appropriate server.The inversion of the terms client and server is confusing in this context, but notice that both named pipe and mailslot servers perform the CreateNamedPipe (or CreateMailSlot) calls, while the client (named pipe or mailslot) connects using CreateFile. Also, in both cases, the client performs the first WriteFile and the server performs the first ReadFile.Figure 11-3 shows the use of mailslots for the first approach.
Figure 11-3. Clients Using a Mailslot to Locate a Server
[View full size image]

Creating and Opening a Mailslot
The mailslot servers (readers) use CreateMailslot to create a mailslot and to get a handle for use with ReadFile. There can be only one mailslot of a given name on a specific machine, but several systems in a network can use the same name to take advantage of mailslots in a multireader situation.
HANDLE CreateMailslot (LPCTSTR lpName,
DWORD cbMaxMsg,
DWORD dwReadTimeout,
LPSECURITY_ATTRIBUTES lpsa)
Parameters
lpName points to a mailslot name of this form:
\\.\mailslot\[path]name
The name must be unique. The period (.) indicates that the mailslot is created on the current machine.cbMaxMsg is the maximum size (in bytes) for messages that a client can write. A value of 0 means no limit.dwReadTimeout is the number of milliseconds that a read operation will wait. A value of 0 causes an immediate return, and MAILSLOT_WAIT_FOREVER is an infinite wait (no time-out).The client (writer), when opening a mailslot with CreateFile, can use the following name forms.
\\.\mailslot\[path]name specifies a local mailslot. Note: Windows 95 limits the name length to 11 characters.\\computername\mailslot\[path]name specifies a mailslot on a specified machine.\\domainname\mailslot\[path]name specifies all mailslots on machines in the domain. In this case, the maximum message length is 424 bytes.\\*\mailslot\[path]name specifies all mailslots on machines in the system's primary domain. In this case, the maximum message length is also 424 bytes.
Finally, the client must specify the FILE_SHARE_READ flag.The functions GetMailslotInfo and SetMailslotInfo are similar to their named pipe counterparts.
UNIX does not have a facility comparable to mailslots. A broadcast or multicast TCP/IP datagram, however, could be used for this purpose. |
