-
class
PortableMessageIOGateway
: public AbstractMessageIOGateway
(Return to index)
-
A "gateway" object that knows how to send/receive PortableMessages over a wire(via a provided PortableDataIO object).
Type:
Superclasses:
Include file:
../../iogateway/PortableMessageIOGateway.h
May be subclassed to change the byte-level
protocol, or used as is if the default protocol is desired.
The default protocol format used by this class is:
1. 4 bytes (uint32) indicating the flattened size of the message
2. 4 bytes (uint32) indicating the encoding type (should always be ENCODING_DEFAULT for now)
3. n bytes of flattened PortableMessage (where n is the value specified in 1)
4. goto 1 ...
Synopsis:
Public methods:
- public virtual status_t
DoInput ()
Reads some more bytes from the wire.
If enough bytes have been read to assemble one or more new PortableMessages,
the new PortableMessages will be available after this method returns by
calling GetNextIncomingMessage().
Tries not to block, but may (depending on implementation)
Returns B_NO_ERROR usually, or B_ERROR if the connection has been broken
or some other catastrophic condition has occurred.
- public virtual status_t
DoOutput ()
Writes some of our outgoing messages to the wire.
Not guaranteed to write all outgoing messages (it will try not to block)
Returns B_NO_ERROR usually, or B_ERROR if the connection has been broken
or some other catastrophic condition has occurred.
- public virtual bool
HasBytesToOutput () const
Returns true iff there are any PortableMessages that have been
added (with AddOutgoingMessage()) but not already sent across the PortableDataIO.
Always returns false if the connection has been closed or hosed.
- public
PortableMessageIOGateway (PortableDataIO * io, long encoding = ENCODING_DEFAULT)
This object becomes the owner of (io) and will delete it later.
- public virtual
~PortableMessageIOGateway ()
Protected methods:
- protected virtual status_t
FlattenMessage (const PortableMessage & msg, uint8 * header, uint8 * buffer) const
Flattens (msg) into (buffer) and (header).
(buffer) is FlattenedMessageSize() bytes long. This should have header info written to it (including body size!)
(header) is HeaderSize() bytes long. This should have the body data written into it
Returns B_NO_ERROR if the message should be sent, B_ERROR if this message should
be dropped.
Default implementation always returns true.
- protected virtual size_t
FlattenedMessageBodySize (const PortableMessage & msg) const
Should return the total size of the body buffer for sending this (msg).
Default implementation returns msg.FlattenedSize().
- protected virtual status_t
GetBodySize (const uint8 * header, size_t & setSize) const
Extracts and returns (in setSize) the dynamic buffer size from the given header.
Note that the returned size should NOT include the header bytes!
(headerBuf) is HeaderSize() bytes long.
Returns B_NO_ERROR iff successful.
- protected virtual size_t
HeaderSize () const
Returns the size of the pre-flattened-message header section, in bytes.
The default format has an 8-byte header (4 bytes for encoding ID, 4 bytes for message size)
- protected virtual status_t
RecoverFromInvalidHeader (const uint8 * headerBuf, size_t & setFirstValidByte) const
This method is called after GetBodySize() returns an error code. (i.e.
when the header bytes we read in were not valid)
It should return B_NO_ERROR if the gateway should try to recover from
the invalid header, or B_ERROR if it should just give up and close the connection.
If it returns B_NO_ERROR, it may set the (setFirstValidByte) parameter to the
index of the first byte in the header buffer to start reading the next header
from, or leave (setFirstValidByte) unchanged to throw out all the bytes in the
current header buffer and start afresh.
Default implementation just returns B_ERROR.
(headerBuf) the header bytes that were read in.
(setFirstValidByte) the length of the header. May be set as described above.
- protected virtual status_t
RecoverFromInvalidMessage (const uint8 * messageBuf, size_t messageBufSize) const
This method is called after UnflattenMessage() returns an error code (i.e.
when the body bytes we read in were not valid)
It should return B_NO_ERROR if the gateway should try to recover from the
invalid message, or B_ERROR if it should just give up and close the connection.
If B_NO_ERROR is returned, the the gateway will begin trying to read the
next header from the bytes immediately after these.
- protected virtual status_t
UnflattenMessage (PortableMessage & setMsg, const uint8 * header, const uint8 * buf, size_t bufSize) const
Unflattens (msg) from (buffer) and (header).
(header) is HeaderSize() bytes long.
(buffer) is (bufSize) bytes long.