Http2Imp.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 "Http2Imp.h"
  17. #include "servant/Application.h"
  18. using namespace std;
  19. TC_SpinLock Http2Imp::_mutex;
  20. unordered_map<int32_t, shared_ptr<TC_Http2Server>> Http2Imp::_http2;
  21. //////////////////////////////////////////////////////
  22. void Http2Imp::initialize()
  23. {
  24. //initialize servant here:
  25. //...
  26. }
  27. //////////////////////////////////////////////////////
  28. void Http2Imp::destroy()
  29. {
  30. //destroy servant here:
  31. //...
  32. }
  33. int Http2Imp::doRequest(TarsCurrentPtr current, vector<char> &buffer)
  34. {
  35. shared_ptr<TC_Http2Server> session = getHttp2(current->getUId());
  36. vector<shared_ptr<TC_Http2Server::Http2Context>> contexts = session->decodeRequest();
  37. for(size_t i = 0; i< contexts.size(); ++i)
  38. {
  39. shared_ptr<TC_Http2Server::Http2Context> context = contexts[i];
  40. vector<char> data;
  41. context->response.setHeader("X-Header", "TARS");
  42. context->response.setResponse(200, "OK", context->request.getContent());
  43. int ret = session->encodeResponse(context, data);
  44. if(ret != 0)
  45. {
  46. cout << "encodeResponse error:" << session->getErrMsg() << endl;
  47. }
  48. // cout << context->request.getContent() << endl;
  49. buffer.insert(buffer.end(), data.begin(), data.end());
  50. }
  51. return 0;
  52. }
  53. int Http2Imp::doClose(TarsCurrentPtr current)
  54. {
  55. delHttp2(current->getUId());
  56. return 0;
  57. }