2 Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 #include "dcpomatic_socket.h"
21 #include "compose.hpp"
22 #include "exceptions.h"
23 #include <boost/bind.hpp>
24 #include <boost/lambda/lambda.hpp>
28 Socket::Socket (int timeout)
29 : _deadline (_io_service)
30 , _socket (_io_service)
33 _deadline.expires_at (boost::posix_time::pos_infin);
40 if (_deadline.expires_at() <= boost::asio::deadline_timer::traits_type::now ()) {
42 _deadline.expires_at (boost::posix_time::pos_infin);
45 _deadline.async_wait (boost::bind (&Socket::check, this));
49 * @param endpoint End-point to connect to.
52 Socket::connect (boost::asio::ip::tcp::endpoint endpoint)
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);
58 _io_service.run_one();
59 } while (ec == boost::asio::error::would_block);
62 throw NetworkError (String::compose (_("error during async_connect (%1)"), ec.value ()));
65 if (!_socket.is_open ()) {
66 throw NetworkError (_("connect timed out"));
71 * @param data Buffer to write.
72 * @param size Number of bytes to write.
75 Socket::write (uint8_t const * data, int size)
77 _deadline.expires_from_now (boost::posix_time::seconds (_timeout));
78 boost::system::error_code ec = boost::asio::error::would_block;
80 boost::asio::async_write (_socket, boost::asio::buffer (data, size), boost::lambda::var(ec) = boost::lambda::_1);
83 _io_service.run_one ();
84 } while (ec == boost::asio::error::would_block);
87 throw NetworkError (String::compose (_("error during async_write (%1)"), ec.value ()));
92 Socket::write (uint32_t v)
95 write (reinterpret_cast<uint8_t*> (&v), 4);
99 * @param data Buffer to read to.
100 * @param size Number of bytes to read.
103 Socket::read (uint8_t* data, int size)
105 _deadline.expires_from_now (boost::posix_time::seconds (_timeout));
106 boost::system::error_code ec = boost::asio::error::would_block;
108 boost::asio::async_read (_socket, boost::asio::buffer (data, size), boost::lambda::var(ec) = boost::lambda::_1);
111 _io_service.run_one ();
112 } while (ec == boost::asio::error::would_block);
115 throw NetworkError (String::compose (_("error during async_read (%1)"), ec.value ()));
120 Socket::read_uint32 ()
123 read (reinterpret_cast<uint8_t *> (&v), 4);