Move Socket to dcpomatic_socket.{cc,h}.
authorCarl Hetherington <cth@carlh.net>
Thu, 29 Jan 2015 00:37:06 +0000 (00:37 +0000)
committerCarl Hetherington <cth@carlh.net>
Thu, 29 Jan 2015 00:37:06 +0000 (00:37 +0000)
14 files changed:
src/lib/colour_conversion.cc
src/lib/colour_conversion.h
src/lib/dcp_video.cc
src/lib/dcpomatic_socket.cc [new file with mode: 0644]
src/lib/dcpomatic_socket.h [new file with mode: 0644]
src/lib/encoded_data.cc
src/lib/image.cc
src/lib/image.h
src/lib/j2k_image_proxy.cc
src/lib/magick_image_proxy.cc
src/lib/server.cc
src/lib/util.cc
src/lib/util.h
src/lib/wscript

index 028912f2b482b2c00d281671724094cab834b407..6c5a6679f7fcdff23b565d4884ee14d1f0933662 100644 (file)
@@ -185,8 +185,8 @@ PresetColourConversion::PresetColourConversion ()
 }
 
 PresetColourConversion::PresetColourConversion (string n, dcp::ColourConversion conversion_)
-       : name (n)
-       , conversion (conversion_)
+       : conversion (conversion_)
+       , name (n)
 {
 
 }
index ae207b46aa3acc5ddd10a4df1c837d3cf052648f..47e6d2b6f873670eb2b0b52fa325f24bcf6c097a 100644 (file)
@@ -57,8 +57,8 @@ public:
 
        void as_xml (xmlpp::Node *) const;
 
-       std::string name;
        ColourConversion conversion;
+       std::string name;
 };
 
 bool operator== (ColourConversion const &, ColourConversion const &);
index 559aef7c6105ac47ec8f4f43c90c2deb9c99ed47..a01a72f6b83aebb0a698a957549ea8a37019cbc7 100644 (file)
@@ -32,7 +32,7 @@
 #include "config.h"
 #include "exceptions.h"
 #include "server.h"
-#include "util.h"
+#include "dcpomatic_socket.h"
 #include "scaler.h"
 #include "image.h"
 #include "log.h"
