main.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include <QCoreApplication>
  2. #include <thread>
  3. #include <chrono>
  4. #include <iostream>
  5. #include <QDateTime>
  6. #include "ivservice.h"
  7. void ProcReq(std::shared_ptr<char> pstr_req,const int nreqsize,std::shared_ptr<char> & pstr_res,int & nressize)
  8. {
  9. pstr_res = std::shared_ptr<char>(new char[100]);
  10. nressize = 100;
  11. }
  12. void ProcAsync(std::shared_ptr<char> & pstr_res,int & ndatasize,int nres)
  13. {
  14. if(nres == 0)
  15. {
  16. std::cout<<"extend time."<<std::endl;
  17. }
  18. if(nres == 1)
  19. {
  20. std::cout<<"Aysnc Service return."<<std::endl;
  21. }
  22. }
  23. void threadclientai(int ai)
  24. {
  25. iv::service::Client xclient("helloa");
  26. // std::this_thread::sleep_for(std::chrono::milliseconds(3));
  27. // std::cout<<"Send Time : "<<QDateTime::currentMSecsSinceEpoch()<<std::endl;
  28. std::shared_ptr<char> pstr_req = std::shared_ptr<char>(new char[1000000]);
  29. std::shared_ptr<char> pstr_res;
  30. int nressize = 0;
  31. int nsendsize = 100;
  32. while(nsendsize < 1000000)
  33. {
  34. std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  35. nsendsize = nsendsize * 1;
  36. if(nsendsize >= 1000000)break;
  37. std::cout<<ai<<" Send Time : "<<QDateTime::currentMSecsSinceEpoch()<<std::endl;
  38. if(iv::service::Client::HAVE_RES == xclient.SendRequest(pstr_req,nsendsize,pstr_res,nressize))
  39. {
  40. std::cout<<ai<<" Res Time: "<<QDateTime::currentMSecsSinceEpoch()<<std::endl;
  41. std::cout<<ai<<" nres: "<<nressize<<std::endl;
  42. }
  43. else
  44. {
  45. std::cout<<ai<<" Fail Get Result."<<std::endl;
  46. }
  47. break;
  48. }
  49. }
  50. void threadclient()
  51. {
  52. iv::service::Client xclient("hellob");
  53. // std::this_thread::sleep_for(std::chrono::milliseconds(3));
  54. std::cout<<"Send Time : "<<QDateTime::currentMSecsSinceEpoch()<<std::endl;
  55. std::shared_ptr<char> pstr_req = std::shared_ptr<char>(new char[1000000]);
  56. std::shared_ptr<char> pstr_res;
  57. int nressize = 0;
  58. if(iv::service::Client::HAVE_RES == xclient.SendRequest(pstr_req,100,pstr_res,nressize))
  59. {
  60. std::cout<<"Res Time: "<<QDateTime::currentMSecsSinceEpoch()<<std::endl;
  61. std::cout<<" nres: "<<nressize<<std::endl;
  62. }
  63. else
  64. {
  65. std::cout<<" Fail Get Result."<<std::endl;
  66. }
  67. return ;
  68. int nsendsize = 100;
  69. while(nsendsize < 1000000)
  70. {
  71. std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  72. nsendsize = nsendsize * 1;
  73. if(nsendsize >= 1000000)break;
  74. std::cout<<"Send Time : "<<QDateTime::currentMSecsSinceEpoch()<<std::endl;
  75. if(iv::service::Client::HAVE_RES == xclient.SendRequest(pstr_req,nsendsize,pstr_res,nressize))
  76. {
  77. std::cout<<"Res Time: "<<QDateTime::currentMSecsSinceEpoch()<<std::endl;
  78. std::cout<<" nres: "<<nressize<<std::endl;
  79. }
  80. else
  81. {
  82. std::cout<<" Fail Get Result."<<std::endl;
  83. }
  84. }
  85. }
  86. int main(int argc, char *argv[])
  87. {
  88. QCoreApplication a(argc, argv);
  89. std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> tp =std::chrono::system_clock::now();
  90. qint64 xtime1 = std::chrono::system_clock::now().time_since_epoch().count();
  91. qint64 xtime2 = std::chrono::system_clock::now().time_since_epoch().count();
  92. qint64 xtd = xtime2 - xtime1;
  93. // iv::service::Server xserver("helloa",ProcReq);
  94. // std::this_thread::sleep_for(std::chrono::milliseconds(300));
  95. // std::thread * pthread_a = new std::thread(threadclientai,1);
  96. // std::thread * pthread_b = new std::thread(threadclientai,2);
  97. iv::service::Client xaync("async");
  98. std::shared_ptr<char> pstr_async = std::shared_ptr<char>(new char[1000]);
  99. xaync.AsyncSendRequest(pstr_async,100,ProcAsync);
  100. std::this_thread::sleep_for(std::chrono::milliseconds(10000));
  101. iv::service::Server xserver("async",ProcReq);
  102. // std::thread * pthread = new std::thread(threadclient);
  103. // int i;
  104. // for(i=0;i<100;i++)
  105. // {
  106. // std::this_thread::sleep_for(std::chrono::milliseconds(3000));
  107. // pthread = new std::thread(threadclient);
  108. // }
  109. return a.exec();
  110. }