allow to set buffer size for server

master
Chunting Gu 5 years ago
parent ca83869912
commit c2f3a87cd2

@ -54,6 +54,8 @@ int main(int argc, char* argv[]) {
try { try {
webcc::Server server{ boost::asio::ip::tcp::v4(), port }; 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.Route("/upload", std::make_shared<FileUploadView>(), { "POST" });
server.Run(); server.Run();

@ -12,9 +12,10 @@ using boost::asio::ip::tcp;
namespace webcc { namespace webcc {
Connection::Connection(tcp::socket socket, ConnectionPool* pool, 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), : 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() { void Connection::Start() {

@ -24,7 +24,8 @@ using ConnectionPtr = std::shared_ptr<Connection>;
class Connection : public std::enable_shared_from_this<Connection> { class Connection : public std::enable_shared_from_this<Connection> {
public: public:
Connection(boost::asio::ip::tcp::socket socket, ConnectionPool* pool, 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; ~Connection() = default;

@ -217,7 +217,7 @@ bool RequestParser::ParsePartHeaders(bool* need_more_data) {
} }
bool RequestParser::GetNextBoundaryLine(std::size_t* b_off, bool RequestParser::GetNextBoundaryLine(std::size_t* b_off,
std::size_t* b_count, std::size_t* b_len,
bool* ended) { bool* ended) {
std::size_t off = 0; std::size_t off = 0;
@ -227,15 +227,15 @@ bool RequestParser::GetNextBoundaryLine(std::size_t* b_off,
break; break;
} }
std::size_t count = pos - off; std::size_t len = pos - off;
if (count == 0) { if (len == 0) {
off = pos + 2; off = pos + 2;
continue; // Empty line continue; // Empty line
} }
if (IsBoundary(pending_data_, off, count, ended)) { if (IsBoundary(pending_data_, off, len, ended)) {
*b_off = off; *b_off = off;
*b_count = count; *b_len = len;
return true; return true;
} }

@ -35,8 +35,7 @@ private:
bool ParseMultipartContent(const char* data, std::size_t length); bool ParseMultipartContent(const char* data, std::size_t length);
bool ParsePartHeaders(bool* need_more_data); bool ParsePartHeaders(bool* need_more_data);
bool GetNextBoundaryLine(std::size_t* b_off, std::size_t* b_count, bool GetNextBoundaryLine(std::size_t* b_off, std::size_t* b_len, bool* ended);
bool* ended);
// Check if the str.substr(off, count) is a boundary. // Check if the str.substr(off, count) is a boundary.
bool IsBoundary(const std::string& str, std::size_t off, bool IsBoundary(const std::string& str, std::size_t off,

@ -23,6 +23,7 @@ Server::Server(boost::asio::ip::tcp protocol, std::uint16_t port,
: protocol_(protocol), : protocol_(protocol),
port_(port), port_(port),
doc_root_(doc_root), doc_root_(doc_root),
buffer_size_(kBufferSize),
file_chunk_size_(1024), file_chunk_size_(1024),
running_(false), running_(false),
acceptor_(io_context_), acceptor_(io_context_),
@ -173,7 +174,8 @@ void Server::AsyncAccept() {
_2, _3); _2, _3);
auto connection = std::make_shared<Connection>( 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); pool_.Start(connection);
} }

@ -29,6 +29,12 @@ public:
Server(const Server&) = delete; Server(const Server&) = delete;
Server& operator=(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) { void set_file_chunk_size(std::size_t file_chunk_size) {
assert(file_chunk_size > 0); assert(file_chunk_size > 0);
file_chunk_size_ = file_chunk_size; file_chunk_size_ = file_chunk_size;
@ -84,7 +90,7 @@ private:
// The connection will keep alive if it's a persistent connection. When next // The connection will keep alive if it's a persistent connection. When next
// request comes, this connection will be put back to the queue again. // request comes, this connection will be put back to the queue again.
virtual void Handle(ConnectionPtr connection); virtual void Handle(ConnectionPtr connection);
// Match the view by HTTP method and URL (path). // Match the view by HTTP method and URL (path).
// Return if a view or static file is matched or not. // Return if a view or static file is matched or not.
// If the view asks for data streaming, |stream| will be set to true. // If the view asks for data streaming, |stream| will be set to true.
@ -104,6 +110,9 @@ private:
// The directory with the static files to be served. // The directory with the static files to be served.
boost::filesystem::path doc_root_; 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 // The size of the chunk loaded into memory each time when serving a
// static file. // static file.
std::size_t file_chunk_size_; std::size_t file_chunk_size_;

Loading…
Cancel
Save