]> git.sesse.net Git - casparcg/commitdiff
* Removed some redundant logging from AsyncEventServer.
authorHelge Norberg <helge.norberg@svt.se>
Tue, 27 Oct 2015 10:09:44 +0000 (11:09 +0100)
committerHelge Norberg <helge.norberg@svt.se>
Tue, 27 Oct 2015 10:09:44 +0000 (11:09 +0100)
* Removed print() from client_connection in favor of always using address()
* Moved some logging from info to trace.
* Fixed potential lifetime issue with using asio post/dispatch in AsyncEventServer.
* Fixed problem where AsyncEventServer did not log the ip address of the remote client, only the servers address.
* Log the number total number of connections served by one AsyncEventServer on connect and disconnect.

protocol/amcp/AMCPProtocolStrategy.cpp
protocol/cii/CIIProtocolStrategy.cpp
protocol/util/AsyncEventServer.cpp
protocol/util/AsyncEventServer.h
protocol/util/ClientInfo.h
protocol/util/protocol_strategy.h
protocol/util/strategy_adapters.cpp

index f22827bd35c0a8729b466e1798674fd382f5a3f5..5068ca875a2287b60bc24788030a85e9373e61ca 100644 (file)
@@ -105,7 +105,7 @@ public:
        //Thesefore the AMCPProtocolStrategy should be decorated with a delimiter_based_chunking_strategy
        void Parse(const std::wstring& message, ClientInfoPtr client)
        {
-               CASPAR_LOG(info) << L"Received message from " << client->print() << ": " << message << L"\\r\\n";
+               CASPAR_LOG(info) << L"Received message from " << client->address() << ": " << message << L"\\r\\n";
        
                command_interpreter_result result;
                if(interpret_command_string(message, result, client))
index d779d2a44a89c5113f1bf368c322c72c1842776a..86a18f794d86ca246c6298dca01c884fc3aacfac 100644 (file)
@@ -70,7 +70,7 @@ void CIIProtocolStrategy::Parse(const std::wstring& message, IO::ClientInfoPtr c
 
 void CIIProtocolStrategy::ProcessMessage(const std::wstring& message, IO::ClientInfoPtr pClientInfo)
 {      
-       CASPAR_LOG(info) << L"Received message from " << pClientInfo->print() << ": " << message << L"\\r\\n";
+       CASPAR_LOG(info) << L"Received message from " << pClientInfo->address() << ": " << message << L"\\r\\n";
 
        std::vector<std::wstring> tokens;
        int tokenCount = TokenizeMessage(message, &tokens);
index 8b3435429fed0d3c605a4cf8f0fcaa19a6e75a6b..c883a78f72f3311f755db83a6813ba1a0e76389e 100644 (file)
@@ -52,8 +52,8 @@ class connection : public spl::enable_shared_from_this<connection>
 
     const spl::shared_ptr<tcp::socket>                         socket_; 
        std::shared_ptr<boost::asio::io_service>                service_;
+       const std::wstring                                                              listen_port_;
        const spl::shared_ptr<connection_set>                   connection_set_;
-       const std::wstring                                                              name_;
        protocol_strategy_factory<char>::ptr                    protocol_factory_;
        std::shared_ptr<protocol_strategy<char>>                protocol_;
 
@@ -85,22 +85,12 @@ class connection : public spl::enable_shared_from_this<connection>
                                conn->disconnect();
                }
 
-               std::wstring print() const override
-               {
-                       auto conn = connection_.lock();
-
-                       if (conn)
-                               return conn->print();
-                       else
-                               return L"[destroyed-connection]";
-               }
-
                std::wstring address() const override
                {
                        auto conn = connection_.lock();
 
                        if (conn)
-                               return conn->address();
+                               return conn->ipv4_address();
                        else
                                return L"[destroyed-connection]";
                }
@@ -139,7 +129,7 @@ public:
 
        std::wstring print() const
        {
-               return L"[" + name_ + L"]";
+               return L"async_event_server[:" + listen_port_ + L"]";
        }
 
        std::wstring address() const
@@ -147,15 +137,22 @@ public:
                return u16(socket_->local_endpoint().address().to_string());
        }
        
-       virtual void send(std::string&& data)
+       std::wstring ipv4_address() const
+       {
+               return socket_->is_open() ? u16(socket_->remote_endpoint().address().to_string()) : L"no-address";
+       }
+
+       void send(std::string&& data)
        {
                send_queue_.push(std::move(data));
-               service_->dispatch([=] { do_write(); });
+               auto self = shared_from_this();
+               service_->dispatch([=] { self->do_write(); });
        }
 
-       virtual void disconnect()
+       void disconnect()
        {
-               service_->dispatch([=] { stop(); });
+               auto self = shared_from_this();
+               service_->dispatch([=] { self->stop(); });
        }
 
        void add_lifecycle_bound_object(const std::wstring& key, const std::shared_ptr<void>& lifecycle_bound)
@@ -176,7 +173,6 @@ public:
                return std::shared_ptr<void>();
        }
 
