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