HelloServer.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "HelloServer.h"
  2. #include "HelloImp.h"
  3. #include "TranImp.h"
  4. #include "HttpImp.h"
  5. #include "CustomImp.h"
  6. #include "PushImp.h"
  7. #include <thread>
  8. // #include "gperftools/profiler.h"
  9. using namespace std;
  10. string g_HelloServerObj;
  11. string g_TransDstServerObj;
  12. atomic<int> g_handleDestroy = {0};
  13. static TC_NetWorkBuffer::PACKET_TYPE parse(TC_NetWorkBuffer &in, vector<char> &out)
  14. {
  15. size_t len = sizeof(tars::Int32);
  16. if (in.getBufferLength() < len)
  17. {
  18. return TC_NetWorkBuffer::PACKET_LESS;
  19. }
  20. string header;
  21. in.getHeader(len, header);
  22. assert(header.size() == len);
  23. tars::Int32 iHeaderLen = 0;
  24. ::memcpy(&iHeaderLen, header.c_str(), sizeof(tars::Int32));
  25. iHeaderLen = ntohl(iHeaderLen);
  26. if (iHeaderLen > 100000 || iHeaderLen < (int)sizeof(unsigned int))
  27. {
  28. throw TarsDecodeException("packet length too long or too short,len:" + TC_Common::tostr(iHeaderLen));
  29. }
  30. if (in.getBufferLength() < (uint32_t)iHeaderLen)
  31. {
  32. return TC_NetWorkBuffer::PACKET_LESS;
  33. }
  34. in.getHeader(iHeaderLen, out);
  35. in.moveHeader(iHeaderLen);
  36. return TC_NetWorkBuffer::PACKET_FULL;
  37. }
  38. void
  39. HelloServer::initialize()
  40. {
  41. g_HelloServerObj = ServerConfig::Application + "." + ServerConfig::ServerName +".HelloObj@" + getEpollServer()->getBindAdapter("HelloAdapter")->getEndpoint().toString();
  42. g_TransDstServerObj = ServerConfig::Application + "." + ServerConfig::ServerName +".TransDstObj@" + getEpollServer()->getBindAdapter("TransDstAdapter")->getEndpoint().toString();
  43. addServant<HelloImp>(ServerConfig::Application + "." + ServerConfig::ServerName +".HelloObj");
  44. addServant<HelloImp>(ServerConfig::Application + "." + ServerConfig::ServerName +".TransObj");
  45. addServant<HelloImp>(ServerConfig::Application + "." + ServerConfig::ServerName +".HelloTimeoutObj");
  46. addServant<HelloImp>(ServerConfig::Application + "." + ServerConfig::ServerName +".HelloNoTimeoutObj");
  47. addServant<HelloImp>(ServerConfig::Application + "." + ServerConfig::ServerName +".Ipv6Obj");
  48. addServant<HttpImp>(ServerConfig::Application + "." + ServerConfig::ServerName +".HttpObj");
  49. addServantProtocol(ServerConfig::Application + "." + ServerConfig::ServerName + ".HttpObj", &TC_NetWorkBuffer::parseHttp);
  50. addServant<HttpImp>(ServerConfig::Application + "." + ServerConfig::ServerName +".HttpsObj");
  51. addServantProtocol(ServerConfig::Application + "." + ServerConfig::ServerName + ".HttpsObj", &TC_NetWorkBuffer::parseHttp);
  52. addServant<HelloImp>(ServerConfig::Application + "." + ServerConfig::ServerName +".TransDstObj");
  53. addServant<TranImp>(ServerConfig::Application + "." + ServerConfig::ServerName +".TransWupObj");
  54. //设置服务的协议解析器, 对服务器端而言, 解析器的目的就是识别包长度
  55. //parseStream,表示第0个字节开始,类型是uint32_t, 字节序, 这个字段表示包的长度
  56. TC_NetWorkBuffer::protocol_functor func = AppProtocol::parseStream<0, uint32_t, true>;
  57. addServantProtocol(ServerConfig::Application + "." + ServerConfig::ServerName +".TransWupObj", func);
  58. addServant<CustomImp>(ServerConfig::Application + "." + ServerConfig::ServerName + ".CustomObj");
  59. addServantProtocol(ServerConfig::Application + "." + ServerConfig::ServerName + ".CustomObj", parse);
  60. addServant<HelloImp>(ServerConfig::Application + "." + ServerConfig::ServerName + ".SSLObj");
  61. addServant<HelloImp>(ServerConfig::Application + "." + ServerConfig::ServerName + ".SSL1Obj");
  62. addServant<HelloImp>(ServerConfig::Application + "." + ServerConfig::ServerName + ".SSL2Obj");
  63. addServant<HelloImp>(ServerConfig::Application + "." + ServerConfig::ServerName + ".SSL3Obj");
  64. addServant<HelloImp>(ServerConfig::Application + "." + ServerConfig::ServerName + ".AuthObj");
  65. addServant<HelloImp>(ServerConfig::Application + "." + ServerConfig::ServerName + ".UdpObj");
  66. addServant<HelloImp>(ServerConfig::Application + "." + ServerConfig::ServerName + ".UdpIpv6Obj");
  67. addServant<PushImp>(ServerConfig::Application + "." + ServerConfig::ServerName + ".PushObj");
  68. addServantProtocol(ServerConfig::Application + "." + ServerConfig::ServerName + ".PushObj", parse);
  69. pushThread = new PushInfoThread();
  70. pushThread->start();
  71. }
  72. /////////////////////////////////////////////////////////////////
  73. void HelloServer::destroyApp()
  74. {
  75. //destroy application here:
  76. //...
  77. if(pushThread)
  78. {
  79. pushThread->terminate();
  80. pushThread->getThreadControl().join();
  81. delete pushThread;
  82. }
  83. // LOG_CONSOLE_DEBUG << endl;
  84. // ProfilerStop();
  85. }
  86. void HelloServer::run()
  87. {
  88. this->waitForShutdown();
  89. }