No-op; fix GPL address and use the explicit-program-name 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 Socket::Socket (int timeout)
31         : _deadline (_io_service)
32         , _socket (_io_service)
33         , _timeout (timeout)
34 {
35         _deadline.expires_at (boost::posix_time::pos_infin);
36         check ();
37 }
38
39 void
40 Socket::check ()
41 {
42         if (_deadline.expires_at() <= boost::asio::deadline_timer::traits_type::now ()) {
43                 _socket.close ();
44                 _deadline.expires_at (boost::posix_time::pos_infin);
45         }
46
47         _deadline.async_wait (boost::bind (&Socket::check, this));
48 }
49
50 /** Blocking connect.
51  *  @param endpoint End-point to connect to.
52  */
53 void
54 Socket::connect (boost::asio::ip::tcp::endpoint endpoint)
55 {
56         _deadline.expires_from_now (boost::posix_time::seconds (_timeout));
57         boost::system::error_code ec = boost::asio::error::would_block;
58         _socket.async_connect (endpoint, boost::lambda::var(ec) = boost::lambda::_1);
59         do {
60                 _io_service.run_one();
61         } while (ec == boost::asio::error::would_block);
62
63         if (ec) {
64                 throw NetworkError (String::compose (_("error during async_connect (%1)"), ec.value ()));
65         }
66
67         if (!_socket.is_open ()) {
68                 throw NetworkError (_("connect timed out"));
69         }
70 }
71
72 /** Blocking write.
73  *  @param data Buffer to write.
74  *  @param size Number of bytes to write.
75  */
76 void
77 Socket::write (uint8_t const * data, int size)
78 {
79         _deadline.expires_from_now (boost::posix_time::seconds (_timeout));
80         boost::system::error_code ec = boost::asio::error::would_block;
81
82         boost::asio::async_write (_socket, boost::asio::buffer (data, size), boost::lambda::var(ec) = boost::lambda::_1);
83
84         do {
85                 _io_service.run_one ();
86         } while (ec == boost::asio::error::would_block);
87
88         if (ec) {
89                 throw NetworkError (String::compose (_("error during async_write (%1)"), ec.value ()));
90         }
91 }
92
93 void
94 Socket::write (uint32_t v)
95 {
96         v = htonl (v);
97         write (reinterpret_cast<uint8_t*> (&v), 4);
98 }
99
100 /** Blocking read.
101  *  @param data Buffer to read to.
102  *  @param size Number of bytes to read.
103  */
104 void
105 Socket::read (uint8_t* data, int size)
106 {
107         _deadline.expires_from_now (boost::posix_time::seconds (_timeout));
108         boost::system::error_code ec = boost::asio::error::would_block;
109
110         boost::asio::async_read (_socket, boost::asio::buffer (data, size), boost::lambda::var(ec) = boost::lambda::_1);
111
112         do {
113                 _io_service.run_one ();
114         } while (ec == boost::asio::error::would_block);
115
116         if (ec) {
117                 throw NetworkError (String::compose (_("error during async_read (%1)"), ec.value ()));
118         }
119 }
120
121 uint32_t
122 Socket::read_uint32 ()
123 {
124         uint32_t v;
125         read (reinterpret_cast<uint8_t *> (&v), 4);
126         return ntohl (v);
127 }