allow to set buffer size for server
This commit is contained in:
@@ -54,6 +54,8 @@ int main(int argc, char* argv[]) {
|
||||
try {
|
||||
webcc::Server server{ boost::asio::ip::tcp::v4(), port };
|
||||
|
||||
server.set_buffer_size(webcc::kBufferSize * 10);
|
||||
|
||||
server.Route("/upload", std::make_shared<FileUploadView>(), { "POST" });
|
||||
|
||||
server.Run();
|
||||
|
||||
+3
-2
@@ -12,9 +12,10 @@ using boost::asio::ip::tcp;
|
||||
namespace webcc {
|
||||
|
||||
Connection::Connection(tcp::socket socket, ConnectionPool* pool,
|
||||
Queue<ConnectionPtr>* queue, ViewMatcher&& view_matcher)
|
||||
Queue<ConnectionPtr>* queue, ViewMatcher&& view_matcher,
|
||||
std::size_t buffer_size)
|
||||
: socket_(std::move(socket)), pool_(pool), queue_(queue),
|
||||
view_matcher_(std::move(view_matcher)), buffer_(kBufferSize) {
|
||||
view_matcher_(std::move(view_matcher)), buffer_(buffer_size) {
|
||||
}
|
||||
|
||||
void Connection::Start() {
|
||||
|
||||
+2
-1
@@ -24,7 +24,8 @@ using ConnectionPtr = std::shared_ptr<Connection>;
|
||||
class Connection : public std::enable_shared_from_this<Connection> {
|
||||
public:
|
||||
Connection(boost::asio::ip::tcp::socket socket, ConnectionPool* pool,
|
||||
Queue<ConnectionPtr>* queue, ViewMatcher&& view_matcher);
|
||||
Queue<ConnectionPtr>* queue, ViewMatcher&& view_matcher,
|
||||
std::size_t buffer_size);
|
||||
|
||||
~Connection() = default;
|
||||
|
||||
|
||||
@@ -217,7 +217,7 @@ bool RequestParser::ParsePartHeaders(bool* need_more_data) {
|
||||
}
|
||||
|
||||
bool RequestParser::GetNextBoundaryLine(std::size_t* b_off,
|
||||
std::size_t* b_count,
|
||||
std::size_t* b_len,
|
||||
bool* ended) {
|
||||
std::size_t off = 0;
|
||||
|
||||
@@ -227,15 +227,15 @@ bool RequestParser::GetNextBoundaryLine(std::size_t* b_off,
|
||||
break;
|
||||
}
|
||||
|
||||
std::size_t count = pos - off;
|
||||
if (count == 0) {
|
||||
std::size_t len = pos - off;
|
||||
if (len == 0) {
|
||||
off = pos + 2;
|
||||
continue; // Empty line
|
||||
}
|
||||
|
||||
if (IsBoundary(pending_data_, off, count, ended)) {
|
||||
if (IsBoundary(pending_data_, off, len, ended)) {
|
||||
*b_off = off;
|
||||
*b_count = count;
|
||||
*b_len = len;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -35,8 +35,7 @@ private:
|
||||
|
||||
bool ParseMultipartContent(const char* data, std::size_t length);
|
||||
bool ParsePartHeaders(bool* need_more_data);
|
||||
bool GetNextBoundaryLine(std::size_t* b_off, std::size_t* b_count,
|
||||
bool* ended);
|
||||
bool GetNextBoundaryLine(std::size_t* b_off, std::size_t* b_len, bool* ended);
|
||||
|
||||
// Check if the str.substr(off, count) is a boundary.
|
||||
bool IsBoundary(const std::string& str, std::size_t off,
|
||||
|
||||
+3
-1
@@ -23,6 +23,7 @@ Server::Server(boost::asio::ip::tcp protocol, std::uint16_t port,
|
||||
: protocol_(protocol),
|
||||
port_(port),
|
||||
doc_root_(doc_root),
|
||||
buffer_size_(kBufferSize),
|
||||
file_chunk_size_(1024),
|
||||
running_(false),
|
||||
acceptor_(io_context_),
|
||||
@@ -173,7 +174,8 @@ void Server::AsyncAccept() {
|
||||
_2, _3);
|
||||
|
||||
auto connection = std::make_shared<Connection>(
|
||||
std::move(socket), &pool_, &queue_, std::move(view_matcher));
|
||||
std::move(socket), &pool_, &queue_, std::move(view_matcher),
|
||||
buffer_size_);
|
||||
|
||||
pool_.Start(connection);
|
||||
}
|
||||
|
||||
@@ -29,6 +29,12 @@ public:
|
||||
Server(const Server&) = delete;
|
||||
Server& operator=(const Server&) = delete;
|
||||
|
||||
void set_buffer_size(std::size_t buffer_size) {
|
||||
if (buffer_size > 0) {
|
||||
buffer_size_ = buffer_size;
|
||||
}
|
||||
}
|
||||
|
||||
void set_file_chunk_size(std::size_t file_chunk_size) {
|
||||
assert(file_chunk_size > 0);
|
||||
file_chunk_size_ = file_chunk_size;
|
||||
@@ -104,6 +110,9 @@ private:
|
||||
// The directory with the static files to be served.
|
||||
boost::filesystem::path doc_root_;
|
||||
|
||||
// The size of the buffer for reading request.
|
||||
std::size_t buffer_size_;
|
||||
|
||||
// The size of the chunk loaded into memory each time when serving a
|
||||
// static file.
|
||||
std::size_t file_chunk_size_;
|
||||
|
||||
Reference in New Issue
Block a user