/////////////////////////////////////////////////////////////////////////////// // Copyright (C) 2002-2025, Open Design Alliance (the "Alliance"). // All rights reserved. // // This software and its documentation and related materials are owned by // the Alliance. The software may only be incorporated into application // programs owned by members of the Alliance, subject to a signed // Membership Agreement and Supplemental Software License Agreement with the // Alliance. The structure and organization of this software are the valuable // trade secrets of the Alliance and its suppliers. The software is also // protected by copyright law and international treaty provisions. Application // programs incorporating this software must include the following statement // with their copyright notices: // // This application incorporates Open Design Alliance software pursuant to a license // agreement with Open Design Alliance. // Open Design Alliance Copyright (C) 2002-2025 by Open Design Alliance. // All rights reserved. // // By use of this software, its documentation or related materials, you // acknowledge and accept the above terms. /////////////////////////////////////////////////////////////////////////////// #ifndef TV_STREAMING_MESSAGE_H #define TV_STREAMING_MESSAGE_H #include "OdaCommon.h" #include "OdVector.h" #include /* 3 * SERVER_PACK_SIZE is aproximate server speed. Make sure CLIENT_PACK_SIZE in few time bigger than SERVER_PACK_SIZE or socket may overflow. */ #define SERVER_PACK_SIZE 100*1024 #define CLIENT_PACK_SIZE 1000*1024 class OdTvMessage { public: enum class Type { kUnknown = 0, kStringList = 1, kBinData = 2, kCommand = 3, kFileRequest = 4, kFileStatus = 5, kFilePartsRequest = 6 }; enum class Commands { kUnknown = 0, kGet_FileList_Linear = 1, kGet_FileList_Partial = 2, kClose = 3, kCancelRequest = 4 }; enum class FileStatus { kNotFound, kComplete }; struct FilePart { OdUInt64 offset = 0; OdUInt64 size = 0; }; OdTvMessage() = default; Type type() const { return m_type; } void setType( Type t ) { m_type = t; } OdUInt64 id() const { return m_id; } void setId( OdUInt64 i ) { m_id = i; } OdUInt32 expectedDataSize() const { return m_dataSize; } void setExpectedDataSize( OdUInt32 sz ) { m_dataSize = sz; } unsigned binDataSize() const { return m_binData.size(); } const char* binData() const { return m_binData.asArrayPtr(); } void setBinData( unsigned size, const char* pData ) { m_binData.resize( size ); memcpy( m_binData.asArrayPtr(), pData, size ); } static size_t headerSize() { return sizeof( OdUInt8 ) + sizeof( OdUInt64 ) + sizeof( OdUInt32 ); } bool writeTo( char* pBuffer, size_t bufferSize ) const; static std::shared_ptr< OdTvMessage > readFrom( const char* pBuffer, size_t bufferSize, size_t& endPosition ); void resetOffset( unsigned offset = 0 ) const { m_currentOffset = offset; } unsigned currentOffset() const { return m_currentOffset; } protected: Type m_type = Type::kUnknown; OdUInt64 m_id = 0; OdUInt32 m_dataSize = 0; OdVector< char > m_binData; mutable unsigned m_currentOffset = 0; }; typedef std::shared_ptr < OdTvMessage > OdTvMessagePtr; class OdTvMessageBuilder { private: OdVector< char > m_unparsedBuffer; public: OdTvMessageBuilder() = default; ~OdTvMessageBuilder() = default; class OdTvMessageBuilderReactor { public: virtual void onMessage( OdTvMessagePtr pMessage ) = 0; virtual void onIncompleteMessage( OdTvMessagePtr pMessage ) { return; } }; void buildFromBuffer( const char* pBuffer, size_t bufferSize, OdTvMessageBuilderReactor* pReactor ); static OdTvMessagePtr buildBinaryMessage( const char* pBuffer, size_t bufferSize ); static OdTvMessagePtr buildStringListMessage( const OdVector< OdString >& strs ); static OdTvMessagePtr buildCommandMessage( OdTvMessage::Commands command ); static OdTvMessagePtr buildFileRequestMessage( const OdString& filename ); static OdTvMessagePtr buildFileStatusMessage( OdTvMessage::FileStatus st ); static OdTvMessagePtr buildFilePartsRequestMessage( const OdString& filename, const OdVector< OdTvMessage::FilePart >& parts ); static bool extractStringListFromMessage( OdTvMessagePtr pMsg, OdVector< OdString >& strs ); static OdTvMessage::Commands extractCommandFromMessage( OdTvMessagePtr pMsg ); static OdString extractFileRequestMessage( OdTvMessagePtr pMsg ); static OdTvMessage::FileStatus extractStatusFromMessage( OdTvMessagePtr pMsg ); static bool extractFilePartsFromMessage( OdTvMessagePtr pMsg, OdString& fileName, OdVector< OdTvMessage::FilePart >& parts ); }; #endif