#pragma once #include #include "WebSocketServer.h" #include #include namespace Incart::FileSystemProxyServer { class FlashDataWebSocketServer : public Net::WebSockets::WebSocketServer { private: std::string m_filePath; std::recursive_mutex m_lock; std::ofstream m_out; std::atomic m_isStopped; public: FlashDataWebSocketServer(quint16 port) : Net::WebSockets::WebSocketServer(port) { m_isStopped.store(true); } protected: void connectMessageListener(QWebSocket* client) override { connect(client, &QWebSocket::binaryMessageReceived, this, &FlashDataWebSocketServer::dataMessageIsReceived); } public: void startWriteToFile(const std::string& filePath_) { m_filePath = filePath_; QString filePath = QString::fromStdString(m_filePath); if (QFileInfo::exists(filePath)) { QFile file(filePath); file.remove(); } m_isStopped.store(false); } void stopWriteToFile() { m_isStopped.store(true); } public slots: void dataMessageIsReceived(const QByteArray& message) { if (m_isStopped.load()) { return; } std::cout << __PRETTY_FUNCTION__ << " message.size=" << message.size() << std::endl; std::lock_guard locker(m_lock); m_out.open(m_filePath, std::ios::app); if (m_out.is_open()) { m_out << message.toStdString(); } m_out.close(); } }; } // namespace Incart::FileSystemProxyServer