#include #include #include #include #include "xmlparam.h" #include "modulecomm.h" #include "rawpic.pb.h" #include "CivetServer.h" #include #ifdef _WIN32 #include #else #include #endif iv::vision::rawpic mRawPic; QMutex gMutex; qint64 gLastUpdate = 0; void * gpa; #define DOCUMENT_ROOT "./frontend/dist" #define PORT "8081" void Listenpic(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname) { if(nSize<1000)return; iv::vision::rawpic pic; if(false == pic.ParseFromArray(strdata,nSize)) { std::cout<<"picview Listenpic fail."<\n"); mg_printf(conn, "\n\n"); mg_printf(conn, "\n"); mg_printf(conn, "ADC IV Web UI\n"); QFile xFile; xFile.setFileName("./frontend/indexpic.html"); if(xFile.open(QIODevice::ReadOnly)) { QByteArray ba = xFile.readAll(); mg_printf(conn,ba.data()); } return 1; } }; class PicHandler : public CivetHandler { public: bool handleGet(CivetServer *server, struct mg_connection *conn) { static int ncount; // return false; // mg_printf(conn, // "HTTP/1.1 200 OK\r\nContent-Type: " // "text/html\r\nConnection: close\r\n\r\n"); // mg_printf(conn, ""); // mg_printf(conn, "

This is the Pic handler!!!

"); // mg_printf(conn, "\n"); mg_printf(conn, "HTTP/1.1 200 OK\r\n" "Connection: close\r\n" "Max-Age: 0\r\n" "Expires: 0\r\n" "Cache-Control: no-cache, no-store, must-revalidate, private\r\n" "Pragma: no-cache\r\n" "Content-Type: multipart/x-mixed-replace; " "boundary=--BoundaryString\r\n" "\r\n"); mg_printf(conn,""); mg_printf(conn, "\r\n"); QByteArray ba; if(gLastUpdate > 0) { iv::vision::rawpic xrawpic; gMutex.lock(); xrawpic.CopyFrom(mRawPic); gMutex.unlock(); ba.append(xrawpic.picdata().data(),xrawpic.picdata().size()); } mg_printf(conn, "--BoundaryString\r\n" "Content-type: image/jpeg\r\n" "Content-Length: %zu\r\n" "\r\n", ba.size()); mg_write(conn, ba.data(), ba.size()); mg_printf(conn, "\r\n\r\n"); ncount++; printf("send pic. %d\n",ncount); return true; } }; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); iv::xmlparam::Xmlparam xp("./picview_civetweb.xml"); std::string strmsgname = xp.GetParam("imagemsgname","picfront"); gpa = iv::modulecomm::RegisterRecv(strmsgname.data(),Listenpic); mg_init_library(0); const char *options[] = { "document_root", DOCUMENT_ROOT, "listening_ports", PORT, 0}; std::vector 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 WsStartHandler h_ws; server.addHandler("/", h_ws); PicHandler h_pic; server.addHandler("/pic", h_pic); return a.exec(); }