TestPushServer.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include "TestPushServer.h"
  2. #include "TestPushServantImp.h"
  3. using namespace std;
  4. TestPushServer g_app;
  5. /////////////////////////////////////////////////////////////////
  6. static TC_NetWorkBuffer::PACKET_TYPE parse(TC_NetWorkBuffer &in, vector<char> &out)
  7. {
  8. size_t len = sizeof(tars::Int32);
  9. if (in.getBufferLength() < len)
  10. {
  11. return TC_NetWorkBuffer::PACKET_LESS;
  12. }
  13. string header;
  14. in.getHeader(len, header);
  15. assert(header.size() == len);
  16. tars::Int32 iHeaderLen = 0;
  17. ::memcpy(&iHeaderLen, header.c_str(), sizeof(tars::Int32));
  18. iHeaderLen = ntohl(iHeaderLen);
  19. if (iHeaderLen > 100000 || iHeaderLen < sizeof(unsigned int))
  20. {
  21. throw TarsDecodeException("packet length too long or too short,len:" + TC_Common::tostr(iHeaderLen));
  22. }
  23. if (in.getBufferLength() < (uint32_t)iHeaderLen)
  24. {
  25. return TC_NetWorkBuffer::PACKET_LESS;
  26. }
  27. in.getHeader(iHeaderLen, out);
  28. in.moveHeader(iHeaderLen);
  29. return TC_NetWorkBuffer::PACKET_FULL;
  30. }
  31. void
  32. TestPushServer::initialize()
  33. {
  34. //initialize application here:
  35. //...
  36. addServant<TestPushServantImp>(ServerConfig::Application + "." + ServerConfig::ServerName + ".TestPushServantObj");
  37. addServantProtocol("TestApp.PushServer.TestPushServantObj", parse);
  38. pushThread.start();
  39. }
  40. /////////////////////////////////////////////////////////////////
  41. void
  42. TestPushServer::destroyApp()
  43. {
  44. //destroy application here:
  45. //...
  46. pushThread.terminate();
  47. pushThread.getThreadControl().join();
  48. }
  49. /////////////////////////////////////////////////////////////////
  50. int
  51. main(int argc, char* argv[])
  52. {
  53. try
  54. {
  55. g_app.main(argc, argv);
  56. g_app.waitForShutdown();
  57. }
  58. catch (std::exception& e)
  59. {
  60. cerr << "std::exception:" << e.what() << std::endl;
  61. }
  62. catch (...)
  63. {
  64. cerr << "unknown exception." << std::endl;
  65. }
  66. return -1;
  67. }
  68. /////////////////////////////////////////////////////////////////