C++ Asio和HTTP保持活动状态
我试图使用asio实现web服务器,但尽管将接受器设置为keep alive,但当我收到连接时,在发送响应后,连接套接字始终关闭,即使我从套接字重新读取(以便从http实现keep alive) 我尝试在代码中插入printfs,出于某种原因,在我编写响应之后,当客户端建立新连接时,它调用我套接字上的接受器,它不应该使用旧套接字并从中重新读取,而不是调用接受器吗 还是我用错误的方式做了“保持活力”功能 守则的有关部分如下:C++ Asio和HTTP保持活动状态,c++,http,asynchronous,boost-asio,keep-alive,C++,Http,Asynchronous,Boost Asio,Keep Alive,我试图使用asio实现web服务器,但尽管将接受器设置为keep alive,但当我收到连接时,在发送响应后,连接套接字始终关闭,即使我从套接字重新读取(以便从http实现keep alive) 我尝试在代码中插入printfs,出于某种原因,在我编写响应之后,当客户端建立新连接时,它调用我套接字上的接受器,它不应该使用旧套接字并从中重新读取,而不是调用接受器吗 还是我用错误的方式做了“保持活力”功能 守则的有关部分如下: #include "connection.hpp" #include &
#include "connection.hpp"
#include <vector>
#include <boost/bind.hpp>
#include "connection_manager.hpp"
#include "request_handler.hpp"
#include <iostream>
namespace http {
namespace server {
connection::connection(asio::io_service& io_service,
connection_manager& manager, request_handler& handler)
: socket_(io_service),
connection_manager_(manager),
request_handler_(handler),
request_parser_(new http_visitor()),
keep_alive_(true)
{
}
asio::ip::tcp::socket& connection::socket()
{
return socket_;
}
void connection::start()
{
socket_.async_read_some(asio::buffer(buffer_),
boost::bind(&connection::handle_read, shared_from_this(),
asio::placeholders::error,
asio::placeholders::bytes_transferred));
std::cout << "In connection start" << std::endl;
}
void connection::stop()
{
socket_.close();
}
void connection::handle_read(const asio::error_code& e,
std::size_t bytes_transferred)
{
if (!e)
{
boost::tribool result;
boost::tie(result, boost::tuples::ignore) = request_parser_.parse(
request_, buffer_.data(), buffer_.data() + bytes_transferred);
if (result)
{
request_handler_.handle_request(request_, reply_);
std::vector<header>::iterator hdr_it;
for(hdr_it = request_.headers.begin(); hdr_it != request_.headers.end(); hdr_it++)
{
if( (hdr_it->name.compare("Connection" ) == 0) &&
(hdr_it->value.compare("close" ) == 0 ) )
{ keep_alive_ = false; }
}
asio::async_write(socket_, reply_.to_buffers(),
boost::bind(&connection::handle_write, shared_from_this(),
asio::placeholders::error));
}
else if (!result)
{
reply_ = reply::stock_reply(reply::bad_request);
asio::async_write(socket_, reply_.to_buffers(),
boost::bind(&connection::handle_write, shared_from_this(),
asio::placeholders::error));
}
else
{
socket_.async_read_some(asio::buffer(buffer_),
boost::bind(&connection::handle_read, shared_from_this(),
asio::placeholders::error,
asio::placeholders::bytes_transferred));
}
}
else if (e != asio::error::operation_aborted)
{
connection_manager_.stop(shared_from_this());
}
}
void connection::handle_write(const asio::error_code& e)
{
if (!e)
{
std::cout << keep_alive_ << std::endl;
if(keep_alive_)
{
buffer_.assign(0);
request_.clear();
keep_alive_ = false;
start();
}
else
{
socket_.close();
}
}
if (e != asio::error::operation_aborted)
{
connection_manager_.stop(shared_from_this());
}
}
}
}
#包括“connection.hpp”
#包括
#包括
#包括“连接管理器.hpp”
#包括“request_handler.hpp”
#包括
命名空间http{
命名空间服务器{
连接::连接(asio::io_服务和io_服务,
连接管理器和管理器、请求处理程序和处理程序)
:插座(io_服务),
连接管理器(管理器),
请求处理程序(处理程序),
请求\u解析器(新http\u访问者()),
让你活着(真)
{
}
asio::ip::tcp::套接字和连接::套接字()
{
返回插座;
}
无效连接::开始()
{
套接字异步读取(asio::buffer(buffer)、,
boost::bind(&connection::handle_read,shared_from_this(),
asio::占位符::错误,
asio::占位符::字节(已传输);
std::cout value.compare(“close”)==0)
{keep_alive_=false;}
}
asio::异步写入(套接字、应答到缓冲区(),
boost::bind(&connection::handle_write,shared_from_this(),
asio::占位符::错误);
}
否则,如果(!结果)
{
回复=回复::库存回复(回复::错误的请求);
asio::异步写入(套接字、应答到缓冲区(),
boost::bind(&connection::handle_write,shared_from_this(),
asio::占位符::错误);
}
其他的
{
套接字异步读取(asio::buffer(buffer)、,
boost::bind(&connection::handle_read,shared_from_this(),
asio::占位符::错误,
asio::占位符::字节(已传输);
}
}
否则如果(e!=asio::错误::操作\u中止)
{
连接管理器停止(从本()共享);
}
}
无效连接::句柄\写入(常量asio::错误\代码&e)
{
如果(!e)
{
std::cout如果没有代码的完全可见性,我猜新连接\uuu
是一个共享的\u ptr
,并且您没有在任何地方保留对它的引用。这将导致智能指针删除连接并关闭套接字
在connections析构函数中插入一个断点,并检查是否是这种情况
如果看不到更多的代码,就很难诊断问题。你的连接管理器\u.start()
函数做什么?@Nick它在新连接上调用start。
#include "server.hpp"
#include <boost/bind.hpp>
#include <signal.h>
#include <iostream>
namespace http {
namespace server {
server::server(const std::string& address, const std::string& port,
const std::string& doc_root)
: io_service_(),
signals_(io_service_),
acceptor_(io_service_),
connection_manager_(),
new_connection_(new connection(io_service_, connection_manager_, request_handler_)),
request_handler_(doc_root)
{
signals_.add(SIGINT);
signals_.add(SIGTERM);
signals_.async_wait(boost::bind(&server::handle_stop, this));
// Open the acceptor with the option to reuse the address (i.e. SO_REUSEADDR).
asio::ip::tcp::resolver resolver(io_service_);
asio::ip::tcp::resolver::query query(address, port);
asio::ip::tcp::endpoint endpoint = *resolver.resolve(query);
acceptor_.open(endpoint.protocol());
acceptor_.set_option(asio::ip::tcp::acceptor::reuse_address(true));
acceptor_.set_option(asio::ip::tcp::acceptor::keep_alive(true));
acceptor_.bind(endpoint);
acceptor_.listen();
acceptor_.async_accept(new_connection_->socket(),
boost::bind(&server::handle_accept, this,
asio::placeholders::error));
}
void server::run()
{
io_service_.run();
}
void server::stop()
{
io_service_.post(boost::bind(&server::handle_stop, this));
}
void server::handle_accept(const asio::error_code& e)
{
if (!e)
{
std::cout << "In accept" << std::endl;
connection_manager_.start(new_connection_);
new_connection_.reset(new connection(io_service_, connection_manager_, request_handler_));
acceptor_.async_accept(new_connection_->socket(),
boost::bind(&server::handle_accept, this, asio::placeholders::error));
}
}
void server::handle_stop()
{
acceptor_.close();
connection_manager_.stop_all();
}
} // namespace server
} // namespace http