| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 889f42a commit 5390d7e
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -69,8 +69,45 @@ Http2Options::Http2Options(Environment* env) { | |||
| 69 | 69 | } | |
| 70 | 70 | } | |
| 71 | 71 | ||
| 72 | - void Http2Session::OnFreeSession() { | ||
| 73 | - ::delete this; | ||
| 72 | + | ||
| 73 | + Http2Session::Http2Session(Environment* env, | ||
| 74 | + Local<Object> wrap, | ||
| 75 | + nghttp2_session_type type) | ||
| 76 | + : AsyncWrap(env, wrap, AsyncWrap::PROVIDER_HTTP2SESSION), | ||
| 77 | + StreamBase(env) { | ||
| 78 | + Wrap(object(), this); | ||
| 79 | + | ||
| 80 | + Http2Options opts(env); | ||
| 81 | + | ||
| 82 | + padding_strategy_ = opts.GetPaddingStrategy(); | ||
| 83 | + | ||
| 84 | + Init(type, *opts); | ||
| 85 | + | ||
| 86 | + // For every node::Http2Session instance, there is a uv_prepare_t handle | ||
| 87 | + // whose callback is triggered on every tick of the event loop. When | ||
| 88 | + // run, nghttp2 is prompted to send any queued data it may have stored. | ||
| 89 | + prep_ = new uv_prepare_t(); | ||
| 90 | + uv_prepare_init(env->event_loop(), prep_); | ||
| 91 | + prep_->data = static_cast<void*>(this); | ||
| 92 | + uv_prepare_start(prep_, [](uv_prepare_t* t) { | ||
| 93 | + Http2Session* session = static_cast<Http2Session*>(t->data); | ||
| 94 | + session->SendPendingData(); | ||
| 95 | + }); | ||
| 96 | + } | ||
| 97 | + | ||
| 98 | + Http2Session::~Http2Session() { | ||
| 99 | + CHECK_EQ(false, persistent().IsEmpty()); | ||
| 100 | + ClearWrap(object()); | ||
| 101 | + persistent().Reset(); | ||
| 102 | + CHECK_EQ(true, persistent().IsEmpty()); | ||
| 103 | + | ||
| 104 | + // Stop the loop | ||
| 105 | + CHECK_EQ(uv_prepare_stop(prep_), 0); | ||
| 106 | + auto prep_close = [](uv_handle_t* handle) { | ||
| 107 | + delete reinterpret_cast<uv_prepare_t*>(handle); | ||
| 108 | + }; | ||
| 109 | + uv_close(reinterpret_cast<uv_handle_t*>(prep_), prep_close); | ||
| 110 | + prep_ = nullptr; | ||
| 74 | 111 | } | |
| 75 | 112 | ||
| 76 | 113 | ssize_t Http2Session::OnMaxFrameSizePadding(size_t frameLen, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -343,24 +343,8 @@ class Http2Session : public AsyncWrap, | |||
| 343 | 343 | public: | |
| 344 | 344 | Http2Session(Environment* env, | |
| 345 | 345 | Local<Object> wrap, | |
| 346 | - nghttp2_session_type type) : | ||
| 347 | - AsyncWrap(env, wrap, AsyncWrap::PROVIDER_HTTP2SESSION), | ||
| 348 | - StreamBase(env) { | ||
| 349 | - Wrap(object(), this); | ||
| 350 | - | ||
| 351 | - Http2Options opts(env); | ||
| 352 | - | ||
| 353 | - padding_strategy_ = opts.GetPaddingStrategy(); | ||
| 354 | - | ||
| 355 | - Init(env->event_loop(), type, *opts); | ||
| 356 | - } | ||
| 357 | - | ||
| 358 | - ~Http2Session() override { | ||
| 359 | - CHECK_EQ(false, persistent().IsEmpty()); | ||
| 360 | - ClearWrap(object()); | ||
| 361 | - persistent().Reset(); | ||
| 362 | - CHECK_EQ(true, persistent().IsEmpty()); | ||
| 363 | - } | ||
| 346 | + nghttp2_session_type type); | ||
| 347 | + ~Http2Session() override; | ||
| 364 | 348 | ||
| 365 | 349 | static void OnStreamAllocImpl(size_t suggested_size, | |
| 366 | 350 | uv_buf_t* buf, | |
@@ -369,9 +353,8 @@ class Http2Session : public AsyncWrap, | |||
| 369 | 353 | const uv_buf_t* bufs, | |
| 370 | 354 | uv_handle_type pending, | |
| 371 | 355 | void* ctx); | |
| 372 | - protected: | ||
| 373 | - void OnFreeSession() override; | ||
| 374 | 356 | ||
| 357 | + protected: | ||
| 375 | 358 | ssize_t OnMaxFrameSizePadding(size_t frameLength, | |
| 376 | 359 | size_t maxPayloadLen); | |
| 377 | 360 | ||
@@ -449,6 +432,9 @@ class Http2Session : public AsyncWrap, | |||
| 449 | 432 | return 0; | |
| 450 | 433 | } | |
| 451 | 434 | ||
| 435 | + uv_loop_t* event_loop() const override { | ||
| 436 | + return env()->event_loop(); | ||
| 437 | + } | ||
| 452 | 438 | public: | |
| 453 | 439 | void Consume(Local<External> external); | |
| 454 | 440 | void Unconsume(); | |
@@ -496,6 +482,7 @@ class Http2Session : public AsyncWrap, | |||
| 496 | 482 | ||
| 497 | 483 | // use this to allow timeout tracking during long-lasting writes | |
| 498 | 484 | uint32_t chunks_sent_since_last_write_ = 0; | |
| 485 | + uv_prepare_t* prep_ = nullptr; | ||
| 499 | 486 | ||
| 500 | 487 | char stream_buf_[kAllocBufferSize]; | |
| 501 | 488 | }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -193,7 +193,8 @@ inline ssize_t Nghttp2Session::OnStreamReadFD(nghttp2_session* session, | |||
| 193 | 193 | uv_fs_t read_req; | |
| 194 | 194 | ||
| 195 | 195 | if (length > 0) { | |
| 196 | - numchars = uv_fs_read(handle->loop_, | ||
| 196 | + // TODO(addaleax): Never use synchronous I/O on the main thread. | ||
| 197 | + numchars = uv_fs_read(handle->event_loop(), | ||
| 197 | 198 | &read_req, | |
| 198 | 199 | fd, &data, 1, | |
| 199 | 200 | offset, nullptr); | |
@@ -541,11 +542,9 @@ inline void Nghttp2Session::SendPendingData() { | |||
| 541 | 542 | // Initialize the Nghttp2Session handle by creating and | |
| 542 | 543 | // assigning the Nghttp2Session instance and associated | |
| 543 | 544 | // uv_loop_t. | |
| 544 | - inline int Nghttp2Session::Init(uv_loop_t* loop, | ||
| 545 | - const nghttp2_session_type type, | ||
| 546 | - nghttp2_option* options, | ||
| 547 | - nghttp2_mem* mem) { | ||
| 548 | - loop_ = loop; | ||
| 545 | + inline int Nghttp2Session::Init(const nghttp2_session_type type, | ||
| 546 | + nghttp2_option* options, | ||
| 547 | + nghttp2_mem* mem) { | ||
| 549 | 548 | session_type_ = type; | |
| 550 | 549 | DEBUG_HTTP2("Nghttp2Session %s: initializing session\n", TypeName()); | |
| 551 | 550 | destroying_ = false; | |
@@ -581,14 +580,6 @@ inline int Nghttp2Session::Init(uv_loop_t* loop, | |||
| 581 | 580 | nghttp2_option_del(opts); | |
| 582 | 581 | } | |
| 583 | 582 | ||
| 584 | - // For every node::Http2Session instance, there is a uv_prep_t handle | ||
| 585 | - // whose callback is triggered on every tick of the event loop. When | ||
| 586 | - // run, nghttp2 is prompted to send any queued data it may have stored. | ||
| 587 | - uv_prepare_init(loop_, &prep_); | ||
| 588 | - uv_prepare_start(&prep_, [](uv_prepare_t* t) { | ||
| 589 | - Nghttp2Session* session = ContainerOf(&Nghttp2Session::prep_, t); | ||
| 590 | - session->SendPendingData(); | ||
| 591 | - }); | ||
| 592 | 583 | return ret; | |
| 593 | 584 | } | |
| 594 | 585 | ||
@@ -601,19 +592,9 @@ inline int Nghttp2Session::Free() { | |||
| 601 | 592 | CHECK(session_ != nullptr); | |
| 602 | 593 | #endif | |
| 603 | 594 | DEBUG_HTTP2("Nghttp2Session %s: freeing session\n", TypeName()); | |
| 604 | - // Stop the loop | ||
| 605 | - CHECK_EQ(uv_prepare_stop(&prep_), 0); | ||
| 606 | - auto PrepClose = [](uv_handle_t* handle) { | ||
| 607 | - Nghttp2Session* session = | ||
| 608 | - ContainerOf(&Nghttp2Session::prep_, | ||
| 609 | - reinterpret_cast<uv_prepare_t*>(handle)); | ||
| 610 | - session->OnFreeSession(); | ||
| 611 | - }; | ||
| 612 | - uv_close(reinterpret_cast<uv_handle_t*>(&prep_), PrepClose); | ||
| 613 | 595 | nghttp2_session_terminate_session(session_, NGHTTP2_NO_ERROR); | |
| 614 | 596 | nghttp2_session_del(session_); | |
| 615 | 597 | session_ = nullptr; | |
| 616 | - loop_ = nullptr; | ||
| 617 | 598 | DEBUG_HTTP2("Nghttp2Session %s: session freed\n", TypeName()); | |
| 618 | 599 | return 1; | |
| 619 | 600 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -93,7 +93,6 @@ class Nghttp2Session { | |||
| 93 | 93 | public: | |
| 94 | 94 | // Initializes the session instance | |
| 95 | 95 | inline int Init( | |
| 96 | - uv_loop_t*, | ||
| 97 | 96 | const nghttp2_session_type type = NGHTTP2_SESSION_SERVER, | |
| 98 | 97 | nghttp2_option* options = nullptr, | |
| 99 | 98 | nghttp2_mem* mem = nullptr); | |
@@ -176,7 +175,6 @@ class Nghttp2Session { | |||
| 176 | 175 | int error_code) {} | |
| 177 | 176 | virtual ssize_t GetPadding(size_t frameLength, | |
| 178 | 177 | size_t maxFrameLength) { return 0; } | |
| 179 | - virtual void OnFreeSession() {} | ||
| 180 | 178 | virtual void AllocateSend(uv_buf_t* buf) = 0; | |
| 181 | 179 | ||
| 182 | 180 | virtual bool HasGetPaddingCallback() { return false; } | |
@@ -200,8 +198,11 @@ class Nghttp2Session { | |||
| 200 | 198 | virtual void OnTrailers(Nghttp2Stream* stream, | |
| 201 | 199 | const SubmitTrailers& submit_trailers) {} | |
| 202 | 200 | ||
| 203 | - private: | ||
| 204 | 201 | inline void SendPendingData(); | |
| 202 | + | ||
| 203 | + virtual uv_loop_t* event_loop() const = 0; | ||
| 204 | + | ||
| 205 | + private: | ||
| 205 | 206 | inline void HandleHeadersFrame(const nghttp2_frame* frame); | |
| 206 | 207 | inline void HandlePriorityFrame(const nghttp2_frame* frame); | |
| 207 | 208 | inline void HandleDataFrame(const nghttp2_frame* frame); | |
@@ -282,8 +283,6 @@ class Nghttp2Session { | |||
| 282 | 283 | static Callbacks callback_struct_saved[2]; | |
| 283 | 284 | ||
| 284 | 285 | nghttp2_session* session_; | |
| 285 | - uv_loop_t* loop_; | ||
| 286 | - uv_prepare_t prep_; | ||
| 287 | 286 | nghttp2_session_type session_type_; | |
| 288 | 287 | std::unordered_map<int32_t, Nghttp2Stream*> streams_; | |
| 289 | 288 | bool destroying_ = false; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments