|
@@ -0,0 +1,98 @@
|
|
|
|
+#include "wshandler.h"
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+#include <iostream>
|
|
|
|
+#include <QFile>
|
|
|
|
+
|
|
|
|
+WSHandler::WSHandler()
|
|
|
|
+{
|
|
|
|
+
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+WSHandler::~WSHandler()
|
|
|
|
+{
|
|
|
|
+ mbRun = false;
|
|
|
|
+ std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+bool WSHandler::handleConnection(CivetServer *server,
|
|
|
|
+ const struct mg_connection *conn)
|
|
|
|
+{
|
|
|
|
+ std::cout<<"WS Connected."<<std::endl;
|
|
|
|
+ return true;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void WSHandler::handleReadyState(CivetServer *server,
|
|
|
|
+ struct mg_connection *conn)
|
|
|
|
+{
|
|
|
|
+
|
|
|
|
+ std::cout<<"WS Ready"<<std::endl;
|
|
|
|
+ std::thread * pthreadsend = new std::thread(&WSHandler::ThreadSend,this,conn);
|
|
|
|
+ mvectorthread.push_back(pthreadsend);
|
|
|
|
+
|
|
|
|
+}
|
|
|
|
+bool WSHandler::handleData(CivetServer *server,
|
|
|
|
+ struct mg_connection *conn,
|
|
|
|
+ int bits,
|
|
|
|
+ char *data,
|
|
|
|
+ size_t data_len)
|
|
|
|
+{
|
|
|
|
+ return true;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void WSHandler::handleClose(CivetServer *server,
|
|
|
|
+ const struct mg_connection *conn)
|
|
|
|
+{
|
|
|
|
+ std::cout<<"WS Close "<<std::endl;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+void WSHandler::ThreadSend(mg_connection *conn)
|
|
|
|
+{
|
|
|
|
+ std::cout<<"Thread send running."<<std::endl;
|
|
|
|
+ QFile xFile;
|
|
|
|
+ xFile.setFileName("/home/nvidia/metadata.json");
|
|
|
|
+ if(!xFile.open(QIODevice::ReadOnly))
|
|
|
|
+ {
|
|
|
|
+ std::cout<<"Read meta File Fail."<<std::endl;
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ QByteArray bameta = xFile.readAll();
|
|
|
|
+ xFile.close();
|
|
|
|
+ xFile.setFileName("/home/nvidia/frame.json");
|
|
|
|
+ if(!xFile.open(QIODevice::ReadOnly))
|
|
|
|
+ {
|
|
|
|
+ std::cout<<"Read frame File Fail."<<std::endl;
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ QByteArray baframe = xFile.readAll();
|
|
|
|
+ xFile.close();
|
|
|
|
+
|
|
|
|
+ int nsend = mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_TEXT,bameta.data(),bameta.size());
|
|
|
|
+ if(nsend == 0)
|
|
|
|
+ {
|
|
|
|
+ std::cout<<" bameta websocket closed."<<std::endl;
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ if(nsend < 0)
|
|
|
|
+ {
|
|
|
|
+ std::cout<<" bameta websocket error."<<std::endl;
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ while(mbRun)
|
|
|
|
+ {
|
|
|
|
+ std::this_thread::sleep_for(std::chrono::milliseconds(300));
|
|
|
|
+ nsend = mg_websocket_write(conn, MG_WEBSOCKET_OPCODE_TEXT,baframe.data(),baframe.size());
|
|
|
|
+ if(nsend == 0)
|
|
|
|
+ {
|
|
|
|
+ std::cout<<" websocket closed."<<std::endl;
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ if(nsend < 0)
|
|
|
|
+ {
|
|
|
|
+ std::cout<<" websocket error."<<std::endl;
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ std::cout<<" Thread send closed."<<std::endl;
|
|
|
|
+}
|