diff options
author | Martin Lucina <martin@lucina.net> | 2012-05-20 07:40:11 +0200 |
---|---|---|
committer | Martin Lucina <martin@lucina.net> | 2012-05-20 07:40:11 +0200 |
commit | 1d76284dee8e9b0735a26ee98a3edcd9f5208f09 (patch) | |
tree | e6ac09d125e5353a3cfb4fbfd25b76f6dc7c308a /src/pgm_sender.cpp | |
parent | 8c23de9f2abc2ec21d4b74785fd175050909176e (diff) |
Implement SP wire protocolsp
Implements the SP wire protocol, and infrastructure for legacy wire
protocol support.
Also added an XS_SERVICE_ID socket option to set the service id and renamed
the XS_PROTOCOL option to XS_PATTERN_VERSION.
The following pattern versions are supported:
PAIR: v3
PUBSUB: v1 (legacy), v4
REQREP: v2
PIPELINE: v3
SURVEY: v2
Note that all existing pattern versions have been bumped by 1 to allow for
use of legacy protocols (otherwise there would be no way to distinguish
between e.g. PUBSUB v3 and PUBSUB v3 using SP).
Signed-off-by: Martin Lucina <martin@lucina.net>
Diffstat (limited to 'src/pgm_sender.cpp')
-rw-r--r-- | src/pgm_sender.cpp | 39 |
1 files changed, 28 insertions, 11 deletions
diff --git a/src/pgm_sender.cpp b/src/pgm_sender.cpp index d476cbb..528ac7a 100644 --- a/src/pgm_sender.cpp +++ b/src/pgm_sender.cpp @@ -60,6 +60,25 @@ int xs::pgm_sender_t::init (bool udp_encapsulation_, const char *network_) out_buffer_size = pgm_socket.get_max_tsdu_size (); out_buffer = (unsigned char*) malloc (out_buffer_size); alloc_assert (out_buffer); + encode_buffer = out_buffer; + encode_buffer_size = out_buffer_size; + header_size = 0; + + // If not using a legacy protocol, fill in our datagram header and reserve + // space for it in the datagram. + if (!options.legacy_protocol) { + sp_get_header (out_buffer, options.sp_service, options.sp_pattern, + options.sp_version, options.sp_role); + encode_buffer += SP_HEADER_LENGTH; + encode_buffer_size -= SP_HEADER_LENGTH; + header_size += SP_HEADER_LENGTH; + } + + // Reserve space in the datagram for the offset of the first message. + offset_p = encode_buffer; + encode_buffer += sizeof (uint16_t); + encode_buffer_size -= sizeof (uint16_t); + header_size += sizeof (uint16_t); return rc; } @@ -156,27 +175,25 @@ void xs::pgm_sender_t::in_event (fd_t fd_) void xs::pgm_sender_t::out_event (fd_t fd_) { - // POLLOUT event from send socket. If write buffer is empty, - // try to read new data from the encoder. + // POLLOUT event from send socket. If write buffer is empty (which means + // that the last write succeeded), try to read new data from the encoder. if (write_size == 0) { - // First two bytes (sizeof uint16_t) are used to store message - // offset in following steps. Note that by passing our buffer to - // the get data function we prevent it from returning its own buffer. - unsigned char *bf = out_buffer + sizeof (uint16_t); - size_t bfsz = out_buffer_size - sizeof (uint16_t); + // Pass our own buffer to the get_data () function to prevent it from + // returning its own buffer. int offset = -1; - encoder.get_data (&bf, &bfsz, &offset); + size_t data_size = encode_buffer_size; + encoder.get_data (&encode_buffer, &data_size, &offset); // If there are no data to write stop polling for output. - if (!bfsz) { + if (!data_size) { reset_pollout (handle); return; } // Put offset information in the buffer. - write_size = bfsz + sizeof (uint16_t); - put_uint16 (out_buffer, offset == -1 ? 0xffff : (uint16_t) offset); + write_size = header_size + data_size; + put_uint16 (offset_p, offset == -1 ? 0xffff : (uint16_t) offset); } if (tx_timer) { |