Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Changes from Version 1 of TopicProtocols

Show
Ignore:
Author:
JJR (IP: 207.194.78.116)
Timestamp:
12/07/07 05:49:35 (16 years ago)
Comment:

first addition

Legend:

Unmodified
Added
Removed
Modified
  • TopicProtocols

    v0 v1  
     1== Protocols == 
     2 
     3Conduits expose typeless data streaming only. Tango provides support for typed streaming via a reader and writer pair; readers extract data-types from a stream whilst writers inject data types into a stream. Each of the native D types is supported, along with their one-dimensional array variants.  
     4 
     5Readers and writers may be configured to converse in text or binary format, handle endian conversion etc, by furnishing them with a protocol. These protocols handle both encoding and decoding of typed data, and represent the core of the stream conversion facilities. Data encoded by one particular protocol can generally be decoded correctly ''only'' by the same protocol. Readers and writers are actually 'skins' around a protocol instance and, while not strictly necessary for the operation of a protocol, we tend to consider reader, writer and protocol as a combined entity.  
     6 
     7Protocols are often attached to a conduit, where the intent is to stream D types to and/or from a file or socket – thus, a conduit is often attached at protocol construction time. In such cases, interacting with the protocol (directly, or via a reader/writer) will access the attached conduit accordingly. 
     8 
     9Some protocols treat arrays in a specific manner: they prefix each with the number of elements contained. When reading, each protocol normally performs array allocation on behalf of the application, utilizing said element count. For the sake of simplicity, the default allocation strategy is to assign via the heap. However, the strategy itself is configurable and a handful of implementations are provided to choose from. One to note is the null strategy, where arrays are instead managed by the application and the length prefix is assumed to be missing from the stream. This allows for those cases where, for example, an array length might be declared in a header.  
     10 
     11In contrast to the vararg style popularized by printf/readf et al, reader & writer methods are discrete for each element read or written, and support call chaining. Thus, basic protocol usage can be illustrated as shown below where input & output represent a protocol reader & writer pair, and where opCall is leveraged in a symmetrical style known as whisper: 
     12{{{ 
     13#!d 
     14int i; 
     15double d; 
     16char[] text; 
     17 
     18output (i) (d) (text); 
     19input  (i) (d) (text); 
     20}}} 
     21 
     22In addition, protocols support a simple object serialization strategy where compatible objects implement a specific interface. Such objects are capable of reading and/or writing their own content via the relevant interface method: 
     23{{{ 
     24#!d 
     25void read  (IReader input); 
     26void write (IWriter output); 
     27}}} 
     28 
     29The interfaces involved are IReadable and IWritable, each of which declares one method relating to their behavior. Compatible objects are read & written in a manner similar to native data types. Following on from the prior example: 
     30{{{ 
     31#!d 
     32class  Wumpus : IReadable, IWritable  
     33{ 
     34    void read  (IReader input) {...} 
     35    void write (IWriter output) {...} 
     36} 
     37 
     38auto wumpus = new Wumpus; 
     39output (wumpus); 
     40input  (wumpus); 
     41}}} 
     42 
     43Serialization of structs is handled in a similar manner, by submitting a delegate instead of an object: 
     44{{{ 
     45#!d 
     46struct Wombat  
     47{ 
     48    void read  (IReader input) {...} 
     49    void write (IWriter output) {...} 
     50} 
     51 
     52Wombat wombat; 
     53 
     54output (&wombat.write); 
     55input  (&wombat.read); 
     56}}} 
     57 
     58=== Protocol Exceptions === 
     59 
     60Protocols generally expect data to be available when an application asks for it. Thus, exceptions are thrown on unexpected end-of-flow conditions. This is quite unlike direct conduit IO, which is stream oriented rather than element oriented. 
     61 
     62 
     63 
     64 
     65 
     66 
     67