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)
34 _deadline.expires_at (boost::posix_time::pos_infin);
46 if (_deadline.expires_at() <= boost::asio::deadline_timer::traits_type::now ()) {
52 _deadline.expires_at (boost::posix_time::pos_infin);
55 _deadline.async_wait (boost::bind (&Socket::check, this));
59 * @param endpoint End-point to connect to.
62 Socket::connect (boost::asio::ip::tcp::endpoint endpoint)
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);
68 _io_service.run_one();
69 } while (ec == boost::asio::error::would_block);
72 throw NetworkError (String::compose (_("error during async_connect (%1)"), ec.value ()));
75 if (!_socket.is_open ()) {
76 throw NetworkError (_("connect timed out"));
81 Socket::accept (int port)
83 _acceptor = new boost::asio::ip::tcp::acceptor (_io_service, boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), port));
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);
89 _io_service.run_one ();
90 } while (ec == boost::asio::error::would_block);
96 throw NetworkError (String::compose (_("error during async_accept (%1)"), ec.value ()));
101 * @param data Buffer to write.
102 * @param size Number of bytes to write.
105 Socket::write (uint8_t const * 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_write (_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_write (%1)"), ec.value ()));
122 Socket::write (uint32_t v)
125 write (reinterpret_cast<uint8_t*> (&v), 4);
129 * @param data Buffer to read to.
130 * @param size Number of bytes to read.
133 Socket::read (uint8_t* data, int size)
135 _deadline.expires_from_now (boost::posix_time::seconds (_timeout));
136 boost::system::error_code ec = boost::asio::error::would_block;
138 boost::asio::async_read (_socket, boost::asio::buffer (data, size), boost::lambda::var(ec) = boost::lambda::_1);
141 _io_service.run_one ();
142 } while (ec == boost::asio::error::would_block);
145 throw NetworkError (String::compose (_("error during async_read (%1)"), ec.value ()));
150 Socket::read_uint32 ()
153 read (reinterpret_cast<uint8_t *> (&v), 4);