Http2Imp.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #ifndef _Http2Imp_H_
  17. #define _Http2Imp_H_
  18. #include <unordered_map>
  19. #include "servant/Application.h"
  20. #include "util/tc_spin_lock.h"
  21. #include "util/tc_http2.h"
  22. using namespace tars;
  23. using namespace std;
  24. /**
  25. *
  26. *
  27. */
  28. class Http2Imp : public Servant
  29. {
  30. public:
  31. /**
  32. *
  33. */
  34. virtual ~Http2Imp() {}
  35. /**
  36. *
  37. */
  38. virtual void initialize();
  39. /**
  40. *
  41. */
  42. virtual void destroy();
  43. /**
  44. *
  45. */
  46. int doRequest(TarsCurrentPtr current, vector<char> &buffer);
  47. /**
  48. * close connection
  49. */
  50. int doClose(TarsCurrentPtr current);
  51. static shared_ptr<TC_Http2Server> getHttp2(uint32_t uid)
  52. {
  53. TC_LockT<TC_SpinLock> lock(_mutex);
  54. auto it = _http2.find(uid);
  55. if(it != _http2.end())
  56. {
  57. return it->second;
  58. }
  59. return NULL;
  60. }
  61. static void addHttp2(uint32_t uid, const shared_ptr<TC_Http2Server> &ptr)
  62. {
  63. TC_LockT<TC_SpinLock> lock(_mutex);
  64. _http2[uid] = ptr;
  65. }
  66. static void delHttp2(uint32_t uid)
  67. {
  68. TC_LockT<TC_SpinLock> lock(_mutex);
  69. auto it = _http2.find(uid);
  70. if(it != _http2.end())
  71. {
  72. _http2.erase(it);
  73. }
  74. }
  75. protected:
  76. static TC_SpinLock _mutex;
  77. static unordered_map<int32_t, shared_ptr<TC_Http2Server>> _http2;
  78. };
  79. /////////////////////////////////////////////////////
  80. #endif