diff options
| author | Carl Hetherington <cth@carlh.net> | 2015-01-29 20:02:56 +0000 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2015-01-29 20:02:56 +0000 |
| commit | bfe277e664a03ec47cd6bee7e1b1e4aca6eb38e6 (patch) | |
| tree | 6bdc6fa07f2cde0565c4025ab552f2c0fc9c3b2f /src/lib/util.cc | |
| parent | 6a0da4eb43fc684c0f54a7a3a817a31dc8db7109 (diff) | |
| parent | 27867be66579551018c926ed3339624e463c1b08 (diff) | |
Merge branch '2.0' of git.carlh.net:git/dcpomatic into 2.0
Diffstat (limited to 'src/lib/util.cc')
| -rw-r--r-- | src/lib/util.cc | 132 |
1 files changed, 1 insertions, 131 deletions
diff --git a/src/lib/util.cc b/src/lib/util.cc index 0a6f381db..c298a1946 100644 --- a/src/lib/util.cc +++ b/src/lib/util.cc @@ -287,9 +287,8 @@ set_backtrace_file (boost::filesystem::path p) void terminate () { - static bool tried_throw = false; - try { + static bool tried_throw = false; // try once to re-throw currently active exception if (!tried_throw) { tried_throw = true; @@ -481,135 +480,6 @@ dcp_audio_frame_rate (int fs) return 96000; } -Socket::Socket (int timeout) - : _deadline (_io_service) - , _socket (_io_service) - , _acceptor (0) - , _timeout (timeout) -{ - _deadline.expires_at (boost::posix_time::pos_infin); - check (); -} - -Socket::~Socket () -{ - delete _acceptor; -} - -void -Socket::check () -{ - if (_deadline.expires_at() <= boost::asio::deadline_timer::traits_type::now ()) { - if (_acceptor) { - _acceptor->cancel (); - } else { - _socket.close (); - } - _deadline.expires_at (boost::posix_time::pos_infin); - } - - _deadline.async_wait (boost::bind (&Socket::check, this)); -} - -/** Blocking connect. - * @param endpoint End-point to connect to. - */ -void -Socket::connect (boost::asio::ip::tcp::endpoint endpoint) -{ - _deadline.expires_from_now (boost::posix_time::seconds (_timeout)); - boost::system::error_code ec = boost::asio::error::would_block; - _socket.async_connect (endpoint, boost::lambda::var(ec) = boost::lambda::_1); - do { - _io_service.run_one(); - } while (ec == boost::asio::error::would_block); - - if (ec) { - throw NetworkError (String::compose (_("error during async_connect (%1)"), ec.value ())); - } - - if (!_socket.is_open ()) { - throw NetworkError (_("connect timed out")); - } -} - -void -Socket::accept (int port) -{ - _acceptor = new boost::asio::ip::tcp::acceptor (_io_service, boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), port)); - - _deadline.expires_from_now (boost::posix_time::seconds (_timeout)); - boost::system::error_code ec = boost::asio::error::would_block; - _acceptor->async_accept (_socket, boost::lambda::var(ec) = boost::lambda::_1); - do { - _io_service.run_one (); - } while (ec == boost::asio::error::would_block); - - delete _acceptor; - _acceptor = 0; - - if (ec) { - throw NetworkError (String::compose (_("error during async_accept (%1)"), ec.value ())); - } -} - -/** Blocking write. - * @param data Buffer to write. - * @param size Number of bytes to write. - */ -void -Socket::write (uint8_t const * data, int size) -{ - _deadline.expires_from_now (boost::posix_time::seconds (_timeout)); - boost::system::error_code ec = boost::asio::error::would_block; - - boost::asio::async_write (_socket, boost::asio::buffer (data, size), boost::lambda::var(ec) = boost::lambda::_1); - - do { - _io_service.run_one (); - } while (ec == boost::asio::error::would_block); - - if (ec) { - throw NetworkError (String::compose (_("error during async_write (%1)"), ec.value ())); - } -} - -void -Socket::write (uint32_t v) -{ - v = htonl (v); - write (reinterpret_cast<uint8_t*> (&v), 4); -} - -/** Blocking read. - * @param data Buffer to read to. - * @param size Number of bytes to read. - */ -void -Socket::read (uint8_t* data, int size) -{ - _deadline.expires_from_now (boost::posix_time::seconds (_timeout)); - boost::system::error_code ec = boost::asio::error::would_block; - - boost::asio::async_read (_socket, boost::asio::buffer (data, size), boost::lambda::var(ec) = boost::lambda::_1); - - do { - _io_service.run_one (); - } while (ec == boost::asio::error::would_block); - - if (ec) { - throw NetworkError (String::compose (_("error during async_read (%1)"), ec.value ())); - } -} - -uint32_t -Socket::read_uint32 () -{ - uint32_t v; - read (reinterpret_cast<uint8_t *> (&v), 4); - return ntohl (v); -} - /** Round a number up to the nearest multiple of another number. * @param c Index. * @param s Array of numbers to round, indexed by c. |
