/////////////////////////////////////////////////////////////////////////////// // 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_SERVER_H #define TV_STREAMING_SERVER_H #include #include #include "OdVector.h" #include "TvMessage.h" class ConsoleLogger { bool m_bCanUpdate = false; public: ConsoleLogger() = default; ~ConsoleLogger() = default; void logMessage( const char* msg, ... ); void updateMessage( const char* msg, ... ); }; class OdTvStreamingServer : public OdTvMessageBuilder::OdTvMessageBuilderReactor { public: static std::shared_ptr< OdTvStreamingServer > createServer(); bool start( const char* port ); virtual ~OdTvStreamingServer(); enum class FileAddingResult { kNotFound = 0, kNotStreamingCompatible = 1, kLinearStreaming = 2, kPartialStreaming = 3 }; FileAddingResult addFile( const OdString& filePath ); protected: virtual bool initialize( const char* port ) = 0; virtual bool hasClient() const = 0; virtual bool readFromClient( char* pBuffer, int bufferMaxLen, int& bytesRead ) = 0; virtual bool writeToClient( const char* pBuffer, int bufferLen, int& bytesSent ) = 0; virtual bool connectClient() = 0; virtual bool onServerLoopIteration() { return true; } virtual void onMessage( OdTvMessagePtr pMessage ); protected: bool serverLoop(); struct ServerCommand { enum class Type { kIdle, kExit, kBinData, kFile }; Type m_type = Type::kIdle; bool m_bDone = false; bool m_bIncomplete = false; virtual void syncBuffer( const OdTvMessageBuilder& ) {} }; typedef std::shared_ptr< ServerCommand > ServerCommandPtr; struct ServerCommand_BinaryData : public ServerCommand { OdTvMessagePtr pMessage = std::shared_ptr< OdTvMessage >(nullptr); }; struct ServerCommand_File : public ServerCommand_BinaryData { OdString filename; OdStreamBufPtr pStream; virtual void syncBuffer( const OdTvMessageBuilder& builder ); }; struct ServerCommand_FileParts : public ServerCommand_File { OdVector< OdTvMessage::FilePart > parts; unsigned curPart = 0; OdUInt64 curOffset = 0; virtual void syncBuffer( const OdTvMessageBuilder& builder ); }; struct ServerCommand_FileStatus : public ServerCommand_BinaryData { OdTvMessage::FileStatus status; }; void addCommand( ServerCommandPtr pCmd ); void removeCommand( OdUInt64 id ); private: OdVector< char > m_buffer; std::list< ServerCommandPtr > m_commands; bool m_bDone = false; bool processCommand( ServerCommandPtr pCmd ); void removeDoneCommands(); bool processBinaryCommand( ServerCommand_BinaryData* pCmd ); bool processFileCommand( ServerCommand_File* pCmd ); struct FileDescr { OdString filePath = OdString::kEmpty; OdString clientName = OdString::kEmpty; bool bSupportPartial = false; }; OdVector< FileDescr > m_files; OdTvMessageBuilder m_builder; OdString resolveFile( const OdString& clientName ) const; protected: ConsoleLogger m_logger; public: ConsoleLogger& logger() { return m_logger; } }; typedef std::shared_ptr< OdTvStreamingServer >OdTvStreamingServerPtr; #endif