NAME
Tangence::Stream
- base class for Tangence
stream-handling mixins
DESCRIPTION
This module provides a base for Tangence::Client and Tangence::Server. It is not intended to be used directly by Tangence
implementation code.
It provides the basic layer of message serialisation, deserialisation, and dispatching to methods that would handle the messages. Higher level classes are used to wrap this functionallity, and provide implementations of methods to handle the messages received.
When a message is received, it will be passed to a method whose name depends on the code of message received. The name will be handle_request_
, followed by the name of the message code, in uppercase; for example handle_request_CALL
.
REQUIRED METHODS
The following methods are required to be implemented by some class using this mixin.
$stream->tangence_write( $data )
Write bytes of data to the connected peer. $data
will be a plain perl string.
$stream->handle_request_$CODE( $token, $message )
Invoked on receipt of a given message code. $token
will be some opaque perl scalar value, and $message
will be an instance of Tangence::Message.
The value of the token has no particular meaning, other than to be passed to the respond
method.
PROVIDED METHODS
The following methods are provided by this mixin.
$stream->tangence_closed
Informs the object that the underlying connection has now been closed, and any attachments to Tangence::Object
or Tangence::ObjectProxy
instances should now be dropped.
$stream->tangence_readfrom( $buffer )
Informs the object that more data has been read from the underlying connection stream. Whole messages will be removed from the beginning of the $buffer
, which should be passed as a direct scalar (because it will be modified). This method will invoke the required handle_request_*
methods. Any bytes remaining that form the start of a partial message will be left in the buffer.
$stream->request( %args )
Serialises a message object to pass to the tangence_write
method, then enqueues a response handler to be invoked when a reply arrives. Takes the following named arguments:
- request => Tangence::Message
-
The message body
- on_response => CODE
-
CODE reference to the callback to be invoked when a response to the message is received. It will be passed the response message:
$on_response->( $message )
$response = $stream->request( request => $request )->get
When called in non-void context, this method returns a Future that will yield the response instead. In this case it should not be given an on_response
callback.
In this form, a MSG_ERROR
response will automatically turn into a failed Future; the subsequent then
or on_done
code will not have to handle this case.
$stream->respond( $token, $message )
Serialises a message object to be sent to the tangence_write
method. The $token
value that was passed to the handle_request_
method ensures that it is sent at the correct position in the stream, to allow the peer to pair it with the corresponding request.
$ver = $stream->minor_version
Returns the minor version negotiated by the MSG_INIT
/ MSG_INITED
initial message handshake.
AUTHOR
Paul Evans <leonerd@leonerd.org.uk>