Rename TYPE_DEBUG_PLAYER to TYPE_DEBUG_VIDEO_VIEW.
[dcpomatic.git] / src / lib / dcpomatic_socket.cc
index 16f01f39c23e8c0a1e411209a17a23349100386c..a0a7a1cf3f3d877ee92aa204c2ad3c23f162a754 100644 (file)
@@ -1,30 +1,37 @@
 /*
-    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 "exceptions.h"
+#include "dcpomatic_assert.h"
 #include <boost/bind.hpp>
 #include <boost/lambda/lambda.hpp>
+#include <iostream>
 
 #include "i18n.h"
 
+using boost::shared_ptr;
+using boost::weak_ptr;
+
+/** @param timeout Timeout in seconds */
 Socket::Socket (int timeout)
        : _deadline (_io_service)
        , _socket (_io_service)
@@ -86,6 +93,10 @@ 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
@@ -114,6 +125,10 @@ Socket::read (uint8_t* data, int size)
        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
@@ -124,3 +139,97 @@ 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 ()
+{
+       shared_ptr<Socket> 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 ()
+{
+       shared_ptr<Socket> 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);
+}
+