main.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include <QCoreApplication>
  2. #include <thread>
  3. #include <chrono>
  4. #include <iostream>
  5. #include "modulecomm.h"
  6. #include "modulecommext.h"
  7. #include <QSharedMemory>
  8. #include "testmodulecommext.pb.h"
  9. void * gpa;
  10. std::chrono::time_point<std::chrono::steady_clock, std::chrono::duration<double,std::nano>> t1,t2;
  11. void testcall(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  12. {
  13. qint64 sendtime;
  14. t2 = std::chrono::steady_clock::now();
  15. double dr_ns = std::chrono::duration<double,std::nano>(t2-t1).count();
  16. memcpy(&sendtime,strdata,8);
  17. qDebug("lat is %d ns is %f ",QDateTime::currentMSecsSinceEpoch() - sendtime,dr_ns);
  18. }
  19. void threadsend()
  20. {
  21. char * strdata = new char[3000000];
  22. while(1)
  23. {
  24. std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  25. qint64 ntime = QDateTime::currentMSecsSinceEpoch();
  26. memcpy(strdata,&ntime,8);
  27. iv::modulecomm::ModuleSendMsg(gpa,strdata,2000000);
  28. t1 = std::chrono::steady_clock::now();
  29. qDebug("send msg.");
  30. }
  31. }
  32. void testcall2(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  33. {
  34. qint64 sendtime;
  35. t2 = std::chrono::steady_clock::now();
  36. double dr_ns = std::chrono::duration<double,std::nano>(t2-t1).count();
  37. memcpy(&sendtime,strdata,8);
  38. qDebug("lat is %d ns is %f ",QDateTime::currentMSecsSinceEpoch() - sendtime,dr_ns);
  39. }
  40. void threadsend2()
  41. {
  42. char * strdata = new char[3000000];
  43. int nsendlen = 10000;
  44. while(1)
  45. {
  46. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  47. qint64 ntime = QDateTime::currentMSecsSinceEpoch();
  48. memcpy(strdata,&ntime,8);
  49. if(nsendlen<2000000)nsendlen = nsendlen*1001/1000;
  50. iv::modulecomm::ModuleSendMsg(gpa,strdata,nsendlen);
  51. t1 = std::chrono::steady_clock::now();
  52. // qDebug("send msg.");
  53. }
  54. }
  55. #include <future>
  56. void testfunc(int a)
  57. {
  58. a = a+1;
  59. std::cout<<"a = "<<a<<std::endl;
  60. }
  61. void testfunc2(double a)
  62. {
  63. a = a+1.1;
  64. std::cout<<"a = "<<a<<std::endl;
  65. }
  66. iv::modulecommext::modulecommmsg<iv::testmodulecommext> * gmsgtestsend,* gmsgtestrecv;
  67. void ListenProto(google::protobuf::Message & xmsg)
  68. {
  69. iv::testmodulecommext * pmsg = (iv::testmodulecommext *)&xmsg;
  70. qDebug("time : %lld",pmsg->time());
  71. }
  72. void threadsendproto()
  73. {
  74. while(1)
  75. {
  76. iv::testmodulecommext xmsg;
  77. xmsg.set_a(1);
  78. xmsg.set_b(2);
  79. xmsg.set_time(QDateTime::currentMSecsSinceEpoch());
  80. gmsgtestsend->ModuleSendMsg(xmsg);
  81. std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  82. }
  83. }
  84. int main(int argc, char *argv[])
  85. {
  86. QCoreApplication a(argc, argv);
  87. // auto x1 = std::chrono::system_clock::now();
  88. // std::cout << "秒:"<<std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count() << std::endl;
  89. // std::cout << "毫秒:" << std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count()<<std::endl;
  90. // std::cout << "微秒:" << std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count() << std::endl;
  91. // std::cout << "纳秒:" << std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch()).count() << std::endl;
  92. // qint64 naotime = std::chrono::system_clock::now().time_since_epoch().count();
  93. // int j;
  94. // for(j=0;j<10000000;j++)
  95. // {
  96. // }
  97. // auto x2 = std::chrono::system_clock::now();
  98. // double elapsed =
  99. // std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now() - x1).count();
  100. // qDebug("ela is %f ",elapsed);
  101. // return 0;
  102. // gpa = iv::modulecomm::RegisterSend("test1",3000000,1,iv::modulecomm::ModuleComm_INTERIOR);
  103. // iv::modulecomm::RegisterRecv("test1",testcall,iv::modulecomm::ModuleComm_INTERIOR);
  104. // std::thread * xthread = new std::thread(threadsend);
  105. // QSharedMemory * pmem = new QSharedMemory("hello");
  106. // pmem->create(1000);
  107. // qDebug("create");
  108. // pmem->lock();
  109. // qDebug("lock");
  110. // gpa = iv::modulecomm::RegisterSend("test1",1000,1);
  111. // gpa = iv::modulecomm::RegisterSend("test1");
  112. // iv::modulecomm::RegisterRecv("test1",testcall2);
  113. // gpa = iv::modulecomm::RegisterSend("test1",1000,1,iv::modulecomm::ModuleComm_FASTRTPS);
  114. // iv::modulecomm::RegisterRecv("test1",testcall2,iv::modulecomm::ModuleComm_FASTRTPS);
  115. // iv::modulecomm::RegisterRecv("test1",testcall2);
  116. // std::thread * xthread = new std::thread(threadsend2);
  117. gmsgtestsend = new iv::modulecommext::modulecommmsg<iv::testmodulecommext>();
  118. gmsgtestsend->RegisterSend("testsendproto");
  119. gmsgtestrecv = new iv::modulecommext::modulecommmsg<iv::testmodulecommext>();
  120. gmsgtestrecv->RegisterRecv("testsendproto",ListenProto);
  121. std::thread * xthreadproto = new std::thread(threadsendproto);
  122. // extfunc<int>::pfun x = &testfunc;
  123. // extfunc<double>::pfun x2 = &testfunc2;
  124. // (*x)(1);
  125. // (*x2)(2.0);
  126. return a.exec();
  127. }