mirror of
https://github.com/zigzap/zap.git
synced 2025-10-21 07:34:08 +00:00
Merge pull request #39 from kassane/cpp-threadpool
C++: add threadpool(4)
This commit is contained in:
commit
7817064885
1 changed files with 38 additions and 24 deletions
|
@ -2,6 +2,7 @@
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <boost/beast.hpp>
|
#include <boost/beast.hpp>
|
||||||
|
#include <boost/asio/thread_pool.hpp>
|
||||||
|
|
||||||
namespace beast = boost::beast;
|
namespace beast = boost::beast;
|
||||||
namespace http = beast::http;
|
namespace http = beast::http;
|
||||||
|
@ -18,9 +19,28 @@ std::string read_html_file(const std::string& file_path) {
|
||||||
return content;
|
return content;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void handle_client(tcp::socket socket, const std::string& msg) {
|
||||||
|
try {
|
||||||
|
// Construct an HTTP response with the HTML content
|
||||||
|
http::response<http::string_body> response;
|
||||||
|
response.version(11);
|
||||||
|
response.result(http::status::ok);
|
||||||
|
response.reason("OK");
|
||||||
|
response.set(http::field::server, "C++ Server");
|
||||||
|
response.set(http::field::content_type, "text/html");
|
||||||
|
response.body() = msg;
|
||||||
|
response.prepare_payload();
|
||||||
|
|
||||||
|
// Send the response to the client
|
||||||
|
http::write(socket, response);
|
||||||
|
} catch (const std::exception& e) {
|
||||||
|
std::cerr << "Error: " << e.what() << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
try {
|
try {
|
||||||
net::io_context io_context;
|
net::io_context io_context{BOOST_ASIO_CONCURRENCY_HINT_UNSAFE_IO};
|
||||||
|
|
||||||
// Create an endpoint to bind to
|
// Create an endpoint to bind to
|
||||||
tcp::endpoint endpoint(tcp::v4(), 8070);
|
tcp::endpoint endpoint(tcp::v4(), 8070);
|
||||||
|
@ -29,35 +49,29 @@ int main() {
|
||||||
tcp::acceptor acceptor(io_context, endpoint);
|
tcp::acceptor acceptor(io_context, endpoint);
|
||||||
std::cout << "Server listening on port 8070..." << std::endl;
|
std::cout << "Server listening on port 8070..." << std::endl;
|
||||||
|
|
||||||
|
// static 17-byte string
|
||||||
|
std::string msg = "Hello from C++!!!";
|
||||||
|
// or
|
||||||
|
// Read HTML content from a file (e.g., "index.html")
|
||||||
|
// std::string html_content = read_html_file("hello.html");
|
||||||
|
|
||||||
|
// std::cout << "str len: " << (html_content.length() == msg.length()) << std::boolalpha << "\n";
|
||||||
|
|
||||||
|
// Create a thread pool with 4 threads
|
||||||
|
net::thread_pool pool(4);
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
// Wait for a client to connect
|
// Wait for a client to connect
|
||||||
tcp::socket socket(io_context);
|
tcp::socket socket(io_context);
|
||||||
acceptor.accept(socket);
|
acceptor.accept(socket);
|
||||||
|
|
||||||
// static 17-byte string
|
// Post a task to the thread pool to handle the client request
|
||||||
|
net::post(pool, [socket = std::move(socket), msg]() mutable {
|
||||||
// Read HTML content from a file (e.g., "index.html")
|
handle_client(std::move(socket), msg);
|
||||||
// std::string html_content = read_html_file("hello.html");
|
});
|
||||||
// or
|
|
||||||
std::string msg = "Hello from C++!!!";
|
|
||||||
|
|
||||||
// std::cout << "str len: " << (html_content.length() == msg.length()) << std::boolalpha << "\n";
|
|
||||||
|
|
||||||
// Construct an HTTP response with the HTML content
|
|
||||||
http::response<http::string_body> response;
|
|
||||||
response.version(11);
|
|
||||||
response.result(http::status::ok);
|
|
||||||
response.reason("OK");
|
|
||||||
response.set(http::field::server, "C++ Server");
|
|
||||||
response.set(http::field::content_type, "text/html");
|
|
||||||
|
|
||||||
// response.body() = html_content;
|
|
||||||
response.body() = msg;
|
|
||||||
response.prepare_payload();
|
|
||||||
|
|
||||||
// Send the response to the client
|
|
||||||
http::write(socket, response);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The thread pool destructor will ensure that all threads are joined properly.
|
||||||
} catch (const std::exception& e) {
|
} catch (const std::exception& e) {
|
||||||
std::cerr << "Error: " << e.what() << std::endl;
|
std::cerr << "Error: " << e.what() << std::endl;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue