CustomServer.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include "CustomServer.h"
  2. #include "CustomServantImp.h"
  3. using namespace std;
  4. CustomServer 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 < (int)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. CustomServer::initialize()
  33. {
  34. //initialize application here:
  35. //...
  36. addServant<CustomServantImp>(ServerConfig::Application + "." + ServerConfig::ServerName + ".CustomServantObj");
  37. addServantProtocol("TestApp.CustomServer.CustomServantObj", parse);
  38. }
  39. /////////////////////////////////////////////////////////////////
  40. void
  41. CustomServer::destroyApp()
  42. {
  43. //destroy application here:
  44. //...
  45. }
  46. /////////////////////////////////////////////////////////////////
  47. int
  48. main(int argc, char* argv[])
  49. {
  50. try
  51. {
  52. g_app.main(argc, argv);
  53. g_app.waitForShutdown();
  54. }
  55. catch (std::exception& e)
  56. {
  57. cerr << "std::exception:" << e.what() << std::endl;
  58. }
  59. catch (...)
  60. {
  61. cerr << "unknown exception." << std::endl;
  62. }
  63. return -1;
  64. }
  65. /////////////////////////////////////////////////////////////////