-       /**************/
 private:
        void do_write() //always called from the asio-service-thread
        {
@@ -193,32 +189,29 @@ private:
        void stop()     //always called from the asio-service-thread
        {
                connection_set_->erase(shared_from_this());
+
+               CASPAR_LOG(info) << print() << L" Client " << ipv4_address() << L" disconnected (" << connection_set_->size() << L" connections).";
+
                try
                {
+                       socket_->cancel();
                        socket_->close();
                }
                catch(...)
                {
                        CASPAR_LOG_CURRENT_EXCEPTION();
                }
-               
-               CASPAR_LOG(info) << print() << L" Disconnected.";
-       }
-
-       const std::string ipv4_address() const
-       {
-               return socket_->is_open() ? socket_->remote_endpoint().address().to_string() : "no-address";
        }
 
     connection(const std::shared_ptr<boost::asio::io_service>& service, const spl::shared_ptr<tcp::socket>& socket, const protocol_strategy_factory<char>::ptr& protocol_factory, const spl::shared_ptr<connection_set>& connection_set) 
                : socket_(socket)
                , service_(service)
-               , name_((socket_->is_open() ? u16(socket_->local_endpoint().address().to_string() + ":" + boost::lexical_cast<std::string>(socket_->local_endpoint().port())) : L"no-address"))
+               , listen_port_(socket_->is_open() ? boost::lexical_cast<std::wstring>(socket_->local_endpoint().port()) : L"no-port")
                , connection_set_(connection_set)
                , protocol_factory_(protocol_factory)
                , is_writing_(false)
        {
-               CASPAR_LOG(info) << print() << L" Connected.";
+               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
@@ -237,8 +230,6 @@ private:
                        {
                                std::string data(data_.begin(), data_.begin() + bytes_transferred);
 
-                               CASPAR_LOG(trace) << print() << L" Received: " << u16(data);
-
                                protocol().parse(data);
                        }
                        catch(...)
@@ -256,7 +247,6 @@ private:
        {
                if(!error)
                {
-                       CASPAR_LOG(trace) << print() << L" Sent: " << (str->size() < 512 ? u16(*str) : L"more than 512 bytes.");
                        if(bytes_transferred != str->size())
                        {
                                str->assign(str->substr(bytes_transferred));
@@ -287,7 +277,7 @@ private:
        friend struct AsyncEventServer::implementation;
 };
 
-struct AsyncEventServer::implementation
+struct AsyncEventServer::implementation : public spl::enable_shared_from_this<implementation>
 {
        std::shared_ptr<boost::asio::io_service>        service_;
        tcp::acceptor                                                           acceptor_;
@@ -301,7 +291,6 @@ struct AsyncEventServer::implementation
                , acceptor_(*service_, tcp::endpoint(tcp::v4(), port))
                , protocol_factory_(protocol)
        {
-               start_accept();
        }
 
        ~implementation()
@@ -343,7 +332,7 @@ struct AsyncEventServer::implementation
 
                        for (auto& lifecycle_factory : lifecycle_factories_)
                        {
-                               auto lifecycle_bound = lifecycle_factory(conn->ipv4_address());
+                               auto lifecycle_bound = lifecycle_factory(u8(conn->ipv4_address()));
                                conn->add_lifecycle_bound_object(lifecycle_bound.first, lifecycle_bound.second);
                        }
                }
@@ -352,13 +341,17 @@ struct AsyncEventServer::implementation
 
        void add_client_lifecycle_object_factory(const lifecycle_factory_t& factory)
        {
-               service_->post([=]{ lifecycle_factories_.push_back(factory); });
+               auto self = shared_from_this();
+               service_->post([=]{ self->lifecycle_factories_.push_back(factory); });
        }
 };
 
 AsyncEventServer::AsyncEventServer(
                std::shared_ptr<boost::asio::io_service> service, const protocol_strategy_factory<char>::ptr& protocol, unsigned short port)
-       : impl_(new implementation(std::move(service), protocol, port)) {}
+       : impl_(new implementation(std::move(service), protocol, port))
+{
+       impl_->start_accept();
+}
 
 AsyncEventServer::~AsyncEventServer() {}
 void AsyncEventServer::add_client_lifecycle_object_factory(const lifecycle_factory_t& factory) { impl_->add_client_lifecycle_object_factory(factory); }
index f7551531b2e44030150aa2ca7da56ab6434440c5..8f491dffeee336dc46758e8b71a562eec7f647b7 100644 (file)
@@ -24,6 +24,8 @@
 
 #include "protocol_strategy.h"
 
+#include <common/memory.h>
+
 #include <boost/asio.hpp>
 
 namespace caspar { namespace IO {
@@ -42,7 +44,7 @@ public:
 
        struct implementation;
 private:
-       std::unique_ptr<implementation> impl_;
+       spl::shared_ptr<implementation> impl_;
 };
 
 }}     
index 8c5421898de218f969d6300e42d09ffe92e04121..1aa2ae6a09cabb1f7619b1f717b4ef0fc725f4d3 100644 (file)
@@ -39,8 +39,7 @@ struct ConsoleClientInfo : public client_connection<wchar_t>
                std::wcout << (L"#" + caspar::log::replace_nonprintable_copy(data, L'?')) << std::flush;
        }
        void disconnect() override {}
-       std::wstring print() const override {return L"Console";}
-       std::wstring address() const override { return L"127.0.0.1"; }
+       std::wstring address() const override { return L"Console"; }
        void add_lifecycle_bound_object(const std::wstring& key, const std::shared_ptr<void>& lifecycle_bound) override {}
        std::shared_ptr<void> remove_lifecycle_bound_object(const std::wstring& key) override { return std::shared_ptr<void>(); }
 };
