TestPushServer.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include "TestPushServer.h"
  2. #include "TestPushServantImp.h"
  3. using namespace std;
  4. TestPushServer g_app;
  5. /////////////////////////////////////////////////////////////////
  6. static int parse(string &in, string &out)
  7. {
  8. if(in.length() < sizeof(unsigned int))
  9. {
  10. return TC_EpollServer::PACKET_LESS;
  11. }
  12. unsigned int iHeaderLen;
  13. memcpy(&iHeaderLen, in.c_str(), sizeof(unsigned int));
  14. iHeaderLen = ntohl(iHeaderLen);
  15. if(iHeaderLen < (unsigned int)(sizeof(unsigned int))|| iHeaderLen > 1000000)
  16. {
  17. return TC_EpollServer::PACKET_ERR;
  18. }
  19. if((unsigned int)in.length() < iHeaderLen)
  20. {
  21. return TC_EpollServer::PACKET_LESS;
  22. }
  23. out = in.substr(0, iHeaderLen);
  24. in = in.substr(iHeaderLen);
  25. return TC_EpollServer::PACKET_FULL;
  26. }
  27. void
  28. TestPushServer::initialize()
  29. {
  30. //initialize application here:
  31. //...
  32. addServant<TestPushServantImp>(ServerConfig::Application + "." + ServerConfig::ServerName + ".TestPushServantObj");
  33. addServantProtocol("Test.TestPushServer.TestPushServantObj", parse);
  34. pushThread.start();
  35. }
  36. /////////////////////////////////////////////////////////////////
  37. void
  38. TestPushServer::destroyApp()
  39. {
  40. //destroy application here:
  41. //...
  42. pushThread.terminate();
  43. pushThread.getThreadControl().join();
  44. }
  45. /////////////////////////////////////////////////////////////////
  46. int
  47. main(int argc, char* argv[])
  48. {
  49. try
  50. {
  51. g_app.main(argc, argv);
  52. g_app.waitForShutdown();
  53. }
  54. catch (std::exception& e)
  55. {
  56. cerr << "std::exception:" << e.what() << std::endl;
  57. }
  58. catch (...)
  59. {
  60. cerr << "unknown exception." << std::endl;
  61. }
  62. return -1;
  63. }
  64. /////////////////////////////////////////////////////////////////