// 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 #include #include #include #include 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 (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: /// Initializes blank instance. Packet() : header_(NULL) , size_(0) { } /// Initializes 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 (data)) , size_(size) { assert(NULL != header_); assert(0 != size_); const size_t headerSize = sizeof(Header); if (size < headerSize) { throwBadPacket( size, headerSize); } } /// Initializes as a copy of the other instance. Packet( const Packet& other) : header_(other.header_) , size_(other.size_) { } /// Does actually nothing. ~Packet() { } /// Packet bytes. const void* data() const { assert(NULL != header_); return static_cast(header_); } /// Packet size. size_t size() const { return size_; } /// Packet sequence number. SequenceNumber seqNumber() const { assert(NULL != header_); return header_->seqNumber; } /// Packet sending time. const Timestamp& sendingTime() const { assert(NULL != header_); return header_->sendingTime; } /// Returns collection of messages stored by instance. BinaryMessages messages() const { assert(NULL != header_); return BinaryMessages( header_ + 1, advanceByBytes( header_, size_)); } /// Resets the instance to the null state. void reset() { header_ = NULL; size_ = 0; } /// Re-initializes the instance /// to refer to the other content. void reset( const void* data, size_t size) { const size_t headerSize = sizeof(Header); if (data && size >= headerSize) { header_ = static_cast (data); size_ = size; } else { throwBadPacket( size, headerSize); } } /// Re-initializes the instance /// as the copy of the other one. Packet& operator =( const Packet& other) { header_ = other.header_; size_ = other.size_; return *this; } }; ONIXS_CMEMDH_LTWT_CLASS NetPacket : public Packet { const NetFeed* source_; Timestamp receiveTime_; public: /// Initializes as null packet. NetPacket() : source_(NULL) { } /// Initializes with primary attributes. NetPacket( const void* data, size_t size) : Packet(data, size) { } /// Initializes as a copy of the other instance. NetPacket( const NetPacket& other) : Packet( static_cast (other)) , source_( other.source_) , receiveTime_( other.receiveTime_) { } /// Does actually nothing. ~NetPacket() { } /// Identifies source of the packet. const NetFeed& source() const { assert(NULL != source_); return *source_; } /// Updates packet source attribute. void source( const NetFeed& source) { source_ = &source; } /// Data reception time (if applicable). const Timestamp& receiveTime() const { return receiveTime_; } /// Updates receive time attribute. void receiveTime( const Timestamp& receiveTime) { receiveTime_ = receiveTime; } /// Resets the instances to the blank state. void reset() { Packet::reset(); source_ = NULL; } /// Re-initializes as a copy of the other instance. NetPacket& operator =( const NetPacket& other) { static_cast(*this) = static_cast(other); source_ = other.source_; receiveTime_ = other.receiveTime_; return *this; } }; /// Alias for a type keeping collection /// of packet-related attributes. typedef NetPacket PacketArgs; ONIXS_CMEMDH_DATA_PACKING_END ONIXS_CMEMDH_NAMESPACE_END