diff --git a/src/lib/dcpomatic_socket.cc b/src/lib/dcpomatic_socket.cc
new file mode 100644 (file)
index 0000000..bc8f596
--- /dev/null
@@ -0,0 +1,156 @@
+/*
+    Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+*/
+
+#include "dcpomatic_socket.h"
+#include "compose.hpp"
+#include "exceptions.h"
+#include <boost/bind.hpp>
+#include <boost/lambda/lambda.hpp>
+
+#include "i18n.h"
+
+Socket::Socket (int timeout)
+       : _deadline (_io_service)
+       , _socket (_io_service)
+       , _acceptor (0)
+       , _timeout (timeout)
+{
+       _deadline.expires_at (boost::posix_time::pos_infin);
+       check ();
+}
+
+Socket::~Socket ()
+{
+       delete _acceptor;
+}
+
+void
+Socket::check ()
+{
+       if (_deadline.expires_at() <= boost::asio::deadline_timer::traits_type::now ()) {
+               if (_acceptor) {
+                       _acceptor->cancel ();
+               } else {
+                       _socket.close ();
+               }
+               _deadline.expires_at (boost::posix_time::pos_infin);
+       }
+
+       _deadline.async_wait (boost::bind (&Socket::check, this));
+}
+
+/** Blocking connect.
+ *  @param endpoint End-point to connect to.
+ */
+void
+Socket::connect (boost::asio::ip::tcp::endpoint endpoint)
+{
+       _deadline.expires_from_now (boost::posix_time::seconds (_timeout));
+       boost::system::error_code ec = boost::asio::error::would_block;
+       _socket.async_connect (endpoint, boost::lambda::var(ec) = boost::lambda::_1);
+       do {
+               _io_service.run_one();
+       } while (ec == boost::asio::error::would_block);
+
+       if (ec) {
+               throw NetworkError (String::compose (_("error during async_connect (%1)"), ec.value ()));
+       }
+
+       if (!_socket.is_open ()) {
+               throw NetworkError (_("connect timed out"));
+       }
+}
+
+void
+Socket::accept (int port)
+{
+       _acceptor = new boost::asio::ip::tcp::acceptor (_io_service, boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), port));
+       
+       _deadline.expires_from_now (boost::posix_time::seconds (_timeout));
+       boost::system::error_code ec = boost::asio::error::would_block;
+       _acceptor->async_accept (_socket, boost::lambda::var(ec) = boost::lambda::_1);
+       do {
+               _io_service.run_one ();
+       } while (ec == boost::asio::error::would_block);
+
+       delete _acceptor;
+       _acceptor = 0;
+       
+       if (ec) {
+               throw NetworkError (String::compose (_("error during async_accept (%1)"), ec.value ()));
+       }
+}
+
+/** Blocking write.
+ *  @param data Buffer to write.
+ *  @param size Number of bytes to write.
+ */
+void
+Socket::write (uint8_t const * data, int size)
+{
+       _deadline.expires_from_now (boost::posix_time::seconds (_timeout));
+       boost::system::error_code ec = boost::asio::error::would_block;
+
+       boost::asio::async_write (_socket, boost::asio::buffer (data, size), boost::lambda::var(ec) = boost::lambda::_1);
+       
+       do {
+               _io_service.run_one ();
+       } while (ec == boost::asio::error::would_block);
+
+       if (ec) {
+               throw NetworkError (String::compose (_("error during async_write (%1)"), ec.value ()));
+       }
+}
+
+void
+Socket::write (uint32_t v)
+{
+       v = htonl (v);
+       write (reinterpret_cast<uint8_t*> (&v), 4);
+}
+
+/** Blocking read.
+ *  @param data Buffer to read to.
+ *  @param size Number of bytes to read.
+ */
+void
+Socket::read (uint8_t* data, int size)
+{
+       _deadline.expires_from_now (boost::posix_time::seconds (_timeout));
+       boost::system::error_code ec = boost::asio::error::would_block;
+
+       boost::asio::async_read (_socket, boost::asio::buffer (data, size), boost::lambda::var(ec) = boost::lambda::_1);
+
+       do {
+               _io_service.run_one ();
+       } while (ec == boost::asio::error::would_block);
+       
+       if (ec) {
+               throw NetworkError (String::compose (_("error during async_read (%1)"), ec.value ()));
+       }
+}
+
+uint32_t
+Socket::read_uint32 ()
+{
+       uint32_t v;
+       read (reinterpret_cast<uint8_t *> (&v), 4);
+       return ntohl (v);
+}
+
diff --git a/src/lib/dcpomatic_socket.h b/src/lib/dcpomatic_socket.h
new file mode 100644 (file)
index 0000000..d2ee6d5
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+    Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+*/
+
+#include <boost/asio.hpp>
+
+/** @class Socket
+ *  @brief A class to wrap a boost::asio::ip::tcp::socket with some things
+ *  that are useful for DCP-o-matic.
+ *
+ *  This class wraps some things that I could not work out how to do with boost;
+ *  most notably, sync read/write calls with timeouts.
+ */
+class Socket
+{
+public:
+       Socket (int timeout = 30);
+       ~Socket ();
+
+       /** @return Our underlying socket */
+       boost::asio::ip::tcp::socket& socket () {
+               return _socket;
+       }
+
+       void connect (boost::asio::ip::tcp::endpoint);
+       void accept (int);
+
+       void write (uint32_t n);
+       void write (uint8_t const * data, int size);
+       
+       void read (uint8_t* data, int size);
+       uint32_t read_uint32 ();
+       
+private:
+       void check ();
+
+       Socket (Socket const &);
+
+       boost::asio::io_service _io_service;
+       boost::asio::deadline_timer _deadline;
+       boost::asio::ip::tcp::socket _socket;
+       boost::asio::ip::tcp::acceptor* _acceptor;
+       int _timeout;
+};
index 61d2644daf324dd251861dbfe686ff93e514dc31..1b1926017993c5da335e6cada7211394c47b86ac 100644 (file)
@@ -21,6 +21,7 @@
 #include "cross.h"
 #include "exceptions.h"
 #include "film.h"
+#include "dcpomatic_socket.h"
 
 #include "i18n.h"
 
index d16de5e551b46b2badfb50424bceeefc645fedea..3df498c874efa6ac02885a88f7a101c6bb1deb4e 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
  *  @brief A class to describe a video image.
  */
 
-#include <iostream>
-extern "C" {
-#include <libswscale/swscale.h>
-#include <libavutil/pixfmt.h>
-#include <libavutil/pixdesc.h>
-}
 #include "image.h"
 #include "exceptions.h"
 #include "scaler.h"
 #include "timer.h"
 #include "rect.h"
+#include "util.h"
 #include "md5_digester.h"
+#include "dcpomatic_socket.h"
+extern "C" {
+#include <libswscale/swscale.h>
+#include <libavutil/pixfmt.h>
+#include <libavutil/pixdesc.h>
+}
+#include <iostream>
 
 #include "i18n.h"
 
