Skip to content

Commit

Permalink
feat: write response once
Browse files Browse the repository at this point in the history
  • Loading branch information
SHIINASAMA committed Sep 15, 2024
1 parent 46119da commit ad73e01
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 14 deletions.
36 changes: 28 additions & 8 deletions src/HttpConnectionEx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,11 @@ void HttpConnectionEx::readMagic() {
}

void HttpConnectionEx::readFrameHeader() {
// SESE_INFO("post read");
using namespace sese::net::http;
readBlock(temp_buffer, 9, [this](const asio::error_code &ec) {
if (ec) {
return;
}
// SESE_INFO("read");
memset(&frame, 0, sizeof(frame));
memcpy(reinterpret_cast<char *>(&frame.length) + 1, temp_buffer + 0, 3);
memcpy(&frame.type, temp_buffer + 3, 1);
Expand All @@ -60,6 +59,7 @@ void HttpConnectionEx::readFrameHeader() {
frame.ident &= 0x7fffffff;

if (frame.length > endpoint_max_frame_size) {
writeGoawayFrame(frame.ident, 0, GOAWAY_PROTOCOL_ERROR, "shutdown", true);
return;
}

Expand Down Expand Up @@ -649,7 +649,8 @@ void HttpConnectionEx::writeGoawayFrame(
uint32_t latest_stream_id,
uint8_t flags,
uint32_t error_code,
const std::string &msg) {
const std::string &msg,
bool once) {
using namespace sese::net::http;
auto frame = std::make_unique<Http2Frame>(msg.length() + 8);
frame->type = FRAME_TYPE_GOAWAY;
Expand All @@ -661,14 +662,24 @@ void HttpConnectionEx::writeGoawayFrame(
error_code = ToBigEndian32(error_code);
memcpy(frame->getFrameContentBuffer() + 0, &latest_stream_id, 4);
memcpy(frame->getFrameContentBuffer() + 4, &error_code, 4);
pre_vector.push_back(std::move(frame));
handleWrite();
if (once) {
auto buf = frame->getFrameBuffer();
auto len = frame->getFrameLength();
writeBlock(buf,
len,
[conn = getPtr(), f = std::shared_ptr(std::move(frame))](const asio::error_code &) {
});
} else {
pre_vector.push_back(std::move(frame));
handleWrite();
}
}

void HttpConnectionEx::writeRstStreamFrame(
uint32_t stream_id,
uint8_t flags,
uint32_t error_code) {
uint32_t error_code,
bool once) {
using namespace sese::net::http;
auto frame = std::make_unique<Http2Frame>(4);
frame->type = FRAME_TYPE_RST_STREAM;
Expand All @@ -678,8 +689,17 @@ void HttpConnectionEx::writeRstStreamFrame(
frame->buildFrameHeader();
error_code = ToBigEndian32(error_code);
memcpy(frame->getFrameContentBuffer(), &error_code, 4);
pre_vector.push_back(std::move(frame));
handleWrite();
if (once) {
auto buf = frame->getFrameBuffer();
auto len = frame->getFrameLength();
writeBlock(buf,
len,
[conn = getPtr(), f = std::shared_ptr(std::move(frame))](const asio::error_code &) {
});
} else {
pre_vector.push_back(std::move(frame));
handleWrite();
}
}


Expand Down
19 changes: 17 additions & 2 deletions src/HttpConnectionEx.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ struct HttpConnectionEx : std::enable_shared_from_this<HttpConnectionEx> {
/// @param buffers 缓存
/// @param callback 完成回调函数
virtual void writeBlocks(const std::vector<asio::const_buffer> &buffers,
const std::function<void(const asio::error_code &code)> &callback) = 0;

/// 写入块函数,此函数会确保写入完单块缓存,出现意外则直接回调
/// @param buffer 单块缓存
/// @param size 缓存大小
/// @param callback 完成回调函数
virtual void writeBlock(const void *buffer, size_t size,
const std::function<void(const asio::error_code &code)> &callback) = 0;

/// 读取块函数,此函数会确保读取完指定大小的缓存,出现意外则直接回调
Expand Down Expand Up @@ -133,13 +140,15 @@ struct HttpConnectionEx : std::enable_shared_from_this<HttpConnectionEx> {
uint32_t latest_stream_id,
uint8_t flags,
uint32_t error_code,
const std::string &msg
const std::string &msg,
bool once = false
);

void writeRstStreamFrame(
uint32_t stream_id,
uint8_t flags,
uint32_t error_code
uint32_t error_code,
bool once = false
);
};

Expand All @@ -156,6 +165,9 @@ struct HttpConnectionExImpl final : HttpConnectionEx {
SharedSocket socket);

void writeBlocks(const std::vector<asio::const_buffer> &buffers,
const std::function<void(const asio::error_code &code)> &callback) override;

void writeBlock(const void *buffer, size_t size,
const std::function<void(const asio::error_code &code)> &callback) override;

void readBlock(char *buffer, size_t length,
Expand All @@ -175,6 +187,9 @@ struct HttpsConnectionExImpl final : HttpConnectionEx {
SharedStream stream);

void writeBlocks(const std::vector<asio::const_buffer> &buffers,
const std::function<void(const asio::error_code &code)> &callback) override;

void writeBlock(const void *buffer, size_t size,
const std::function<void(const asio::error_code &code)> &callback) override;

void readBlock(char *buffer, size_t length,
Expand Down
26 changes: 22 additions & 4 deletions src/HttpConnectionExImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,21 @@ HttpConnectionExImpl::HttpConnectionExImpl(const std::shared_ptr<HttpServiceImpl


void HttpConnectionExImpl::writeBlocks(const std::vector<asio::const_buffer> &buffers,
const std::function<void(const asio::error_code &code)> &callback) {
async_write(*this->socket, buffers, [conn = getPtr(), callback](const asio::error_code &error, size_t bytes) {
const std::function<void(const asio::error_code &code)> &callback) {
async_write(*this->socket, buffers, [conn = getPtr(), callback](const asio::error_code &error, size_t) {
callback(error);
});
}

void HttpConnectionExImpl::writeBlock(const void *buffer, size_t size,
const std::function<void(const asio::error_code &code)> &callback) {
async_write(*this->socket, asio::buffer(buffer, size),
[conn = getPtr(), callback](const asio::error_code &error, size_t) {
callback(error);
});
}


void HttpConnectionExImpl::readBlock(char *buffer, size_t length,
const std::function<void(const asio::error_code &code)> &callback) {
is_read = true;
Expand All @@ -39,12 +48,21 @@ HttpsConnectionExImpl::HttpsConnectionExImpl(const std::shared_ptr<HttpServiceIm


void HttpsConnectionExImpl::writeBlocks(const std::vector<asio::const_buffer> &buffers,
const std::function<void(const asio::error_code &code)> &callback) {
asio::async_write(*this->stream, buffers, [conn = getPtr(), callback](const asio::error_code &error, size_t) {
const std::function<void(const asio::error_code &code)> &callback) {
async_write(*this->stream, buffers, [conn = getPtr(), callback](const asio::error_code &error, size_t) {
callback(error);
});
}

void HttpsConnectionExImpl::writeBlock(const void *buffer, size_t size,
const std::function<void(const asio::error_code &code)> &callback) {
async_write(*this->stream, asio::buffer(buffer, size),
[conn = getPtr(), callback](const asio::error_code &error, size_t) {
callback(error);
});
}


void HttpsConnectionExImpl::readBlock(char *buffer, size_t length,
const std::function<void(const asio::error_code &code)> &callback) {
is_read = true;
Expand Down

0 comments on commit ad73e01

Please sign in to comment.