11cb0ef41Sopenharmony_ci#ifndef SRC_REQ_WRAP_H_
21cb0ef41Sopenharmony_ci#define SRC_REQ_WRAP_H_
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci#include "async_wrap.h"
71cb0ef41Sopenharmony_ci#include "util.h"
81cb0ef41Sopenharmony_ci#include "v8.h"
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_cinamespace node {
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciclass Environment;
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciclass ReqWrapBase {
151cb0ef41Sopenharmony_ci public:
161cb0ef41Sopenharmony_ci  explicit inline ReqWrapBase(Environment* env);
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci  virtual ~ReqWrapBase() = default;
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci  virtual void Cancel() = 0;
211cb0ef41Sopenharmony_ci  virtual AsyncWrap* GetAsyncWrap() = 0;
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci private:
241cb0ef41Sopenharmony_ci  friend int GenDebugSymbols();
251cb0ef41Sopenharmony_ci  friend class Environment;
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci  ListNode<ReqWrapBase> req_wrap_queue_;
281cb0ef41Sopenharmony_ci};
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_citemplate <typename T>
311cb0ef41Sopenharmony_ciclass ReqWrap : public AsyncWrap, public ReqWrapBase {
321cb0ef41Sopenharmony_ci public:
331cb0ef41Sopenharmony_ci  inline ReqWrap(Environment* env,
341cb0ef41Sopenharmony_ci                 v8::Local<v8::Object> object,
351cb0ef41Sopenharmony_ci                 AsyncWrap::ProviderType provider);
361cb0ef41Sopenharmony_ci  inline ~ReqWrap() override;
371cb0ef41Sopenharmony_ci  // Call this after the req has been dispatched, if that did not already
381cb0ef41Sopenharmony_ci  // happen by using Dispatch().
391cb0ef41Sopenharmony_ci  inline void Dispatched();
401cb0ef41Sopenharmony_ci  // Call this after a request has finished, if re-using this object is planned.
411cb0ef41Sopenharmony_ci  inline void Reset();
421cb0ef41Sopenharmony_ci  T* req() { return &req_; }
431cb0ef41Sopenharmony_ci  inline void Cancel() final;
441cb0ef41Sopenharmony_ci  inline AsyncWrap* GetAsyncWrap() override;
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  static ReqWrap* from_req(T* req);
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci  template <typename LibuvFunction, typename... Args>
491cb0ef41Sopenharmony_ci  inline int Dispatch(LibuvFunction fn, Args... args);
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci private:
521cb0ef41Sopenharmony_ci  friend int GenDebugSymbols();
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci  // Adding `friend struct MakeLibuvRequestCallback` is not enough anymore
551cb0ef41Sopenharmony_ci  // for some reason. Consider this private.
561cb0ef41Sopenharmony_ci public:
571cb0ef41Sopenharmony_ci  typedef void (*callback_t)();
581cb0ef41Sopenharmony_ci  callback_t original_callback_ = nullptr;
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci protected:
611cb0ef41Sopenharmony_ci  // req_wrap_queue_ needs to be at a fixed offset from the start of the class
621cb0ef41Sopenharmony_ci  // because it is used by ContainerOf to calculate the address of the embedding
631cb0ef41Sopenharmony_ci  // ReqWrap. ContainerOf compiles down to simple, fixed pointer arithmetic. It
641cb0ef41Sopenharmony_ci  // is also used by src/node_postmortem_metadata.cc to calculate offsets and
651cb0ef41Sopenharmony_ci  // generate debug symbols for ReqWrap, which assumes that the position of
661cb0ef41Sopenharmony_ci  // members in memory are predictable. sizeof(req_) depends on the type of T,
671cb0ef41Sopenharmony_ci  // so req_wrap_queue_ would no longer be at a fixed offset if it came after
681cb0ef41Sopenharmony_ci  // req_. For more information please refer to
691cb0ef41Sopenharmony_ci  // `doc/contributing/node-postmortem-support.md`
701cb0ef41Sopenharmony_ci  T req_;
711cb0ef41Sopenharmony_ci};
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci}  // namespace node
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci#endif  // SRC_REQ_WRAP_H_
78