index 4efa2004f7b89d520a932cda1ecf01fa4ac2bf05..68bba4560d491ce35842b0a6f233af19a003cd27 100644 (file)
@@ -64,7 +64,6 @@ public:
 \r
        virtual void send(std::basic_string<CharT>&& data) = 0;\r
        virtual void disconnect() = 0;\r
-       virtual std::wstring print() const = 0;\r
        virtual std::wstring address() const = 0;\r
 \r
        virtual void add_lifecycle_bound_object(const std::wstring& key, const std::shared_ptr<void>& lifecycle_bound) = 0;\r
index c3d5714ab7c3a41824df9512b4147dae46b88463..a1f80b7f80e285e68ae26735365727764d3b0416 100644 (file)
@@ -59,11 +59,11 @@ public:
                : client_(client)\r
                , codepage_(codepage)\r
        {\r
-               CASPAR_LOG(info) << "from_unicode_client_connection created.";\r
+               CASPAR_LOG(trace) << "from_unicode_client_connection created.";\r
        }\r
        ~from_unicode_client_connection()\r
        {\r
-               CASPAR_LOG(info) << "from_unicode_client_connection destroyed.";\r
+               CASPAR_LOG(trace) << "from_unicode_client_connection destroyed.";\r
        }\r
 \r
        void send(std::basic_string<wchar_t>&& data) override\r
@@ -76,10 +76,10 @@ public:
                {\r
                        boost::replace_all(data, L"\n", L"\\n");\r
                        boost::replace_all(data, L"\r", L"\\r");\r
-                       CASPAR_LOG(info) << L"Sent message to " << client_->print() << L":" << data;\r
+                       CASPAR_LOG(info) << L"Sent message to " << client_->address() << L":" << data;\r
                }\r
                else\r
-                       CASPAR_LOG(info) << L"Sent more than 512 bytes to " << client_->print();\r
+                       CASPAR_LOG(info) << L"Sent more than 512 bytes to " << client_->address();\r
        }\r
 \r
        void disconnect() override\r
@@ -87,11 +87,6 @@ public:
                client_->disconnect();\r
        }\r
 \r
-       std::wstring print() const override\r
-       {\r
-               return client_->print();\r
-       }\r
-\r
        std::wstring address() const override\r
        {\r
                return client_->address();\r
@@ -175,11 +170,11 @@ public:
                : strategy_(strategy)\r
                , client_info_(client_connection)\r
        {\r
-               CASPAR_LOG(info) << "legacy_strategy_adapter created.";\r
+               CASPAR_LOG(trace) << "legacy_strategy_adapter created.";\r
        }\r
        ~legacy_strategy_adapter()\r
        {\r
-               CASPAR_LOG(info) << "legacy_strategy_adapter destroyed.";\r
+               CASPAR_LOG(trace) << "legacy_strategy_adapter destroyed.";\r
        }\r
 \r
        void parse(const std::basic_string<wchar_t>& data) override\r