static spl::shared_ptr<connection> create(std::shared_ptr<boost::asio::io_service> service, spl::shared_ptr<tcp::socket> socket, const protocol_strategy_factory<char>::ptr& protocol, spl::shared_ptr<connection_set> connection_set)
{
spl::shared_ptr<connection> con(new connection(std::move(service), std::move(socket), std::move(protocol), std::move(connection_set)));
+ con->init();
con->read_some();
return con;
}
+ void init()
+ {
+ protocol_ = protocol_factory_->create(spl::make_shared<connection_holder>(shared_from_this()));
+ }
+
~connection()
{
- CASPAR_LOG(info) << print() << L" connection destroyed.";
+ CASPAR_LOG(debug) << print() << L" connection destroyed.";
}
std::wstring print() const
{
CASPAR_LOG(info) << print() << L" Accepted connection from " << ipv4_address() << L" (" << (connection_set_->size() + 1) << L" connections).";
}
-
- protocol_strategy<char>& protocol() //always called from the asio-service-thread
- {
- if (!protocol_)
- protocol_ = protocol_factory_->create(spl::make_shared<connection_holder>(shared_from_this()));
-
- return *protocol_;
- }
void handle_read(const boost::system::error_code& error, size_t bytes_transferred) //always called from the asio-service-thread
{
{
std::string data(data_.begin(), data_.begin() + bytes_transferred);
- protocol().parse(data);
+ protocol_->parse(data);
}
catch(...)
{
do_write();
}
}
- else if (error != boost::asio::error::operation_aborted)
+ else if (error != boost::asio::error::operation_aborted && socket_->is_open())
stop();
}
if (!error)
{
+ boost::system::error_code ec;
+ socket->set_option(boost::asio::socket_base::keep_alive(true), ec);
+
+ if (ec)
+ CASPAR_LOG(warning) << print() << L" Failed to enable TCP keep-alive on socket";
+
auto conn = connection::create(service_, socket, protocol_factory_, connection_set_);
connection_set_->insert(conn);
start_accept();
}
+ std::wstring print() const
+ {
+ return L"async_event_server[:" + boost::lexical_cast<std::wstring>(acceptor_.local_endpoint().port()) + L"]";
+ }
+
void add_client_lifecycle_object_factory(const lifecycle_factory_t& factory)
{
auto self = shared_from_this();