]> git.sesse.net Git - casparcg/blobdiff - protocol/osc/client.cpp
Improved performance in OSC message formatting especially noticeable in debug mode.
[casparcg] / protocol / osc / client.cpp
index 61e577f85723933f5b1a6ac97fb8426920d8ad23..be4c76c3a60b9f2e27a8a35b101279ecea2090cb 100644 (file)
@@ -20,7 +20,7 @@
 * Author: Helge Norberg, helge.norberg@svt.se
 */
 
-#include "../stdafx.h"
+#include "../StdAfx.h"
 
 #include "client.h"
 
@@ -31,6 +31,8 @@
 #include <common/except.h>
 #include <common/endian.h>
 #include <common/cache_aligned_vector.h>
+#include <common/os/general_protection_fault.h>
+
 #include <core/monitor/monitor.h>
 
 #include <functional>
@@ -83,19 +85,34 @@ struct param_visitor : public boost::static_visitor<void>
        void operator()(const std::vector<int8_t>& value)       {o << ::osc::Blob(value.data(), static_cast<unsigned long>(value.size()));}
 };
 
-void write_osc_event(byte_vector& destination, const core::monitor::message& e)
-{              
-       destination.resize(4096);
+void write_osc_event(byte_vector& destination, const core::monitor::message& message, int retry_allocation_attempt = 0)
+{
+       static std::size_t max_size = 128;
+
+       destination.resize(max_size);
 
        ::osc::OutboundPacketStream o(reinterpret_cast<char*>(destination.data()), static_cast<unsigned long>(destination.size()));
-       o << ::osc::BeginMessage(e.path().c_str());
-                               
-       param_visitor<decltype(o)> param_visitor(o);
-       for (const auto& data : e.data())
-               boost::apply_visitor(param_visitor, data);
-                               
-       o << ::osc::EndMessage;
-               
+
+       try
+       {
+               o << ::osc::BeginMessage(message.path().c_str());
+
+               param_visitor<decltype(o)> param_visitor(o);
+               for (const auto& data : message.data())
+                       boost::apply_visitor(param_visitor, data);
+
+               o << ::osc::EndMessage;
+       }
+       catch (const ::osc::OutOfBufferMemoryException& e)
+       {
+               if (retry_allocation_attempt > message.data().size())
+                       throw;
+
+               max_size = e.required;
+               CASPAR_LOG(trace) << L"[osc] Too small buffer for osc message. Increasing to " << max_size;
+               return write_osc_event(destination, message, retry_allocation_attempt + 1);
+       }
+
        destination.resize(o.Size());
 }
 
@@ -127,7 +144,8 @@ void write_osc_bundle_element_start(byte_vector& destination, const byte_vector&
 
 struct client::impl : public spl::enable_shared_from_this<client::impl>, core::monitor::sink
 {
-       udp::socket socket_;
+       std::shared_ptr<boost::asio::io_service>                service_;
+       udp::socket                                                                             socket_;
        tbb::spin_mutex                                                                 endpoints_mutex_;
        std::map<udp::endpoint, int>                                    reference_counts_by_endpoint_;
 
@@ -140,8 +158,9 @@ struct client::impl : public spl::enable_shared_from_this<client::impl>, core::m
        boost::thread                                                                   thread_;
        
 public:
-       impl(boost::asio::io_service& service)
-               : socket_(service, udp::v4())
+       impl(std::shared_ptr<boost::asio::io_service> service)
+               : service_(std::move(service))
+               , socket_(*service_, udp::v4())
                , thread_(boost::bind(&impl::run, this))
        {
        }
@@ -215,6 +234,8 @@ private:
                // http://stackoverflow.com/questions/14993000/the-most-reliable-and-efficient-udp-packet-size
                const int SAFE_DATAGRAM_SIZE = 508;
 
+               ensure_gpf_handler_installed_for_thread("osc-sender-thread");
+
                try
                {
                        is_running_ = true;
@@ -232,6 +253,9 @@ private:
                                {                       
                                        boost::unique_lock<boost::mutex> cond_lock(updates_mutex_);
 
+                                       if (!is_running_)
+                                               return;
+
                                        if (updates_.empty())
                                                updates_cond_.wait(cond_lock);
 
@@ -289,8 +313,8 @@ private:
        }
 };
 
-client::client(boost::asio::io_service& service) 
-       : impl_(new impl(service))
+client::client(std::shared_ptr<boost::asio::io_service> service)
+       : impl_(new impl(std::move(service)))
 {
 }