]> git.sesse.net Git - casparcg/blobdiff - protocol/util/AsyncEventServer.cpp
Enabled TCP keep alive on TCP connections to clients. This might help in discovering...
[casparcg] / protocol / util / AsyncEventServer.cpp
index 0a18c8d844f4294cb20f4a2be09524470f890d46..d404becdbb9c5711f5d6f2203a8a14864ef49441 100644 (file)
@@ -118,13 +118,19 @@ public:
        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
@@ -213,14 +219,6 @@ private:
        {
                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
        {               
@@ -230,7 +228,7 @@ private:
                        {
                                std::string data(data_.begin(), data_.begin() + bytes_transferred);
 
-                               protocol().parse(data);
+                               protocol_->parse(data);
                        }
                        catch(...)
                        {
@@ -258,7 +256,7 @@ private:
                                do_write();
                        }
                }
-               else if (error != boost::asio::error::operation_aborted)                
+               else if (error != boost::asio::error::operation_aborted && socket_->is_open())          
                        stop();
     }
 
@@ -331,6 +329,12 @@ struct AsyncEventServer::implementation : public spl::enable_shared_from_this<im
                
         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);
 
@@ -343,6 +347,11 @@ struct AsyncEventServer::implementation : public spl::enable_shared_from_this<im
                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();