details.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #pragma once
  2. #include "neh.h"
  3. #include <library/cpp/neh/utils.h>
  4. #include <library/cpp/http/io/headers.h>
  5. #include <util/generic/singleton.h>
  6. #include <library/cpp/deprecated/atomic/atomic.h>
  7. namespace NNeh {
  8. class TNotifyHandle: public THandle {
  9. public:
  10. inline TNotifyHandle(IOnRecv* r, const TMessage& msg, TStatCollector* s = nullptr) noexcept
  11. : THandle(r, s)
  12. , Msg_(msg)
  13. , StartTime_(TInstant::Now())
  14. {
  15. }
  16. void NotifyResponse(const TString& resp, const TString& firstLine = {}, const THttpHeaders& headers = Default<THttpHeaders>()) {
  17. Notify(new TResponse(Msg_, resp, ExecDuration(), firstLine, headers));
  18. }
  19. void NotifyError(const TString& errorText) {
  20. Notify(TResponse::FromError(Msg_, new TError(errorText), ExecDuration()));
  21. }
  22. void NotifyError(TErrorRef error) {
  23. Notify(TResponse::FromError(Msg_, error, ExecDuration()));
  24. }
  25. /** Calls when asnwer is received and reponse has headers and first line.
  26. */
  27. void NotifyError(TErrorRef error, const TString& data, const TString& firstLine, const THttpHeaders& headers) {
  28. Notify(TResponse::FromError(Msg_, error, data, ExecDuration(), firstLine, headers));
  29. }
  30. const TMessage& Message() const noexcept {
  31. return Msg_;
  32. }
  33. private:
  34. inline TDuration ExecDuration() const {
  35. TInstant now = TInstant::Now();
  36. if (now > StartTime_) {
  37. return now - StartTime_;
  38. }
  39. return TDuration::Zero();
  40. }
  41. const TMessage Msg_;
  42. const TInstant StartTime_;
  43. };
  44. typedef TIntrusivePtr<TNotifyHandle> TNotifyHandleRef;
  45. class TSimpleHandle: public TNotifyHandle {
  46. public:
  47. inline TSimpleHandle(IOnRecv* r, const TMessage& msg, TStatCollector* s = nullptr) noexcept
  48. : TNotifyHandle(r, msg, s)
  49. , SendComplete_(false)
  50. , Canceled_(false)
  51. {
  52. }
  53. bool MessageSendedCompletely() const noexcept override {
  54. return SendComplete_;
  55. }
  56. void Cancel() noexcept override {
  57. Canceled_ = true;
  58. THandle::Cancel();
  59. }
  60. inline void SetSendComplete() noexcept {
  61. SendComplete_ = true;
  62. }
  63. inline bool Canceled() const noexcept {
  64. return Canceled_;
  65. }
  66. inline const TAtomicBool* CanceledPtr() const noexcept {
  67. return &Canceled_;
  68. }
  69. void ResetOnRecv() noexcept {
  70. F_ = nullptr;
  71. }
  72. private:
  73. TAtomicBool SendComplete_;
  74. TAtomicBool Canceled_;
  75. };
  76. typedef TIntrusivePtr<TSimpleHandle> TSimpleHandleRef;
  77. }