* Author: Helge Norberg, helge.norberg@svt.se
*/
-#include "../stdafx.h"
+#include "../StdAfx.h"
#include "client.h"
#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>
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());
}
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_;
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))
{
}
// 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;
{
boost::unique_lock<boost::mutex> cond_lock(updates_mutex_);
+ if (!is_running_)
+ return;
+
if (updates_.empty())
updates_cond_.wait(cond_lock);
}
};
-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)))
{
}