BDE 4.14.0 Production release
|
Provide a mechanism for reading control messages from a named pipe.
This component provides a platform-independent mechanism, balb::PipeControlChannel
, for establishing, monitoring, and shutting down a named pipe. It reads text messages (generally short operational commands) from the pipe and passes them to a callback function. Note that this component does not provide a general transport mechanism over a named pipe: it is specialized for the case that the messages to be read are relatively short newline-terminated strings.
This component is thread-safe but not thread-enabled, meaning that multiple threads may safely use their own instances of PipeControlChannel, but may not manipulate the same PipeControlChannel simultaneously (except that shutdown
may always be called safely). The start
function creates a new thread which listens to the pipe for messages until shutdown
is called.
Users that expect multiple concurrent writers to a single pipe must be aware that the message content might be corrupted (interleaved) unless:
write
system call.PIPE_BUF
(the limit for guaranteed atomicity).The value PIPE_BUF
depends on the platform:
Also note that Linux allows the PIPE_BUF
size to be changed via the fcntl
system call.
This component requires a fully-qualified native pipe name. bdlsu::PipeUtil
provides a portable utility method to generate such names.
This component requires a trailing newline ('
') character at the end of each message. This trailing newline is stripped from the message before the message is passed to the control callback.
Pipe-name encodings have the following caveats for the following operating systems:
balb::PipeControlChannel
that take or return a pipe name as bsl::string
(or a reference to it) type assume that the name is encoded in UTF-8. The routines attempt to convert the name to a UTF-16 wchar_t
string via bdlde::CharConvertUtf16::utf8ToUtf16
, and if the conversion succeeds, call the Windows wide-character W
APIs with the UTF-16 name. If the conversion fails, the method fails.utf8ToUtf16
nor the Windows W
APIs do any normalization of the UTF-16 strings resulting from UTF-8 conversion, and it is therefore possible to have sets of pipe names that have the same visual representation but are treated as different names by the system.balb::PipeControlChannel
as bsl::string
type is passed unchanged to the underlying system file APIs. Because the pipe names are passed unchanged, balb::PipeControlChannel
methods will work correctly on Posix with any encoding, but will interoperate only with processes that use the same encoding as the current process.This section illustrates intended use of this component.
This example illustrates how to construct a simple server that records messages sent on a pipe control channel until "EXIT" is received, at which point the channel is closed and the server stops.
First, let's define the implementation of our server.
Now, construct and run the server using a canonical name for the pipe:
Once the server is started, clients can send messages to the server.
The server shuts down once it processes the "EXIT" control message.
Finally, let's ensure the server received each control message sent.