Kaynağa Gözat

add ui/xviz_civetweb.

yuchuli 3 yıl önce
ebeveyn
işleme
bc882b40ef

+ 1 - 0
src/driver/driver_cloud_grpc_civetweb/main.cpp

@@ -54,6 +54,7 @@ int main(int argc, char *argv[])
 #ifdef USE_WEBSOCKET
     RemoteHandler h_remotesocket;
     server.addWebSocketHandler("/remoteservice", h_remotesocket);
+//    server.addWebSocketHandler("/", h_remotesocket);
 //    printf("Run websocket example at http://localhost:%s/\n", PORT);
 #endif
 

+ 73 - 0
src/ui/xviz_civetweb/.gitignore

@@ -0,0 +1,73 @@
+# This file is used to ignore files which are generated
+# ----------------------------------------------------------------------------
+
+*~
+*.autosave
+*.a
+*.core
+*.moc
+*.o
+*.obj
+*.orig
+*.rej
+*.so
+*.so.*
+*_pch.h.cpp
+*_resource.rc
+*.qm
+.#*
+*.*#
+core
+!core/
+tags
+.DS_Store
+.directory
+*.debug
+Makefile*
+*.prl
+*.app
+moc_*.cpp
+ui_*.h
+qrc_*.cpp
+Thumbs.db
+*.res
+*.rc
+/.qmake.cache
+/.qmake.stash
+
+# qtcreator generated files
+*.pro.user*
+
+# xemacs temporary files
+*.flc
+
+# Vim temporary files
+.*.swp
+
+# Visual Studio generated files
+*.ib_pdb_index
+*.idb
+*.ilk
+*.pdb
+*.sln
+*.suo
+*.vcproj
+*vcproj.*.*.user
+*.ncb
+*.sdf
+*.opensdf
+*.vcxproj
+*vcxproj.*
+
+# MinGW generated files
+*.Debug
+*.Release
+
+# Python byte code
+*.pyc
+
+# Binaries
+# --------
+*.dll
+*.exe
+

+ 38 - 0
src/ui/xviz_civetweb/main.cpp

@@ -0,0 +1,38 @@
+#include <QCoreApplication>
+
+#include "wshandler.h"
+
+
+#include "xmlparam.h"
+
+#define DOCUMENT_ROOT "./frontend/dist"
+
+int main(int argc, char *argv[])
+{
+    QCoreApplication a(argc, argv);
+
+
+    iv::xmlparam::Xmlparam xp("./xviz_civetweb.xml");
+    std::string strport = xp.GetParam("Port","6101");
+
+    mg_init_library(0);
+
+
+    const char *options[] = {
+        "document_root", DOCUMENT_ROOT, "listening_ports", strport.data(), 0};
+
+    std::vector<std::string> cpp_options;
+    for (int i=0; i<(sizeof(options)/sizeof(options[0])-1); i++) {
+        cpp_options.push_back(options[i]);
+    }
+
+    // CivetServer server(options); // <-- C style start
+    CivetServer server(cpp_options); // <-- C++ style start
+
+#ifdef USE_WEBSOCKET
+    WSHandler h_wssocket;
+    server.addWebSocketHandler("/", h_wssocket);
+#endif
+
+    return a.exec();
+}

+ 98 - 0
src/ui/xviz_civetweb/wshandler.cpp

@@ -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;
+}

+ 50 - 0
src/ui/xviz_civetweb/wshandler.h

@@ -0,0 +1,50 @@
+#ifndef WSHANDLER_H
+#define WSHANDLER_H
+
+
+#include "CivetServer.h"
+#include <cstring>
+
+#ifdef _WIN32
+#include <windows.h>
+#else
+#include <unistd.h>
+#endif
+
+#include <thread>
+#include <vector>
+
+
+#ifdef USE_WEBSOCKET
+
+class WSHandler  : public CivetWebSocketHandler
+{
+public:
+    WSHandler();
+    ~WSHandler();
+
+public:
+    virtual bool handleConnection(CivetServer *server,
+                                  const struct mg_connection *conn);
+    virtual void handleReadyState(CivetServer *server,
+                                  struct mg_connection *conn);
+    virtual bool handleData(CivetServer *server,
+                            struct mg_connection *conn,
+                            int bits,
+                            char *data,
+                            size_t data_len);
+
+    virtual void handleClose(CivetServer *server,
+                             const struct mg_connection *conn);
+
+private:
+    bool mbRun = true;
+    std::vector<std::thread * > mvectorthread;
+private:
+    void ThreadSend(struct mg_connection *conn);
+};
+
+
+#endif
+
+#endif // WSHANDLER_H

+ 36 - 0
src/ui/xviz_civetweb/xviz_civetweb.pro

@@ -0,0 +1,36 @@
+QT -= gui
+
+CONFIG += c++11 console
+CONFIG -= app_bundle
+
+# The following define makes your compiler emit warnings if you use
+# any feature of Qt which as been marked deprecated (the exact warnings
+# depend on your compiler). Please consult the documentation of the
+# deprecated API in order to know how to port your code away from it.
+DEFINES += QT_DEPRECATED_WARNINGS
+
+# You can also make your code fail to compile if you use deprecated APIs.
+# In order to do so, uncomment the following line.
+# You can also select to disable deprecated APIs only up to a certain version of Qt.
+#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
+
+SOURCES += main.cpp \
+    ../../../thirdpartylib/civetweb/CivetServer.cpp \
+    ../../../thirdpartylib/civetweb/civetweb.c \
+    wshandler.cpp
+
+
+!include(../../../include/common.pri ) {
+    error( "Couldn't find the common.pri file!" )
+}
+
+
+INCLUDEPATH += $$PWD/../../../thirdpartylib/civetweb
+DEFINES += NO_SSL
+#DEFINES += NO_SSL_DL
+DEFINES += USE_WEBSOCKET
+
+HEADERS += \
+    ../../../thirdpartylib/civetweb/CivetServer.h \
+    ../../../thirdpartylib/civetweb/civetweb.h \
+    wshandler.h