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 Socket::Socket (int timeout)
31 : _deadline (_io_service)
32 , _socket (_io_service)
35 _deadline.expires_at (boost::posix_time::pos_infin);
42 if (_deadline.expires_at() <= boost::asio::deadline_timer::traits_type::now ()) {
44 _deadline.expires_at (boost::posix_time::pos_infin);
47 _deadline.async_wait (boost::bind (&Socket::check, this));
51 * @param endpoint End-point to connect to.
54 Socket::connect (boost::asio::ip::tcp::endpoint endpoint)
56 _deadline.expires_from_now (boost::posix_time::seconds (_timeout));
57 boost::system::error_code ec = boost::asio::error::would_block;
58 _socket.async_connect (endpoint, boost::lambda::var(ec) = boost::lambda::_1);
60 _io_service.run_one();
61 } while (ec == boost::asio::error::would_block);
64 throw NetworkError (String::compose (_("error during async_connect (%1)"), ec.value ()));
67 if (!_socket.is_open ()) {
68 throw NetworkError (_("connect timed out"));
73 * @param data Buffer to write.
74 * @param size Number of bytes to write.
77 Socket::write (uint8_t const * data, int size)
79 _deadline.expires_from_now (boost::posix_time::seconds (_timeout));
80 boost::system::error_code ec = boost::asio::error::would_block;
82 boost::asio::async_write (_socket, boost::asio::buffer (data, size), boost::lambda::var(ec) = boost::lambda::_1);
85 _io_service.run_one ();
86 } while (ec == boost::asio::error::would_block);
89 throw NetworkError (String::compose (_("error during async_write (%1)"), ec.value ()));
94 Socket::write (uint32_t v)
97 write (reinterpret_cast<uint8_t*> (&v), 4);
101 * @param data Buffer to read to.
102 * @param size Number of bytes to read.
105 Socket::read (uint8_t* data, int size)
107 _deadline.expires_from_now (boost::posix_time::seconds (_timeout));
108 boost::system::error_code ec = boost::asio::error::would_block;
110 boost::asio::async_read (_socket, boost::asio::buffer (data, size), boost::lambda::var(ec) = boost::lambda::_1);
113 _io_service.run_one ();
114 } while (ec == boost::asio::error::would_block);
117 throw NetworkError (String::compose (_("error during async_read (%1)"), ec.value ()));
122 Socket::read_uint32 ()
125 read (reinterpret_cast<uint8_t *> (&v), 4);