Skip to content
This repository has been archived by the owner on Apr 19, 2024. It is now read-only.

Commit

Permalink
feat: write_end_callback for stream and ws connection
Browse files Browse the repository at this point in the history
  • Loading branch information
gaowanlu committed Apr 8, 2024
1 parent 4c985b7 commit c0dca25
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/connection/stream_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ bool stream_connection::buf2sock(bool &closed)
}
else
{
if (this->write_end_callback)
{
return this->write_end_callback(*this);
}
return false; // nothing m_waiting_send_pack to m_send_buffer
}
}
Expand Down Expand Up @@ -250,4 +254,6 @@ void stream_connection::reuse()
this->should_send_idx = -1;
this->should_send_size = 0;
this->sock2buf_data_len = 0;

write_end_callback = nullptr;
}
1 change: 1 addition & 0 deletions src/connection/stream_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ namespace tubekit
buffer::buffer m_send_buffer;
buffer::buffer m_recv_buffer;
buffer::buffer m_wating_send_pack;
std::function<bool(stream_connection &connection)> write_end_callback{nullptr};

private:
int should_send_idx{-1};
Expand Down
5 changes: 5 additions & 0 deletions src/connection/websocket_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ void websocket_connection::reuse()
this->m_wating_send_pack.set_limit_max(mem_buffer_size_max);

this->destory_callback = nullptr;
this->write_end_callback = nullptr;

http_parser_init(&m_http_parser, HTTP_REQUEST);
m_http_parser.data = this;
Expand Down Expand Up @@ -209,6 +210,10 @@ bool websocket_connection::buf2sock(bool &closed)
}
else
{
if (this->write_end_callback)
{
return this->write_end_callback(*this);
}
return false; // noting m_waiting_send_pack to m_send_buffer
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/connection/websocket_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ namespace tubekit
buffer::buffer m_send_buffer;
buffer::buffer m_wating_send_pack;

std::function<void(websocket_connection &connection)> destory_callback;
std::function<void(websocket_connection &connection)> destory_callback{nullptr};
std::function<bool(websocket_connection &connection)> write_end_callback{nullptr};

private:
int should_send_idx{-1};
Expand Down

0 comments on commit c0dca25

Please sign in to comment.