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