/*----------------------------------------------------------------------------*/ /* Copyright (c) FIRST 2016. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ /*----------------------------------------------------------------------------*/ #ifndef CAMERASERVER_MJPEGSERVERIMPL_H_ #define CAMERASERVER_MJPEGSERVERIMPL_H_ #include #include #include #include #include #include #include "llvm/raw_ostream.h" #include "llvm/SmallVector.h" #include "llvm/StringRef.h" #include "support/raw_istream.h" #include "support/raw_socket_ostream.h" #include "tcpsockets/NetworkAcceptor.h" #include "tcpsockets/NetworkStream.h" #include "SinkImpl.h" namespace cs { class SourceImpl; class MJPEGServerImpl : public SinkImpl { public: MJPEGServerImpl(llvm::StringRef name, llvm::StringRef description, std::unique_ptr acceptor); ~MJPEGServerImpl() override; llvm::StringRef GetDescription( llvm::SmallVectorImpl& buf) const override; void Stop(); static void SendHeader(llvm::raw_ostream& os, int code, llvm::StringRef codeText, llvm::StringRef contentType, llvm::StringRef extra = llvm::StringRef{}); static void SendError(llvm::raw_ostream& os, int code, llvm::StringRef message); static bool ReadLine(wpi::raw_istream& istream, llvm::SmallVectorImpl& buffer, int maxLen); static bool UnescapeURI(llvm::StringRef str, llvm::SmallVectorImpl& out); static bool ProcessCommand(llvm::raw_ostream& os, SourceImpl& source, llvm::StringRef parameters, bool respond); static void SendJSON(llvm::raw_ostream& os, SourceImpl& source, bool header); void SendStream(wpi::raw_socket_ostream& os); private: void ServerThreadMain(); void ConnThreadMain(wpi::NetworkStream* stream); std::string m_description; std::unique_ptr m_acceptor; std::atomic_bool m_active; // set to false to terminate threads std::thread m_serverThread; std::mutex m_mutex; std::vector m_connThreads; std::vector> m_connStreams; }; } // namespace cs #endif // CAMERASERVER_MJPEGSERVERIMPL_H_