Move Socket to dcpomatic_socket.{cc,h}.
[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
26 #include "i18n.h"
27
28 Socket::Socket (int timeout)
29         : _deadline (_io_service)
30         , _socket (_io_service)
31         , _acceptor (0)
32         , _timeout (timeout)
33 {
34         _deadline.expires_at (boost::posix_time::pos_infin);
35         check ();
36 }
37
38 Socket::~Socket ()
39 {
40         delete _acceptor;
41 }
42
43 void
44 Socket::check ()
45 {
46         if (_deadline.expires_at() <= boost::asio::deadline_timer::traits_type::now ()) {
47                 if (_acceptor) {
48                         _acceptor->cancel ();
49                 } else {
50                         _socket.close ();
51                 }
52                 _deadline.expires_at (boost::posix_time::pos_infin);
53         }
54
55         _deadline.async_wait (boost::bind (&Socket::check, this));
56 }
57
58 /** Blocking connect.
59  *  @param endpoint End-point to connect to.
60  */
61 void
62 Socket::connect (boost::asio::ip::tcp::endpoint endpoint)
63 {
64         _deadline.expires_from_now (boost::posix_time::seconds (_timeout));
65         boost::system::error_code ec = boost::asio::error::would_block;
66         _socket.async_connect (endpoint, boost::lambda::var(ec) = boost::lambda::_1);
67         do {
68                 _io_service.run_one();
69         } while (ec == boost::asio::error::would_block);
70
71         if (ec) {
72                 throw NetworkError (String::compose (_("error during async_connect (%1)"), ec.value ()));
73         }
74
75         if (!_socket.is_open ()) {
76                 throw NetworkError (_("connect timed out"));
77         }
78 }
79
80 void
81 Socket::accept (int port)
82 {
83         _acceptor = new boost::asio::ip::tcp::acceptor (_io_service, boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), port));
84         
85         _deadline.expires_from_now (boost::posix_time::seconds (_timeout));
86         boost::system::error_code ec = boost::asio::error::would_block;
87         _acceptor->async_accept (_socket, boost::lambda::var(ec) = boost::lambda::_1);
88         do {
89                 _io_service.run_one ();
90         } while (ec == boost::asio::error::would_block);
91
92         delete _acceptor;
93         _acceptor = 0;
94         
95         if (ec) {
96                 throw NetworkError (String::compose (_("error during async_accept (%1)"), ec.value ()));
97         }
98 }
99
100 /** Blocking write.
101  *  @param data Buffer to write.
102  *  @param size Number of bytes to write.
103  */
104 void
105 Socket::write (uint8_t const * 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_write (_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_write (%1)"), ec.value ()));
118         }
119 }
120
121 void
122 Socket::write (uint32_t v)
123 {
124         v = htonl (v);
125         write (reinterpret_cast<uint8_t*> (&v), 4);
126 }
127
128 /** Blocking read.
129  *  @param data Buffer to read to.
130  *  @param size Number of bytes to read.
131  */
132 void
133 Socket::read (uint8_t* data, int size)
134 {
135         _deadline.expires_from_now (boost::posix_time::seconds (_timeout));
136         boost::system::error_code ec = boost::asio::error::would_block;
137
138         boost::asio::async_read (_socket, boost::asio::buffer (data, size), boost::lambda::var(ec) = boost::lambda::_1);
139
140         do {
141                 _io_service.run_one ();
142         } while (ec == boost::asio::error::would_block);
143         
144         if (ec) {
145                 throw NetworkError (String::compose (_("error during async_read (%1)"), ec.value ()));
146         }
147 }
148
149 uint32_t
150 Socket::read_uint32 ()
151 {
152         uint32_t v;
153         read (reinterpret_cast<uint8_t *> (&v), 4);
154         return ntohl (v);
155 }
156