diff options
author | Roland Reichwein <mail@reichwein.it> | 2023-01-26 19:14:05 +0100 |
---|---|---|
committer | Roland Reichwein <mail@reichwein.it> | 2023-01-26 19:14:05 +0100 |
commit | fd3da82dd7772419c01bb751e5b5cb7f198b4752 (patch) | |
tree | a22034831ec31df68b4e9cb22a1c51949e41b613 /websocket.cpp | |
parent | 9b0320c8b47c43adce1aa5d9821b67c1fa42e1d6 (diff) |
websocket bugfix: socket leak
Diffstat (limited to 'websocket.cpp')
-rw-r--r-- | websocket.cpp | 30 |
1 files changed, 24 insertions, 6 deletions
diff --git a/websocket.cpp b/websocket.cpp index 68ff194..d361ec1 100644 --- a/websocket.cpp +++ b/websocket.cpp @@ -65,6 +65,20 @@ public: relative_target_ = websocket_address.substr(slash_pos); } + void fail(boost::beast::error_code ec, char const* what) + { + if (ec == websocket::error::closed) + return; + + boost::beast::error_code ec2; + if (auto& ws_in{derived().ws_in()}; ws_in.is_open()) + ws_in.close(beast::websocket::close_code::going_away, ec2); + if (auto& ws_app{ws_app_}; ws_app.is_open()) + ws_app.close(beast::websocket::close_code::going_away, ec2); + + std::cerr << what << ": " << ec.message() << "\n"; + } + // // The initial setup path // @@ -181,8 +195,10 @@ private: if (ec == websocket::error::closed) return; - if (ec) - fail(ec, "read in"); + if (ec) { + // not error::closed as above, but no other strategy known + return fail(ec, "read in"); + } ws_app_.text(derived().ws_in().got_text()); @@ -202,7 +218,7 @@ private: boost::ignore_unused(bytes_transferred); if (ec) - fail(ec, "write app"); + return fail(ec, "write app"); buffer_in_.consume(buffer_in_.size()); @@ -231,8 +247,10 @@ private: if (ec == websocket::error::closed) return; - if (ec) - fail(ec, "read app"); + if (ec) { + // not error::closed as above, but no other strategy known + return fail(ec, "read app"); + } do_write_out(); } @@ -251,7 +269,7 @@ private: { boost::ignore_unused(bytes_transferred); - if(ec) + if (ec) return fail(ec, "write out"); // Clear the buffer |