HttpServer.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #include "HttpServer.h"
  17. #include "Http2Imp.h"
  18. #include "util/tc_http2.h"
  19. using namespace std;
  20. HttpServer g_app;
  21. TC_NetWorkBuffer::PACKET_TYPE parseHttp2(TC_NetWorkBuffer&in, vector<char> &out)
  22. {
  23. TC_Http2Server*sessionPtr = (TC_Http2Server*)(in.getContextData());
  24. if(sessionPtr == NULL)
  25. {
  26. shared_ptr<TC_Http2Server> session(new TC_Http2Server());
  27. in.setContextData(session.get());
  28. session->settings(3000);
  29. TC_EpollServer::Connection *connection = (TC_EpollServer::Connection *)in.getConnection();
  30. Http2Imp::addHttp2(connection->getId(), session);
  31. sessionPtr = session.get();
  32. }
  33. return sessionPtr->parse(in, out);
  34. }
  35. void
  36. HttpServer::initialize()
  37. {
  38. //initialize application here:
  39. //...
  40. addServant<Http2Imp>(ServerConfig::Application + "." + ServerConfig::ServerName + ".Http2Obj");
  41. addServantProtocol(ServerConfig::Application + "." + ServerConfig::ServerName + ".Http2Obj", &parseHttp2);
  42. }
  43. /////////////////////////////////////////////////////////////////
  44. void
  45. HttpServer::destroyApp()
  46. {
  47. //destroy application here:
  48. //...
  49. }
  50. /////////////////////////////////////////////////////////////////
  51. int
  52. main(int argc, char* argv[])
  53. {
  54. try
  55. {
  56. g_app.main(argc, argv);
  57. g_app.waitForShutdown();
  58. }
  59. catch (std::exception& e)
  60. {
  61. cerr << "std::exception:" << e.what() << std::endl;
  62. }
  63. catch (...)
  64. {
  65. cerr << "unknown exception." << std::endl;
  66. }
  67. return -1;
  68. }
  69. /////////////////////////////////////////////////////////////////