index 22f3f6e502ca6f6775f7c71265220efe9d84d399..814ad1c5895a8c7d6edd8f5ed7563c4479c306cd 100644 (file)
@@ -24,9 +24,9 @@
 #ifndef DCPOMATIC_IMAGE_H
 #define DCPOMATIC_IMAGE_H
 
-#include "util.h"
 #include "position.h"
 #include "position_image.h"
+#include "types.h"
 #include <dcp/image.h>
 extern "C" {
 #include <libavcodec/avcodec.h>
@@ -37,6 +37,7 @@ extern "C" {
 #include <string>
 
 class Scaler;
+class Socket;
 
 class Image : public dcp::Image
 {
index 175f4c796e6afc039cd6f216da68a608257463dd..09eaa4695beff11a0ba446c074b6cb0ab99f1145 100644 (file)
@@ -22,7 +22,7 @@
 #include <dcp/mono_picture_frame.h>
 #include <dcp/stereo_picture_frame.h>
 #include "j2k_image_proxy.h"
-#include "util.h"
+#include "dcpomatic_socket.h"
 #include "image.h"
 #include "encoded_data.h"
 
index c9cddd8997fc71ae1d3d4c58f037d4c289b807c0..e4417f3665e85d0e15163bd0ffccde7b1fa2f23b 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2014-2015 Carl Hetherington <cth@carlh.net>
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -21,8 +21,9 @@
 #include "magick_image_proxy.h"
 #include "cross.h"
 #include "exceptions.h"
-#include "util.h"
+#include "dcpomatic_socket.h"
 #include "image.h"
+#include "compose.hpp"
 
 #include "i18n.h"
 
index 0212356f3ea90e6537af41544757c8248df52c82..32e1e38b23f7148e0f7f1fa132fe2beaa4a8c0c4 100644 (file)
  *  encoding work, and a class to implement such a server.
  */
 
-#include <string>
-#include <vector>
-#include <iostream>
-#include <boost/algorithm/string.hpp>
-#include <boost/scoped_array.hpp>
-#include <libcxml/cxml.h>
-#include <dcp/raw_convert.h>
 #include "server.h"
-#include "util.h"
+#include "dcpomatic_socket.h"
 #include "scaler.h"
 #include "image.h"
 #include "dcp_video.h"
 #include "player_video.h"
 #include "encoded_data.h"
 #include "safe_stringstream.h"
+#include <dcp/raw_convert.h>
+#include <libcxml/cxml.h>
+#include <boost/algorithm/string.hpp>
+#include <boost/scoped_array.hpp>
+#include <string>
+#include <vector>
+#include <iostream>
 
 #include "i18n.h"
 
index 807883ca0906ffc787ff8da0f54533c50566df9e..c298a1946be278847e905693e4f9f663eeb13a86 100644 (file)
@@ -480,135 +480,6 @@ dcp_audio_frame_rate (int fs)
        return 96000;
 }
 
-Socket::Socket (int timeout)
-       : _deadline (_io_service)
-       , _socket (_io_service)
-       , _acceptor (0)
-       , _timeout (timeout)
-{
-       _deadline.expires_at (boost::posix_time::pos_infin);
-       check ();
-}
-
-Socket::~Socket ()
-{
-       delete _acceptor;
-}
-
-void
-Socket::check ()
-{
-       if (_deadline.expires_at() <= boost::asio::deadline_timer::traits_type::now ()) {
-               if (_acceptor) {
-                       _acceptor->cancel ();
-               } else {
-                       _socket.close ();
-               }
-               _deadline.expires_at (boost::posix_time::pos_infin);
-       }
-
-       _deadline.async_wait (boost::bind (&Socket::check, this));
-}
-
-/** Blocking connect.
- *  @param endpoint End-point to connect to.
- */
-void
-Socket::connect (boost::asio::ip::tcp::endpoint endpoint)
-{
-       _deadline.expires_from_now (boost::posix_time::seconds (_timeout));
-       boost::system::error_code ec = boost::asio::error::would_block;
-       _socket.async_connect (endpoint, boost::lambda::var(ec) = boost::lambda::_1);
-       do {
-               _io_service.run_one();
-       } while (ec == boost::asio::error::would_block);
-
-       if (ec) {
-               throw NetworkError (String::compose (_("error during async_connect (%1)"), ec.value ()));
-       }
-
-       if (!_socket.is_open ()) {
-               throw NetworkError (_("connect timed out"));
-       }
-}
-
-void
-Socket::accept (int port)
-{
-       _acceptor = new boost::asio::ip::tcp::acceptor (_io_service, boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), port));
-       
-       _deadline.expires_from_now (boost::posix_time::seconds (_timeout));
-       boost::system::error_code ec = boost::asio::error::would_block;
-       _acceptor->async_accept (_socket, boost::lambda::var(ec) = boost::lambda::_1);
-       do {
-               _io_service.run_one ();
-       } while (ec == boost::asio::error::would_block);
-
-       delete _acceptor;
-       _acceptor = 0;
-       
-       if (ec) {
-               throw NetworkError (String::compose (_("error during async_accept (%1)"), ec.value ()));
-       }
-}
-
-/** Blocking write.
- *  @param data Buffer to write.
- *  @param size Number of bytes to write.
- */
-void
-Socket::write (uint8_t const * data, int size)
-{
-       _deadline.expires_from_now (boost::posix_time::seconds (_timeout));
-       boost::system::error_code ec = boost::asio::error::would_block;
-
-       boost::asio::async_write (_socket, boost::asio::buffer (data, size), boost::lambda::var(ec) = boost::lambda::_1);
-       
-       do {
-               _io_service.run_one ();
-       } while (ec == boost::asio::error::would_block);
-
-       if (ec) {
-               throw NetworkError (String::compose (_("error during async_write (%1)"), ec.value ()));
-       }
-}
-
-void
-Socket::write (uint32_t v)
-{
-       v = htonl (v);
-       write (reinterpret_cast<uint8_t*> (&v), 4);
-}
-
-/** Blocking read.
- *  @param data Buffer to read to.
- *  @param size Number of bytes to read.
- */
-void
-Socket::read (uint8_t* data, int size)
-{
-       _deadline.expires_from_now (boost::posix_time::seconds (_timeout));
-       boost::system::error_code ec = boost::asio::error::would_block;
-
-       boost::asio::async_read (_socket, boost::asio::buffer (data, size), boost::lambda::var(ec) = boost::lambda::_1);
-
-       do {
-               _io_service.run_one ();
-       } while (ec == boost::asio::error::would_block);
-       
-       if (ec) {
-               throw NetworkError (String::compose (_("error during async_read (%1)"), ec.value ()));
-       }
-}
-
-uint32_t
-Socket::read_uint32 ()
-{
-       uint32_t v;
-       read (reinterpret_cast<uint8_t *> (&v), 4);
-       return ntohl (v);
-}
-
 /** Round a number up to the nearest multiple of another number.
  *  @param c Index.
  *  @param s Array of numbers to round, indexed by c.
index ee2865e763df84a2328c5b05064d4add768e43fc..c17dbf05bb7c61b75a4b6a6628c1f2f95b7238ed 100644 (file)
@@ -34,7 +34,6 @@ extern "C" {
 #include <libavfilter/avfilter.h>
 }
 #include <boost/shared_ptr.hpp>
-#include <boost/asio.hpp>
 #include <boost/optional.hpp>
 #include <boost/filesystem.hpp>
 #include <string>
@@ -77,46 +76,6 @@ extern int round_to (float n, int r);
 extern void* wrapped_av_malloc (size_t);
 extern ContentTimePeriod subtitle_period (AVSubtitle const &);
 extern void set_backtrace_file (boost::filesystem::path);
-
-/** @class Socket
- *  @brief A class to wrap a boost::asio::ip::tcp::socket with some things
- *  that are useful for DCP-o-matic.
- *
- *  This class wraps some things that I could not work out how to do with boost;
- *  most notably, sync read/write calls with timeouts.
- */
-class Socket
-{
-public:
-       Socket (int timeout = 30);
-       ~Socket ();
-
-       /** @return Our underlying socket */
-       boost::asio::ip::tcp::socket& socket () {
-               return _socket;
-       }
-
-       void connect (boost::asio::ip::tcp::endpoint);
-       void accept (int);
-
-       void write (uint32_t n);
-       void write (uint8_t const * data, int size);
-       
-       void read (uint8_t* data, int size);
-       uint32_t read_uint32 ();
-       
-private:
-       void check ();
-
-       Socket (Socket const &);
-
-       boost::asio::io_service _io_service;
-       boost::asio::deadline_timer _deadline;
-       boost::asio::ip::tcp::socket _socket;
-       boost::asio::ip::tcp::acceptor* _acceptor;
-       int _timeout;
-};
-
 extern int64_t video_frames_to_audio_frames (VideoFrame v, float audio_sample_rate, float frames_per_second);
 
 #endif
index e3c8def4e77705eba36e24a655b1cf08e946d84b..bad426258c2492b923837fb63588acfe9729116f 100644 (file)
@@ -26,6 +26,7 @@ sources = """
           dcp_subtitle_content.cc
           dcp_subtitle_decoder.cc
           dcp_video.cc
+          dcpomatic_socket.cc
           dcpomatic_time.cc
           dolby_cp750.cc
           encoder.cc