2 Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
21 #include "dcpomatic_socket.h"
22 #include "compose.hpp"
23 #include "exceptions.h"
24 #include <boost/bind.hpp>
25 #include <boost/lambda/lambda.hpp>
30 /** @param timeout Timeout in seconds */
31 Socket::Socket (int timeout)
32 : _deadline (_io_service)
33 , _socket (_io_service)
36 _deadline.expires_at (boost::posix_time::pos_infin);
43 if (_deadline.expires_at() <= boost::asio::deadline_timer::traits_type::now ()) {
45 _deadline.expires_at (boost::posix_time::pos_infin);
48 _deadline.async_wait (boost::bind (&Socket::check, this));
52 * @param endpoint End-point to connect to.
55 Socket::connect (boost::asio::ip::tcp::endpoint endpoint)
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);
61 _io_service.run_one();
62 } while (ec == boost::asio::error::would_block);
65 throw NetworkError (String::compose (_("error during async_connect (%1)"), ec.value ()));
68 if (!_socket.is_open ()) {
69 throw NetworkError (_("connect timed out"));
74 * @param data Buffer to write.
75 * @param size Number of bytes to write.
78 Socket::write (uint8_t const * data, int size)
80 _deadline.expires_from_now (boost::posix_time::seconds (_timeout));
81 boost::system::error_code ec = boost::asio::error::would_block;
83 boost::asio::async_write (_socket, boost::asio::buffer (data, size), boost::lambda::var(ec) = boost::lambda::_1);
86 _io_service.run_one ();
87 } while (ec == boost::asio::error::would_block);
90 throw NetworkError (String::compose (_("error during async_write (%1)"), ec.value ()));
95 Socket::write (uint32_t v)
98 write (reinterpret_cast<uint8_t*> (&v), 4);
102 * @param data Buffer to read to.
103 * @param size Number of bytes to read.
106 Socket::read (uint8_t* data, int size)
108 _deadline.expires_from_now (boost::posix_time::seconds (_timeout));
109 boost::system::error_code ec = boost::asio::error::would_block;
111 boost::asio::async_read (_socket, boost::asio::buffer (data, size), boost::lambda::var(ec) = boost::lambda::_1);
114 _io_service.run_one ();
115 } while (ec == boost::asio::error::would_block);
118 throw NetworkError (String::compose (_("error during async_read (%1)"), ec.value ()));
123 Socket::read_uint32 ()
126 read (reinterpret_cast<uint8_t *> (&v), 4);