]> git.sesse.net Git - casparcg/commitdiff
osc:
authorRobert Nagy <ronag@live.com>
Sun, 7 Jul 2013 10:47:05 +0000 (12:47 +0200)
committerRobert Nagy <ronag@live.com>
Sun, 7 Jul 2013 10:47:05 +0000 (12:47 +0200)
-Drop invalid updates.
-Improved update size calculation.

protocol/osc/client.cpp

index 30e9615e1acf00557686c488ea9e6ca42191edb9..2dbb4eba93a11cf8f56b7914c0c2a11bcd66983e 100644 (file)
@@ -61,45 +61,44 @@ struct param_visitor : public boost::static_visitor<void>
 
 struct size_visitor : public boost::static_visitor<std::size_t>
 {              
-       std::size_t operator()(const bool value)                                        { return sizeof(bool); }
-       std::size_t operator()(const int32_t value)                                     { return sizeof(int64_t); }
-       std::size_t operator()(const uint32_t value)                            { return sizeof(int64_t); }
-       std::size_t operator()(const int64_t value)                                     { return sizeof(int64_t); }
-       std::size_t operator()(const uint64_t value)                            { return sizeof(int64_t); }
-       std::size_t operator()(const float value)                                       { return sizeof(float); }
-       std::size_t operator()(const double value)                                      { return sizeof(float); }
-       std::size_t operator()(const std::string& value)                        { return value.size(); }
-       std::size_t operator()(const std::wstring& value)                       { return value.size(); }
-       std::size_t operator()(const std::vector<int8_t>& value)        { return value.size(); }
+       static inline long round_up(long x)
+       {
+               return ((x-1) & (~0x03L)) + 4;
+       }
+
+       std::size_t operator()(const bool value)                                        { return round_up(sizeof(bool)); }
+       std::size_t operator()(const int32_t value)                                     { return round_up(sizeof(int64_t)); }
+       std::size_t operator()(const uint32_t value)                            { return round_up(sizeof(int64_t)); }
+       std::size_t operator()(const int64_t value)                                     { return round_up(sizeof(int64_t)); }
+       std::size_t operator()(const uint64_t value)                            { return round_up(sizeof(int64_t)); }
+       std::size_t operator()(const float value)                                       { return round_up(sizeof(float)); }
+       std::size_t operator()(const double value)                                      { return round_up(sizeof(float)); }
+       std::size_t operator()(const std::string& value)                        { return round_up(value.size() + 1); }
+       std::size_t operator()(const std::wstring& value)                       { return round_up(value.size() + 1); }
+       std::size_t operator()(const std::vector<int8_t>& value)        { return round_up(value.size()); }
 };
 
 void write_osc_event(byte_vector& destination, const core::monitor::message& e)
 {
-       try
-       {
-               std::size_t size = 256; // This should be enough to cover address, padding and meta-data.
+       std::size_t size = 256; // This should be enough to cover address, padding and meta-data.
                
-               size_visitor size_visitor;
-               BOOST_FOREACH(auto& data, e.data())
-                       size += boost::apply_visitor(size_visitor, data);
+       size_visitor size_visitor;
+       BOOST_FOREACH(const auto& data, e.data())
+               size += boost::apply_visitor(size_visitor, data);
 
-               destination.resize(size);
+       destination.resize(size);
 
-               ::osc::OutboundPacketStream o(reinterpret_cast<char*>(destination.data()), static_cast<unsigned long>(destination.size()));
+       ::osc::OutboundPacketStream o(reinterpret_cast<char*>(destination.data()), static_cast<unsigned long>(destination.size()));
 
-               o << ::osc::BeginMessage(e.path().c_str());
+       o << ::osc::BeginMessage(e.path().c_str());
                                
-               param_visitor<decltype(o)> param_visitor(o);
-               BOOST_FOREACH(auto& data, e.data())
-                       boost::apply_visitor(param_visitor, data);
+       param_visitor<decltype(o)> param_visitor(o);
+       BOOST_FOREACH(const auto& data, e.data())
+               boost::apply_visitor(param_visitor, data);
                                
-               o << ::osc::EndMessage;
+       o << ::osc::EndMessage;
                
-               destination.resize(o.Size());
-       }
-       catch(...)
-       {
-       }
+       destination.resize(o.Size());
 }
 
 struct client::impl
@@ -145,7 +144,15 @@ public:
        {                               
                {
                        boost::lock_guard<boost::mutex> lock(updates_mutex_);
-                       write_osc_event(updates_[msg.path()], msg);
+
+                       try
+                       {
+                               write_osc_event(updates_[msg.path()], msg);
+                       }
+                       catch(...)
+                       {
+                               updates_.erase(msg.path());
+                       }
                }
 
                cond_.notify_all();