neh.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. #pragma once
  2. #include "wfmo.h"
  3. #include "stat.h"
  4. #include <library/cpp/http/io/headers.h>
  5. #include <util/generic/ptr.h>
  6. #include <util/generic/string.h>
  7. #include <util/datetime/base.h>
  8. #include <utility>
  9. namespace NNeh {
  10. struct TMessage {
  11. TMessage() = default;
  12. inline TMessage(TString addr, TString data)
  13. : Addr(std::move(addr))
  14. , Data(std::move(data))
  15. {
  16. }
  17. static TMessage FromString(TStringBuf request);
  18. TString Addr;
  19. TString Data;
  20. };
  21. using TMessageRef = TAutoPtr<TMessage>;
  22. struct TError {
  23. public:
  24. enum TType {
  25. UnknownType,
  26. Cancelled,
  27. ProtocolSpecific
  28. };
  29. TError(TString text, TType type = UnknownType, i32 code = 0, i32 systemCode = 0)
  30. : Type(std::move(type))
  31. , Code(code)
  32. , Text(text)
  33. , SystemCode(systemCode)
  34. {
  35. }
  36. TType Type = UnknownType;
  37. i32 Code = 0; // protocol specific code (example(http): 404)
  38. TString Text;
  39. i32 SystemCode = 0; // system error code
  40. };
  41. using TErrorRef = TAutoPtr<TError>;
  42. struct TResponse;
  43. using TResponseRef = TAutoPtr<TResponse>;
  44. struct TResponse {
  45. inline TResponse(TMessage req,
  46. TString data,
  47. const TDuration duration)
  48. : TResponse(std::move(req), std::move(data), duration, {} /* firstLine */, {} /* headers */, {} /* error */)
  49. {
  50. }
  51. inline TResponse(TMessage req,
  52. TString data,
  53. const TDuration duration,
  54. TString firstLine,
  55. THttpHeaders headers)
  56. : TResponse(std::move(req), std::move(data), duration, std::move(firstLine), std::move(headers), {} /* error */)
  57. {
  58. }
  59. inline TResponse(TMessage req,
  60. TString data,
  61. const TDuration duration,
  62. TString firstLine,
  63. THttpHeaders headers,
  64. TErrorRef error)
  65. : Request(std::move(req))
  66. , Data(std::move(data))
  67. , Duration(duration)
  68. , FirstLine(std::move(firstLine))
  69. , Headers(std::move(headers))
  70. , Error_(std::move(error))
  71. {
  72. }
  73. inline static TResponseRef FromErrorText(TMessage msg, TString error, const TDuration duration) {
  74. return new TResponse(std::move(msg), {} /* data */, duration, {} /* firstLine */, {} /* headers */, new TError(std::move(error)));
  75. }
  76. inline static TResponseRef FromError(TMessage msg, TErrorRef error, const TDuration duration) {
  77. return new TResponse(std::move(msg), {} /* data */, duration, {} /* firstLine */, {} /* headers */, error);
  78. }
  79. inline static TResponseRef FromError(TMessage msg, TErrorRef error, const TDuration duration,
  80. TString data, TString firstLine, THttpHeaders headers)
  81. {
  82. return new TResponse(std::move(msg), std::move(data), duration, std::move(firstLine), std::move(headers), error);
  83. }
  84. inline static TResponseRef FromError(
  85. TMessage msg,
  86. TErrorRef error,
  87. TString data,
  88. const TDuration duration,
  89. TString firstLine,
  90. THttpHeaders headers)
  91. {
  92. return new TResponse(std::move(msg), std::move(data), duration, std::move(firstLine), std::move(headers), error);
  93. }
  94. inline bool IsError() const {
  95. return Error_.Get();
  96. }
  97. inline TError::TType GetErrorType() const {
  98. return Error_.Get() ? Error_->Type : TError::UnknownType;
  99. }
  100. inline i32 GetErrorCode() const {
  101. return Error_.Get() ? Error_->Code : 0;
  102. }
  103. inline i32 GetSystemErrorCode() const {
  104. return Error_.Get() ? Error_->SystemCode : 0;
  105. }
  106. inline TString GetErrorText() const {
  107. return Error_.Get() ? Error_->Text : TString();
  108. }
  109. const TMessage Request;
  110. const TString Data;
  111. const TDuration Duration;
  112. const TString FirstLine;
  113. THttpHeaders Headers;
  114. private:
  115. THolder<TError> Error_;
  116. };
  117. class THandle;
  118. class IOnRecv {
  119. public:
  120. virtual ~IOnRecv() = default;
  121. virtual void OnNotify(THandle&) {
  122. } //callback on receive response
  123. virtual void OnEnd() {
  124. } //response was extracted by Wait() method, - OnRecv() will not be called
  125. virtual void OnRecv(THandle& resp) = 0; //callback on destroy handler
  126. };
  127. class THandle: public TThrRefBase, public TWaitHandle {
  128. public:
  129. inline THandle(IOnRecv* f, TStatCollector* s = nullptr) noexcept
  130. : F_(f)
  131. , Stat_(s)
  132. {
  133. }
  134. ~THandle() override {
  135. if (F_) {
  136. try {
  137. F_->OnRecv(*this);
  138. } catch (...) {
  139. }
  140. }
  141. }
  142. virtual bool MessageSendedCompletely() const noexcept {
  143. //TODO
  144. return true;
  145. }
  146. virtual void Cancel() noexcept {
  147. //TODO
  148. if (!!Stat_)
  149. Stat_->OnCancel();
  150. }
  151. inline const TResponse* Response() const noexcept {
  152. return R_.Get();
  153. }
  154. //method MUST be called only after success Wait() for this handle or from callback IOnRecv::OnRecv()
  155. //else exist chance for memory leak (race between Get()/Notify())
  156. inline TResponseRef Get() noexcept {
  157. return R_;
  158. }
  159. inline bool Wait(TResponseRef& msg, const TInstant deadLine) {
  160. if (WaitForOne(*this, deadLine)) {
  161. if (F_) {
  162. F_->OnEnd();
  163. F_ = nullptr;
  164. }
  165. msg = Get();
  166. return true;
  167. }
  168. return false;
  169. }
  170. inline bool Wait(TResponseRef& msg, const TDuration timeOut) {
  171. return Wait(msg, timeOut.ToDeadLine());
  172. }
  173. inline bool Wait(TResponseRef& msg) {
  174. return Wait(msg, TInstant::Max());
  175. }
  176. inline TResponseRef Wait(const TInstant deadLine) {
  177. TResponseRef ret;
  178. Wait(ret, deadLine);
  179. return ret;
  180. }
  181. inline TResponseRef Wait(const TDuration timeOut) {
  182. return Wait(timeOut.ToDeadLine());
  183. }
  184. inline TResponseRef Wait() {
  185. return Wait(TInstant::Max());
  186. }
  187. protected:
  188. inline void Notify(TResponseRef resp) {
  189. if (!!Stat_) {
  190. if (!resp || resp->IsError()) {
  191. Stat_->OnFail();
  192. } else {
  193. Stat_->OnSuccess();
  194. }
  195. }
  196. R_.Swap(resp);
  197. if (F_) {
  198. try {
  199. F_->OnNotify(*this);
  200. } catch (...) {
  201. }
  202. }
  203. Signal();
  204. }
  205. IOnRecv* F_;
  206. private:
  207. TResponseRef R_;
  208. THolder<TStatCollector> Stat_;
  209. };
  210. using THandleRef = TIntrusivePtr<THandle>;
  211. THandleRef Request(const TMessage& msg, IOnRecv* fallback);
  212. inline THandleRef Request(const TMessage& msg) {
  213. return Request(msg, nullptr);
  214. }
  215. THandleRef Request(const TString& req, IOnRecv* fallback);
  216. inline THandleRef Request(const TString& req) {
  217. return Request(req, nullptr);
  218. }
  219. class IMultiRequester {
  220. public:
  221. virtual ~IMultiRequester() = default;
  222. virtual void Add(const THandleRef& req) = 0;
  223. virtual void Del(const THandleRef& req) = 0;
  224. virtual bool Wait(THandleRef& req, TInstant deadLine) = 0;
  225. virtual bool IsEmpty() const = 0;
  226. inline void Schedule(const TString& req) {
  227. Add(Request(req));
  228. }
  229. inline bool Wait(THandleRef& req, TDuration timeOut) {
  230. return Wait(req, timeOut.ToDeadLine());
  231. }
  232. inline bool Wait(THandleRef& req) {
  233. return Wait(req, TInstant::Max());
  234. }
  235. inline bool Wait(TResponseRef& resp, TInstant deadLine) {
  236. THandleRef req;
  237. while (Wait(req, deadLine)) {
  238. resp = req->Get();
  239. if (!!resp) {
  240. return true;
  241. }
  242. }
  243. return false;
  244. }
  245. inline bool Wait(TResponseRef& resp) {
  246. return Wait(resp, TInstant::Max());
  247. }
  248. };
  249. using IMultiRequesterRef = TAutoPtr<IMultiRequester>;
  250. IMultiRequesterRef CreateRequester();
  251. bool SetProtocolOption(TStringBuf protoOption, TStringBuf value);
  252. }