Added Client::close, and Client::connections and Client::connections_mutex are no longer shared_ptrs
This commit is contained in:
parent
16aa958903
commit
d9416c2566
4 changed files with 42 additions and 42 deletions
|
|
@ -214,25 +214,24 @@ namespace SimpleWeb {
|
||||||
void request(const std::string &method, const std::string &path, string_view content, const CaseInsensitiveMultimap &header,
|
void request(const std::string &method, const std::string &path, string_view content, const CaseInsensitiveMultimap &header,
|
||||||
std::function<void(std::shared_ptr<Response>, const error_code &)> &&request_callback_) {
|
std::function<void(std::shared_ptr<Response>, const error_code &)> &&request_callback_) {
|
||||||
auto session = std::make_shared<Session>(this->shared_from_this(), get_connection(), create_request_header(method, path, header));
|
auto session = std::make_shared<Session>(this->shared_from_this(), get_connection(), create_request_header(method, path, header));
|
||||||
|
auto client = session->client;
|
||||||
auto connection = session->connection;
|
auto connection = session->connection;
|
||||||
auto response = session->response;
|
auto response = session->response;
|
||||||
auto request_callback = std::make_shared<std::function<void(std::shared_ptr<Response>, const error_code &)>>(std::move(request_callback_));
|
auto request_callback = std::make_shared<std::function<void(std::shared_ptr<Response>, const error_code &)>>(std::move(request_callback_));
|
||||||
auto connections = this->connections;
|
session->callback = [client, connection, response, request_callback](const error_code &ec) {
|
||||||
auto connections_mutex = this->connections_mutex;
|
|
||||||
session->callback = [connection, response, request_callback, connections, connections_mutex](const error_code &ec) {
|
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(*connections_mutex);
|
std::lock_guard<std::mutex> lock(client->connections_mutex);
|
||||||
connection->in_use = false;
|
connection->in_use = false;
|
||||||
|
|
||||||
// Remove unused connections, but keep one open for HTTP persistent connection:
|
// Remove unused connections, but keep one open for HTTP persistent connection:
|
||||||
size_t unused_connections = 0;
|
size_t unused_connections = 0;
|
||||||
for(auto it = connections->begin(); it != connections->end();) {
|
for(auto it = client->connections.begin(); it != client->connections.end();) {
|
||||||
if((*it)->in_use)
|
if((*it)->in_use)
|
||||||
++it;
|
++it;
|
||||||
else {
|
else {
|
||||||
++unused_connections;
|
++unused_connections;
|
||||||
if(unused_connections > 1)
|
if(unused_connections > 1)
|
||||||
it = connections->erase(it);
|
it = client->connections.erase(it);
|
||||||
else
|
else
|
||||||
++it;
|
++it;
|
||||||
}
|
}
|
||||||
|
|
@ -273,25 +272,24 @@ namespace SimpleWeb {
|
||||||
void request(const std::string &method, const std::string &path, std::istream &content, const CaseInsensitiveMultimap &header,
|
void request(const std::string &method, const std::string &path, std::istream &content, const CaseInsensitiveMultimap &header,
|
||||||
std::function<void(std::shared_ptr<Response>, const error_code &)> &&request_callback_) {
|
std::function<void(std::shared_ptr<Response>, const error_code &)> &&request_callback_) {
|
||||||
auto session = std::make_shared<Session>(this->shared_from_this(), get_connection(), create_request_header(method, path, header));
|
auto session = std::make_shared<Session>(this->shared_from_this(), get_connection(), create_request_header(method, path, header));
|
||||||
|
auto client = session->client;
|
||||||
auto connection = session->connection;
|
auto connection = session->connection;
|
||||||
auto response = session->response;
|
auto response = session->response;
|
||||||
auto request_callback = std::make_shared<std::function<void(std::shared_ptr<Response>, const error_code &)>>(std::move(request_callback_));
|
auto request_callback = std::make_shared<std::function<void(std::shared_ptr<Response>, const error_code &)>>(std::move(request_callback_));
|
||||||
auto connections = this->connections;
|
session->callback = [client, connection, response, request_callback](const error_code &ec) {
|
||||||
auto connections_mutex = this->connections_mutex;
|
|
||||||
session->callback = [connection, response, request_callback, connections, connections_mutex](const error_code &ec) {
|
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(*connections_mutex);
|
std::lock_guard<std::mutex> lock(client->connections_mutex);
|
||||||
connection->in_use = false;
|
connection->in_use = false;
|
||||||
|
|
||||||
// Remove unused connections, but keep one open for HTTP persistent connection:
|
// Remove unused connections, but keep one open for HTTP persistent connection:
|
||||||
size_t unused_connections = 0;
|
size_t unused_connections = 0;
|
||||||
for(auto it = connections->begin(); it != connections->end();) {
|
for(auto it = client->connections.begin(); it != client->connections.end();) {
|
||||||
if((*it)->in_use)
|
if((*it)->in_use)
|
||||||
++it;
|
++it;
|
||||||
else {
|
else {
|
||||||
++unused_connections;
|
++unused_connections;
|
||||||
if(unused_connections > 1)
|
if(unused_connections > 1)
|
||||||
it = connections->erase(it);
|
it = client->connections.erase(it);
|
||||||
else
|
else
|
||||||
++it;
|
++it;
|
||||||
}
|
}
|
||||||
|
|
@ -321,17 +319,25 @@ namespace SimpleWeb {
|
||||||
request(method, path, content, CaseInsensitiveMultimap(), std::move(request_callback));
|
request(method, path, content, CaseInsensitiveMultimap(), std::move(request_callback));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Close connections
|
||||||
|
void close() {
|
||||||
|
std::lock_guard<std::mutex> lock(connections_mutex);
|
||||||
|
for(auto it = connections.begin(); it != connections.end();) {
|
||||||
|
(*it)->close();
|
||||||
|
it = connections.erase(it);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::string host;
|
std::string host;
|
||||||
unsigned short port;
|
unsigned short port;
|
||||||
|
|
||||||
std::unique_ptr<asio::ip::tcp::resolver::query> query;
|
std::unique_ptr<asio::ip::tcp::resolver::query> query;
|
||||||
|
|
||||||
std::shared_ptr<std::vector<std::shared_ptr<Connection>>> connections;
|
std::vector<std::shared_ptr<Connection>> connections;
|
||||||
std::shared_ptr<std::mutex> connections_mutex;
|
std::mutex connections_mutex;
|
||||||
|
|
||||||
ClientBase(const std::string &host_port, unsigned short default_port)
|
ClientBase(const std::string &host_port, unsigned short default_port) : io_service(new asio::io_service()) {
|
||||||
: io_service(new asio::io_service()), connections(new std::vector<std::shared_ptr<Connection>>()), connections_mutex(new std::mutex()) {
|
|
||||||
auto parsed_host_port = parse_host_port(host_port, default_port);
|
auto parsed_host_port = parse_host_port(host_port, default_port);
|
||||||
host = parsed_host_port.first;
|
host = parsed_host_port.first;
|
||||||
port = parsed_host_port.second;
|
port = parsed_host_port.second;
|
||||||
|
|
@ -339,8 +345,8 @@ namespace SimpleWeb {
|
||||||
|
|
||||||
std::shared_ptr<Connection> get_connection() {
|
std::shared_ptr<Connection> get_connection() {
|
||||||
std::shared_ptr<Connection> connection;
|
std::shared_ptr<Connection> connection;
|
||||||
std::lock_guard<std::mutex> lock(*connections_mutex);
|
std::lock_guard<std::mutex> lock(connections_mutex);
|
||||||
for(auto it = connections->begin(); it != connections->end(); ++it) {
|
for(auto it = connections.begin(); it != connections.end(); ++it) {
|
||||||
if(!(*it)->in_use && !connection) {
|
if(!(*it)->in_use && !connection) {
|
||||||
connection = *it;
|
connection = *it;
|
||||||
break;
|
break;
|
||||||
|
|
@ -348,7 +354,7 @@ namespace SimpleWeb {
|
||||||
}
|
}
|
||||||
if(!connection) {
|
if(!connection) {
|
||||||
connection = create_connection();
|
connection = create_connection();
|
||||||
connections->emplace_back(connection);
|
connections.emplace_back(connection);
|
||||||
}
|
}
|
||||||
connection->reconnecting = false;
|
connection->reconnecting = false;
|
||||||
connection->in_use = true;
|
connection->in_use = true;
|
||||||
|
|
|
||||||
|
|
@ -517,7 +517,7 @@ namespace SimpleWeb {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}; // namespace SimpleWeb
|
};
|
||||||
|
|
||||||
template <class socket_type>
|
template <class socket_type>
|
||||||
class Server : public ServerBase<socket_type> {};
|
class Server : public ServerBase<socket_type> {};
|
||||||
|
|
@ -538,12 +538,10 @@ namespace SimpleWeb {
|
||||||
Server() : ServerBase<HTTP>::ServerBase(80) {}
|
Server() : ServerBase<HTTP>::ServerBase(80) {}
|
||||||
|
|
||||||
void accept() override {
|
void accept() override {
|
||||||
//Create new socket for this connection
|
|
||||||
//Shared_ptr is used to pass temporary objects to the asynchronous functions
|
|
||||||
auto session = std::make_shared<Session>(this->shared_from_this(), std::make_shared<HTTP>(*io_service));
|
auto session = std::make_shared<Session>(this->shared_from_this(), std::make_shared<HTTP>(*io_service));
|
||||||
|
|
||||||
acceptor->async_accept(*session->socket, [this, session](const error_code &ec) {
|
acceptor->async_accept(*session->socket, [this, session](const error_code &ec) {
|
||||||
//Immediately start accepting a new connection (if io_service hasn't been stopped)
|
//Immediately start accepting a new connection (unless io_service has been stopped)
|
||||||
if(ec != asio::error::operation_aborted)
|
if(ec != asio::error::operation_aborted)
|
||||||
this->accept();
|
this->accept();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -55,16 +55,12 @@ namespace SimpleWeb {
|
||||||
asio::ssl::context context;
|
asio::ssl::context context;
|
||||||
|
|
||||||
void accept() override {
|
void accept() override {
|
||||||
//Create new socket for this connection
|
|
||||||
//Shared_ptr is used to pass temporary objects to the asynchronous functions
|
|
||||||
auto session = std::make_shared<Session>(this->shared_from_this(), std::make_shared<HTTPS>(*io_service, context));
|
auto session = std::make_shared<Session>(this->shared_from_this(), std::make_shared<HTTPS>(*io_service, context));
|
||||||
|
|
||||||
acceptor->async_accept(session->socket->lowest_layer(), [this, session](const error_code &ec) {
|
acceptor->async_accept(session->socket->lowest_layer(), [this, session](const error_code &ec) {
|
||||||
//Immediately start accepting a new connection (if io_service hasn't been stopped)
|
|
||||||
if(ec != asio::error::operation_aborted)
|
if(ec != asio::error::operation_aborted)
|
||||||
this->accept();
|
this->accept();
|
||||||
|
|
||||||
|
|
||||||
if(!ec) {
|
if(!ec) {
|
||||||
asio::ip::tcp::no_delay option(true);
|
asio::ip::tcp::no_delay option(true);
|
||||||
session->socket->lowest_layer().set_option(option);
|
session->socket->lowest_layer().set_option(option);
|
||||||
|
|
|
||||||
|
|
@ -151,8 +151,8 @@ int main() {
|
||||||
auto r = client->request("POST", "/string", content);
|
auto r = client->request("POST", "/string", content);
|
||||||
output << r->content.rdbuf();
|
output << r->content.rdbuf();
|
||||||
assert(output.str() == "A string");
|
assert(output.str() == "A string");
|
||||||
assert(client->connections->size() == 1);
|
assert(client->connections.size() == 1);
|
||||||
connection = client->connections->front().get();
|
connection = client->connections.front().get();
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
|
|
@ -160,8 +160,8 @@ int main() {
|
||||||
auto r = client->request("POST", "/string", "A string");
|
auto r = client->request("POST", "/string", "A string");
|
||||||
output << r->content.rdbuf();
|
output << r->content.rdbuf();
|
||||||
assert(output.str() == "A string");
|
assert(output.str() == "A string");
|
||||||
assert(client->connections->size() == 1);
|
assert(client->connections.size() == 1);
|
||||||
assert(connection == client->connections->front().get());
|
assert(connection == client->connections.front().get());
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
|
|
@ -169,16 +169,16 @@ int main() {
|
||||||
auto r = client->request("GET", "/header", "", {{"test1", "test"}, {"test2", "ing"}});
|
auto r = client->request("GET", "/header", "", {{"test1", "test"}, {"test2", "ing"}});
|
||||||
output << r->content.rdbuf();
|
output << r->content.rdbuf();
|
||||||
assert(output.str() == "testing");
|
assert(output.str() == "testing");
|
||||||
assert(client->connections->size() == 1);
|
assert(client->connections.size() == 1);
|
||||||
assert(connection == client->connections->front().get());
|
assert(connection == client->connections.front().get());
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
stringstream output;
|
stringstream output;
|
||||||
auto r = client->request("GET", "/query_string?testing");
|
auto r = client->request("GET", "/query_string?testing");
|
||||||
assert(r->content.string() == "testing");
|
assert(r->content.string() == "testing");
|
||||||
assert(client->connections->size() == 1);
|
assert(client->connections.size() == 1);
|
||||||
assert(connection == client->connections->front().get());
|
assert(connection == client->connections.front().get());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -212,10 +212,10 @@ int main() {
|
||||||
}
|
}
|
||||||
for(auto &thread : threads)
|
for(auto &thread : threads)
|
||||||
thread.join();
|
thread.join();
|
||||||
assert(client->connections->size() == 100);
|
assert(client->connections.size() == 100);
|
||||||
client->io_service->reset();
|
client->io_service->reset();
|
||||||
client->io_service->run();
|
client->io_service->run();
|
||||||
assert(client->connections->size() == 1);
|
assert(client->connections.size() == 1);
|
||||||
for(auto call : calls)
|
for(auto call : calls)
|
||||||
assert(call);
|
assert(call);
|
||||||
}
|
}
|
||||||
|
|
@ -223,18 +223,18 @@ int main() {
|
||||||
|
|
||||||
{
|
{
|
||||||
auto client = HttpClient::create("localhost:8080");
|
auto client = HttpClient::create("localhost:8080");
|
||||||
assert(client->connections->size() == 0);
|
assert(client->connections.size() == 0);
|
||||||
for(size_t c = 0; c < 5000; ++c) {
|
for(size_t c = 0; c < 5000; ++c) {
|
||||||
auto r1 = client->request("POST", "/string", "A string");
|
auto r1 = client->request("POST", "/string", "A string");
|
||||||
assert(SimpleWeb::status_code(r1->status_code) == SimpleWeb::StatusCode::success_ok);
|
assert(SimpleWeb::status_code(r1->status_code) == SimpleWeb::StatusCode::success_ok);
|
||||||
assert(r1->content.string() == "A string");
|
assert(r1->content.string() == "A string");
|
||||||
assert(client->connections->size() == 1);
|
assert(client->connections.size() == 1);
|
||||||
|
|
||||||
stringstream content("A string");
|
stringstream content("A string");
|
||||||
auto r2 = client->request("POST", "/string", content);
|
auto r2 = client->request("POST", "/string", content);
|
||||||
assert(SimpleWeb::status_code(r2->status_code) == SimpleWeb::StatusCode::success_ok);
|
assert(SimpleWeb::status_code(r2->status_code) == SimpleWeb::StatusCode::success_ok);
|
||||||
assert(r2->content.string() == "A string");
|
assert(r2->content.string() == "A string");
|
||||||
assert(client->connections->size() == 1);
|
assert(client->connections.size() == 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -244,7 +244,7 @@ int main() {
|
||||||
auto r = client->request("POST", "/string", "A string");
|
auto r = client->request("POST", "/string", "A string");
|
||||||
assert(SimpleWeb::status_code(r->status_code) == SimpleWeb::StatusCode::success_ok);
|
assert(SimpleWeb::status_code(r->status_code) == SimpleWeb::StatusCode::success_ok);
|
||||||
assert(r->content.string() == "A string");
|
assert(r->content.string() == "A string");
|
||||||
assert(client->connections->size() == 1);
|
assert(client->connections.size() == 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
|
|
@ -253,7 +253,7 @@ int main() {
|
||||||
auto r = client->request("POST", "/string", content);
|
auto r = client->request("POST", "/string", content);
|
||||||
assert(SimpleWeb::status_code(r->status_code) == SimpleWeb::StatusCode::success_ok);
|
assert(SimpleWeb::status_code(r->status_code) == SimpleWeb::StatusCode::success_ok);
|
||||||
assert(r->content.string() == "A string");
|
assert(r->content.string() == "A string");
|
||||||
assert(client->connections->size() == 1);
|
assert(client->connections.size() == 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue