Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: server stop send websocket close message #422

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions include/cinatra/connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ class connection : public base_connection,
void async_close() {
auto self = this->shared_from_this();
asio::dispatch(socket_.get_executor(), [this, self] {
active_close_websocket();
close();
});
}
Expand Down Expand Up @@ -698,6 +699,19 @@ class connection : public base_connection,
has_shake_ = false;
}

void active_close_websocket() {
if (req_.get_content_type() == content_type::websocket &&
!req_.get_websocket_state()) {
req_.set_websocket_state(true);
std::string close_reason = "server close";
std::string close_msg = ws_.format_close_payload(
close_code::normal, close_reason.data(), close_reason.size());
auto header = ws_.format_header(close_msg.length(), opcode::close);
is_active_close_ = true;
send_msg(std::move(header), std::move(close_msg));
}
}

/****************** begin handle http body data *****************/
void handle_string_body(std::size_t) {
// defalt add limitation for string_body and else. you can remove the
Expand Down Expand Up @@ -1133,7 +1147,9 @@ class connection : public base_connection,
[this, self](const std::error_code &ec, size_t bytes_transferred) {
if (ec) {
cancel_timer();
req_.call_event(data_proc_state::data_error);

if (!req_.get_websocket_state())
req_.call_event(data_proc_state::data_error);

close();
return;
Expand Down Expand Up @@ -1239,6 +1255,7 @@ class connection : public base_connection,
close_code::normal, close_frame.message, len);
auto header = ws_.format_header(close_msg.length(), opcode::close);
send_msg(std::move(header), std::move(close_msg));
req_.set_websocket_state(true);
} break;
case cinatra::ws_frame_type::WS_PING_FRAME: {
auto header = ws_.format_header(payload.length(), opcode::pong);
Expand Down Expand Up @@ -1419,7 +1436,8 @@ class connection : public base_connection,
send_failed_cb_(ec);
req_.set_state(data_proc_state::data_error);
call_back();
close();
if (!is_active_close_)
close();
}
});
}
Expand Down Expand Up @@ -1478,6 +1496,8 @@ class connection : public base_connection,

QuitCallback quit_callback_ = nullptr;
uint64_t conn_id_ = 0;

bool is_active_close_ = false;
};

inline constexpr data_proc_state ws_open = data_proc_state::data_begin;
Expand Down
6 changes: 6 additions & 0 deletions include/cinatra/request.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,10 @@ class request {

data_proc_state get_state() const { return state_; }

void set_websocket_state(bool is_closed) { is_websocket_closed_ = is_closed; }

bool get_websocket_state() { return is_websocket_closed_; }

void set_part_data(std::string_view data) {
#ifdef CINATRA_ENABLE_GZIP
if (has_gzip_) {
Expand Down Expand Up @@ -944,5 +948,7 @@ class request {
event_call_backs_ = {};
std::smatch matches_;
std::unordered_map<std::string, int> restful_params_;

bool is_websocket_closed_ = false;
};
} // namespace cinatra