337 lines
8.0 KiB
C++
337 lines
8.0 KiB
C++
// Copyright Onix Solutions Limited [OnixS]. All rights reserved.
|
|
//
|
|
// This software owned by Onix Solutions Limited [OnixS] and is
|
|
// protected by copyright law and international copyright treaties.
|
|
//
|
|
// Access to and use of the software is governed by the terms of the applicable
|
|
// OnixS Software Services Agreement (the Agreement) and Customer end user license
|
|
// agreements granting a non-assignable, non-transferable and non-exclusive license
|
|
// to use the software for it's own data processing purposes under the terms defined
|
|
// in the Agreement.
|
|
//
|
|
// Except as otherwise granted within the terms of the Agreement, copying or
|
|
// reproduction of any part of this source code or associated reference material
|
|
// to any other location for further reproduction or redistribution, and any
|
|
// amendments to this copyright notice, are expressly prohibited.
|
|
//
|
|
// Any reproduction or redistribution for sale or hiring of the Software not in
|
|
// accordance with the terms of the Agreement is a violation of copyright law.
|
|
//
|
|
|
|
#pragma once
|
|
|
|
#include <cassert>
|
|
#include <stdexcept>
|
|
|
|
#include <OnixS/CME/MDH/Time.h>
|
|
#include <OnixS/CME/MDH/Integral.h>
|
|
#include <OnixS/CME/MDH/BinaryMessage.h>
|
|
|
|
ONIXS_CMEMDH_NAMESPACE_BEGIN
|
|
ONIXS_CMEMDH_DATA_PACKING_BEGIN(1)
|
|
|
|
ONIXS_CMEMDH_LTWT_CLASS_DECL(NetFeed);
|
|
|
|
/// Iterator over SBE-encoded messages.
|
|
ONIXS_CMEMDH_LTWT_CLASS
|
|
BinaryMessageIterator
|
|
{
|
|
// Messages are prefixed with message header.
|
|
struct Header
|
|
{
|
|
MessageSize messageSize;
|
|
};
|
|
|
|
const Header* header_;
|
|
const void* end_;
|
|
|
|
void
|
|
validate() const
|
|
{
|
|
if (const
|
|
size_t
|
|
availableSize =
|
|
byteDistance(
|
|
end_, header_))
|
|
{
|
|
if (availableSize < sizeof(Header) ||
|
|
availableSize < header_->messageSize)
|
|
{
|
|
throw std::runtime_error(
|
|
"Not enough data to retrieve SBE message. ");
|
|
}
|
|
}
|
|
}
|
|
|
|
public:
|
|
// Identifies iterator category.
|
|
typedef
|
|
std::forward_iterator_tag
|
|
iterator_category;
|
|
|
|
/// Establishes instance difference type.
|
|
typedef
|
|
ptrdiff_t
|
|
difference_type;
|
|
|
|
/// Defines value type.
|
|
typedef
|
|
const BinaryMessage
|
|
value_type;
|
|
|
|
/// Defines pointer to a value type.
|
|
typedef value_type* pointer;
|
|
|
|
/// Defines reference to a value type.
|
|
typedef value_type& reference;
|
|
|
|
/// Constructs encoded message iterator from raw data pointers.
|
|
///
|
|
/// @throw runtime_error in case when raw data pointers
|
|
/// cannot be casted to valid encoded message iterator.
|
|
BinaryMessageIterator(
|
|
const void* dataBegin,
|
|
const void* dataEnd)
|
|
: header_(
|
|
static_cast
|
|
<const Header*>
|
|
(dataBegin))
|
|
, end_(
|
|
dataEnd)
|
|
{
|
|
assert(dataBegin <= dataEnd);
|
|
|
|
validate();
|
|
}
|
|
|
|
/// Gets reference to encoded message.
|
|
///
|
|
/// @warning Behavior is undefined in case
|
|
/// when iterator points to the packet end.
|
|
BinaryMessage message() const
|
|
{
|
|
assert(header_ != end_);
|
|
|
|
return
|
|
BinaryMessage(
|
|
header_ + 1,
|
|
header_->messageSize -
|
|
sizeof(Header));
|
|
}
|
|
|
|
/// Dereferences encoded message.
|
|
BinaryMessage operator *() const
|
|
{
|
|
return message();
|
|
}
|
|
|
|
/// Advances iterator to next message.
|
|
/// @throw runtime_error in case when iteration is not possible.
|
|
BinaryMessageIterator&
|
|
operator ++()
|
|
{
|
|
header_ =
|
|
advanceByBytes(
|
|
header_,
|
|
header_->messageSize);
|
|
|
|
validate();
|
|
|
|
return *this;
|
|
}
|
|
|
|
/// Checks whether two iterators are equal.
|
|
bool
|
|
operator ==(
|
|
const BinaryMessageIterator& other) const
|
|
{
|
|
return header_ == other.header_;
|
|
}
|
|
|
|
/// Checks whether two iterators are not equal.
|
|
bool
|
|
operator !=(
|
|
const BinaryMessageIterator& other) const
|
|
{
|
|
return header_ != other.header_;
|
|
}
|
|
};
|
|
|
|
/// Collection of encoded messages stored in single packet.
|
|
ONIXS_CMEMDH_LTWT_CLASS BinaryMessages
|
|
{
|
|
// Memory block keeping messages.
|
|
const void* begin_;
|
|
const void* end_;
|
|
|
|
public:
|
|
/// Initializes messages container.
|
|
BinaryMessages(
|
|
const void* messagesBegin,
|
|
const void* messagesEnd)
|
|
: begin_(messagesBegin)
|
|
, end_(messagesEnd)
|
|
{
|
|
}
|
|
|
|
/// Returns iterator pointing to first encoded message.
|
|
BinaryMessageIterator begin() const
|
|
{
|
|
return BinaryMessageIterator(begin_, end_);
|
|
}
|
|
|
|
/// Returns iterator pointing past last encoded message.
|
|
BinaryMessageIterator end() const
|
|
{
|
|
return BinaryMessageIterator(end_, end_);
|
|
}
|
|
};
|
|
|
|
/// Integral type used to identify packets
|
|
/// in a sequence transmitted by MDP.
|
|
typedef
|
|
UInt32
|
|
SequenceNumber;
|
|
|
|
/// Raises exception on ill-formed packet.
|
|
inline
|
|
void
|
|
throwBadPacket(
|
|
size_t packetSize,
|
|
size_t minimalSize)
|
|
{
|
|
std::string error;
|
|
|
|
error +=
|
|
"Cannot construct packet over given buffer "
|
|
"because it doesn't meet requirements on minimal size. "
|
|
"Size of buffer is ";
|
|
|
|
toStr(error, packetSize);
|
|
|
|
error += " byte(s), whereas packet must have at least ";
|
|
|
|
toStr(error, minimalSize);
|
|
|
|
error += " bytes. ";
|
|
|
|
throw std::runtime_error(error);
|
|
}
|
|
|
|
/// Represents CME binary packet containing SBE messages.
|
|
ONIXS_CMEMDH_LTWT_CLASS Packet
|
|
{
|
|
struct Header
|
|
{
|
|
SequenceNumber seqNumber;
|
|
Timestamp sendingTime;
|
|
};
|
|
|
|
const Header* header_;
|
|
size_t size_;
|
|
|
|
public:
|
|
/// Constructs binary packet over given buffer.
|
|
///
|
|
/// @throw std::exception if given buffer doesn't
|
|
/// satisfy base requirements on well-formed CME packet.
|
|
Packet(
|
|
const void* data,
|
|
size_t size)
|
|
: header_(
|
|
static_cast
|
|
<const Header*>
|
|
(data))
|
|
, size_(size)
|
|
{
|
|
assert(NULL != header_);
|
|
assert(0 != size_);
|
|
|
|
const
|
|
size_t
|
|
headerSize =
|
|
sizeof(Header);
|
|
|
|
if (size < headerSize)
|
|
{
|
|
throwBadPacket(
|
|
size, headerSize);
|
|
}
|
|
}
|
|
|
|
/// Packet bytes.
|
|
const void* data() const
|
|
{
|
|
return static_cast<const void*>(header_);
|
|
}
|
|
|
|
/// Packet size.
|
|
size_t size() const
|
|
{
|
|
return size_;
|
|
}
|
|
|
|
/// Packet sequence number.
|
|
SequenceNumber seqNumber() const
|
|
{
|
|
return header_->seqNumber;
|
|
}
|
|
|
|
/// Packet sending time.
|
|
const Timestamp& sendingTime() const
|
|
{
|
|
return header_->sendingTime;
|
|
}
|
|
|
|
/// Returns collection of messages stored by instance.
|
|
BinaryMessages messages() const
|
|
{
|
|
return
|
|
BinaryMessages(
|
|
header_ + 1,
|
|
advanceByBytes(
|
|
header_, size_));
|
|
}
|
|
};
|
|
|
|
/// Collection of attributes associated with packet.
|
|
ONIXS_CMEMDH_LTWT_CLASS PacketArgs
|
|
{
|
|
Packet packet_;
|
|
Timestamp timestamp_;
|
|
const NetFeed* source_;
|
|
|
|
public:
|
|
/// Attaches given packet to the instance.
|
|
PacketArgs(
|
|
const NetFeed& source,
|
|
const Packet& packet,
|
|
const Timestamp& timestamp)
|
|
: packet_(packet)
|
|
, timestamp_(timestamp)
|
|
, source_(&source)
|
|
{
|
|
}
|
|
|
|
/// Identifies source of data.
|
|
const NetFeed& source() const
|
|
{
|
|
return *source_;
|
|
}
|
|
|
|
/// Instance of data being referenced.
|
|
const Packet& packet() const
|
|
{
|
|
return packet_;
|
|
}
|
|
|
|
/// Data reception time (if applicable).
|
|
const Timestamp& receiveTime() const
|
|
{
|
|
return timestamp_;
|
|
}
|
|
};
|
|
|
|
ONIXS_CMEMDH_DATA_PACKING_END
|
|
ONIXS_CMEMDH_NAMESPACE_END
|