Fix typo in log message.
[dcpomatic.git] / src / lib / dcpomatic_socket.cc
index d575e323027a4e77ed14afa9802d8ae1e82c657f..014e498e636e1578d7121c85405cfca7871229cf 100644 (file)
@@ -1,30 +1,41 @@
 /*
-    Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2012-2020 Carl Hetherington <cth@carlh.net>
 
-    This program is free software; you can redistribute it and/or modify
+    This file is part of DCP-o-matic.
+
+    DCP-o-matic 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,
+    DCP-o-matic 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.
+    along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
 
 */
 
-#include "dcpomatic_socket.h"
+
 #include "compose.hpp"
+#include "dcpomatic_assert.h"
+#include "dcpomatic_log.h"
+#include "dcpomatic_socket.h"
 #include "exceptions.h"
-#include <boost/bind.hpp>
+#include <boost/bind/bind.hpp>
 #include <boost/lambda/lambda.hpp>
+#include <iostream>
 
 #include "i18n.h"
 
+
+using std::shared_ptr;
+using std::weak_ptr;
+
+
+/** @param timeout Timeout in seconds */
 Socket::Socket (int timeout)
        : _deadline (_io_service)
        , _socket (_io_service)
@@ -45,6 +56,7 @@ Socket::check ()
        _deadline.async_wait (boost::bind (&Socket::check, this));
 }
 
+
 /** Blocking connect.
  *  @param endpoint End-point to connect to.
  */
@@ -65,8 +77,19 @@ Socket::connect (boost::asio::ip::tcp::endpoint endpoint)
        if (!_socket.is_open ()) {
                throw NetworkError (_("connect timed out"));
        }
+
+       if (_send_buffer_size) {
+               boost::asio::socket_base::send_buffer_size old_size;
+               _socket.get_option(old_size);
+
+               boost::asio::socket_base::send_buffer_size new_size(*_send_buffer_size);
+               _socket.set_option(new_size);
+
+               LOG_GENERAL("Changed socket send buffer size from %1 to %2", old_size.value(), *_send_buffer_size);
+       }
 }
 
+
 /** Blocking write.
  *  @param data Buffer to write.
  *  @param size Number of bytes to write.
@@ -78,7 +101,7 @@ Socket::write (uint8_t const * data, int size)
        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);
@@ -86,8 +109,13 @@ Socket::write (uint8_t const * data, int size)
        if (ec) {
                throw NetworkError (String::compose (_("error during async_write (%1)"), ec.value ()));
        }
+
+       if (_write_digester) {
+               _write_digester->add (data, static_cast<size_t>(size));
+       }
 }
 
+
 void
 Socket::write (uint32_t v)
 {
@@ -95,6 +123,7 @@ Socket::write (uint32_t v)
        write (reinterpret_cast<uint8_t*> (&v), 4);
 }
 
+
 /** Blocking read.
  *  @param data Buffer to read to.
  *  @param size Number of bytes to read.
@@ -110,12 +139,17 @@ Socket::read (uint8_t* data, int size)
        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 ()));
        }
+
+       if (_read_digester) {
+               _read_digester->add (data, static_cast<size_t>(size));
+       }
 }
 
+
 uint32_t
 Socket::read_uint32 ()
 {
@@ -124,3 +158,106 @@ Socket::read_uint32 ()
        return ntohl (v);
 }
 
+
+void
+Socket::start_read_digest ()
+{
+       DCPOMATIC_ASSERT (!_read_digester);
+       _read_digester.reset (new Digester());
+}
+
+
+void
+Socket::start_write_digest ()
+{
+       DCPOMATIC_ASSERT (!_write_digester);
+       _write_digester.reset (new Digester());
+}
+
+
+Socket::ReadDigestScope::ReadDigestScope (shared_ptr<Socket> socket)
+       : _socket (socket)
+{
+       socket->start_read_digest ();
+}
+
+
+bool
+Socket::ReadDigestScope::check ()
+{
+       auto sp = _socket.lock ();
+       if (!sp) {
+               return false;
+       }
+
+       return sp->check_read_digest ();
+}
+
+
+Socket::WriteDigestScope::WriteDigestScope (shared_ptr<Socket> socket)
+       : _socket (socket)
+{
+       socket->start_write_digest ();
+}
+
+
+Socket::WriteDigestScope::~WriteDigestScope ()
+{
+       auto sp = _socket.lock ();
+       if (sp) {
+               try {
+                       sp->finish_write_digest ();
+               } catch (...) {
+                       /* If we can't write our digest, something bad has happened
+                        * so let's just let it happen.
+                        */
+               }
+       }
+}
+
+
+bool
+Socket::check_read_digest ()
+{
+       DCPOMATIC_ASSERT (_read_digester);
+       int const size = _read_digester->size ();
+
+       uint8_t ref[size];
+       _read_digester->get (ref);
+
+       /* Make sure _read_digester is gone before we call read() so that the digest
+        * isn't itself digested.
+        */
+       _read_digester.reset ();
+
+       uint8_t actual[size];
+       read (actual, size);
+
+       return memcmp(ref, actual, size) == 0;
+}
+
+
+void
+Socket::finish_write_digest ()
+{
+       DCPOMATIC_ASSERT (_write_digester);
+       int const size = _write_digester->size();
+
+       uint8_t buffer[size];
+       _write_digester->get (buffer);
+
+       /* Make sure _write_digester is gone before we call write() so that the digest
+        * isn't itself digested.
+        */
+       _write_digester.reset ();
+
+       write (buffer, size);
+}
+
+
+void
+Socket::set_send_buffer_size (int size)
+{
+       _send_buffer_size = size;
+}
+