Missed update to private test repo version.
[dcpomatic.git] / src / lib / dcpomatic_socket.cc
1 /*
2     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "dcpomatic_socket.h"
22 #include "compose.hpp"
23 #include "exceptions.h"
24 #include <boost/bind.hpp>
25 #include <boost/lambda/lambda.hpp>
26 #include <iostream>
27
28 #include "i18n.h"
29
30 /** @param timeout Timeout in seconds */
31 Socket::Socket (int timeout)
32         : _deadline (_io_service)
33         , _socket (_io_service)
34         , _timeout (timeout)
35 {
36         _deadline.expires_at (boost::posix_time::pos_infin);
37         check ();
38 }
39
40 void
41 Socket::check ()
42 {
43         if (_deadline.expires_at() <= boost::asio::deadline_timer::traits_type::now ()) {
44                 _socket.close ();
45                 _deadline.expires_at (boost::posix_time::pos_infin);
46         }
47
48         _deadline.async_wait (boost::bind (&Socket::check, this));
49 }
50
51 /** Blocking connect.
52  *  @param endpoint End-point to connect to.
53  */
54 void
55 Socket::connect (boost::asio::ip::tcp::endpoint endpoint)
56 {
57         _deadline.expires_from_now (boost::posix_time::seconds (_timeout));
58         boost::system::error_code ec = boost::asio::error::would_block;
59         _socket.async_connect (endpoint, boost::lambda::var(ec) = boost::lambda::_1);
60         do {
61                 _io_service.run_one();
62         } while (ec == boost::asio::error::would_block);
63
64         if (ec) {
65                 throw NetworkError (String::compose (_("error during async_connect (%1)"), ec.value ()));
66         }
67
68         if (!_socket.is_open ()) {
69                 throw NetworkError (_("connect timed out"));
70         }
71 }
72
73 /** Blocking write.
74  *  @param data Buffer to write.
75  *  @param size Number of bytes to write.
76  */
77 void
78 Socket::write (uint8_t const * data, int size)
79 {
80         _deadline.expires_from_now (boost::posix_time::seconds (_timeout));
81         boost::system::error_code ec = boost::asio::error::would_block;
82
83         boost::asio::async_write (_socket, boost::asio::buffer (data, size), boost::lambda::var(ec) = boost::lambda::_1);
84
85         do {
86                 _io_service.run_one ();
87         } while (ec == boost::asio::error::would_block);
88
89         if (ec) {
90                 throw NetworkError (String::compose (_("error during async_write (%1)"), ec.value ()));
91         }
92 }
93
94 void
95 Socket::write (uint32_t v)
96 {
97         v = htonl (v);
98         write (reinterpret_cast<uint8_t*> (&v), 4);
99 }
100
101 /** Blocking read.
102  *  @param data Buffer to read to.
103  *  @param size Number of bytes to read.
104  */
105 void
106 Socket::read (uint8_t* data, int size)
107 {
108         _deadline.expires_from_now (boost::posix_time::seconds (_timeout));
109         boost::system::error_code ec = boost::asio::error::would_block;
110
111         boost::asio::async_read (_socket, boost::asio::buffer (data, size), boost::lambda::var(ec) = boost::lambda::_1);
112
113         do {
114                 _io_service.run_one ();
115         } while (ec == boost::asio::error::would_block);
116
117         if (ec) {
118                 throw NetworkError (String::compose (_("error during async_read (%1)"), ec.value ()));
119         }
120 }
121
122 uint32_t
123 Socket::read_uint32 ()
124 {
125         uint32_t v;
126         read (reinterpret_cast<uint8_t *> (&v), 4);
127         return ntohl (v);
128 }