Browse Source

Merge branch 'master' of http://192.168.14.36:3000/adc_pilot/modularization

fujiankuan 4 years ago
parent
commit
36f634306c
1 changed files with 64 additions and 1 deletions
  1. 64 1
      src/driver/driver_cloud_grpc_server_stream/main.cpp

+ 64 - 1
src/driver/driver_cloud_grpc_server_stream/main.cpp

@@ -33,6 +33,60 @@ using grpc::Status;
 static cumsgbuffer gcumsgbuf;
 static pcmsgbuffer gpcmsgbuf;
 
+char gstrport[255];
+
+#include <getopt.h>
+
+void print_useage()
+{
+    std::cout<<" -p --port $port : port . eq.  -p 50051"<<std::endl;
+    std::cout<<" -h --help print help"<<std::endl;
+}
+
+int  GetOptLong(int argc, char *argv[]) {
+    int nRtn = 0;
+    int opt; // getopt_long() 的返回值
+    int digit_optind = 0; // 设置短参数类型及是否需要参数
+
+    // 如果option_index非空,它指向的变量将记录当前找到参数符合long_opts里的
+    // 第几个元素的描述,即是long_opts的下标值
+    int option_index = 0;
+    // 设置短参数类型及是否需要参数
+    const char *optstring = "m:r:x:y:o:p:s:h";
+
+
+    static struct option long_options[] = {
+        {"port", required_argument, NULL, 'p'},
+        {"help",  no_argument,       NULL, 'h'},
+ //       {"optarg", optional_argument, NULL, 'o'},
+        {0, 0, 0, 0}  // 添加 {0, 0, 0, 0} 是为了防止输入空值
+    };
+
+    while ( (opt = getopt_long(argc,
+                               argv,
+                               optstring,
+                               long_options,
+                               &option_index)) != -1) {
+        switch(opt)
+        {
+
+        case 'p':
+            strncpy(gstrport,optarg,255);
+            break;
+        case 'h':
+            print_useage();
+            nRtn = 1; //because use -h
+            break;
+        default:
+            break;
+        }
+
+    }
+
+    return nRtn;
+}
+
+
 
 void uploadsend(::grpc::ServerReaderWriter<iv::UploadReplyStream, iv::UploadRequestStream>* stream,bool * pbrun,
                 std::string * pstrvin,std::string * pstrmd5,bool *pbUpdatemd4orvin,QMutex * pmutex,int * preqid,qint64 * pnsendtime,qint64 * recvtime,QMutex * pmutexidtime)
@@ -339,7 +393,8 @@ class UploadServiceImpl final : public iv::UploadStream::Service {
 };
 
 void RunServer() {
-  std::string server_address("0.0.0.0:50051");
+  std::string server_address("0.0.0.0:");
+  server_address = server_address.append(gstrport);
   UploadServiceImpl service;
 
   grpc::EnableDefaultHealthCheckService(true);
@@ -373,6 +428,14 @@ int main(int argc, char *argv[])
 {
     QCoreApplication a(argc, argv);
 
+    snprintf(gstrport,255,"50051");
+
+    int nRtn = GetOptLong(argc,argv);
+    if(nRtn == 1)  //show help,so exit.
+    {
+        return 0;
+    }
+
     gpcmsgbuf.start();
 
     RunServer();