CppCommWithFileXfer code
CppCommWithFileXfer Repository
Asynchronous message-passing communication channel
Quick Status
Code functions correctly
no known defects
Demonstration code
yes
Documentation
yes
Test cases
no, but planned
Static library
no, but planned
Build requires
C++17 option, Windows
Planned design changes
- More demonstration code
- Make file transfer interface
more user friendly
Fig 1. Async Msg-Passing Comm
Fig 2. Comm Channel Structure
Fig 3. Comm Channel classes
1.0 Concept
CppCommWithFileXfer provides a socket-based asynchronous message-passing communication channel. Each
message contains a "To" and "From" attribute with appropriate endpoint
addresses, which allows a conversational style of communication. A sender sends a message,
but does not wait for a reply - there is none. If the receiver wishes to send a reply, it does
that by simply sending another message to the initial sender, using the "From" address.
Fig 1. illustrates these messaging "conversations" and Fig 2. provides details about message
flow and file block handling. The queues shown in Fig 2. are
CppBlockingQueue instances.
2.0 Design
As shown in Fig 3., the channel provides sender and receiver components.
A sender connects with one receiver
at a time. The receiver accepts messages in a thread-safe blocking queue, so can have
multiple concurrent clients. See the blog linked below for more details.
Messages are capable of transfering binary blocks, based on a "content-length"
attribute. To send files as a series of blocks, the message has "sendFile" and
"file" attributes, where the value of the file attribute is a fully-qualified file
specification.
File blocks are not copied into messages. Instead, a message header is sent with content-length
attribute, followed by the file block. That avoids two copies - into message and then into socket.
2.1 Class Structure
The Comm class, shown in Fig 3., represents a communication facility of which Client and
Server classes each hold an instance as a member. Comm exposes a Sender and
Receiver through postMessage and getMessage methods, respectively.
A client creates an instance of a Message class with application specific attributes and data.
That is posted with Comm's postMessage method. The client's Sender
pushes the message into its SocketConnector and that is pulled by the server's
Receiver from its Socket.
Both Sender and Receiver use blocking queues to make message-passing asynchronous.
For file transfer, both Sender and Receiver open file handles for reading and
writing file blocks respectively.
3.0 Build
The channel code was built with Visual Studio Community Edition - 2019, and tested on Windows 10.
4.0 Status
Eventually, I plan to use this code to build a CppHttpClientServer, to create a channel that
uses a subset of the HTTP protocol (much of HTTP 1.0), that will support both synchronous
communication, like standard HTTP, and also asynchronous communication, by using a
"one-way" attribute, e.g., don't wait for a reply message.
The intent is to use browsers as Graphical User Interfaces for C++ projects. The UI layout will be
created with an HTML page and some Javascript and CSS. Page Javascript will use webworkers to communicate
with REST services in the application code.
There are other alternatives, but this seems like an interesting thing to try.
5.0 Resources
CommunicationChannel.pdf
Diagrams showing the communication channel structure.
Blog: Message Passing Communication
Provides discussion of channel operation, along with the diagrams linked above.