Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 91 additions & 9 deletions core/include/join/socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#include <join/error.hpp>

// C++.
#include <algorithm>
#include <chrono>
#include <memory>
#include <string>

Expand All @@ -53,6 +55,7 @@ namespace join
public:
using Ptr = std::unique_ptr<BasicSocket<Protocol>>;
using Endpoint = typename Protocol::Endpoint;
using TimePoint = std::chrono::steady_clock::time_point;

/**
* @brief socket modes.
Expand Down Expand Up @@ -318,12 +321,31 @@ namespace join

/**
* @brief block until new data is available for reading.
* @param timeout timeout in milliseconds.
* @return true if there is new data available for reading, false otherwise.
*/
bool waitReadyRead (int timeout = 0) const noexcept
bool waitReadyRead () const noexcept
{
return (wait (true, false, timeout) == 0);
return (waitUntil (true, false, TimePoint::max ()) == 0);
}

/**
* @brief block until new data is available for reading, giving up after the given duration.
* @param timeout maximum time to wait.
* @return true if there is new data available for reading, false otherwise.
*/
bool waitReadyRead (std::chrono::nanoseconds timeout) const noexcept
{
return (waitUntil (true, false, std::chrono::steady_clock::now () + timeout) == 0);
}

/**
* @brief block until new data is available for reading, giving up at the given time point.
* @param deadline time point at which to give up, max to wait indefinitely.
* @return true if there is new data available for reading, false otherwise.
*/
bool waitReadyRead (TimePoint deadline) const noexcept
{
return (waitUntil (true, false, deadline) == 0);
}

/**
Expand Down Expand Up @@ -364,12 +386,31 @@ namespace join

/**
* @brief block until at least one byte can be written.
* @param timeout timeout in milliseconds.
* @return true if data can be written, false otherwise.
*/
bool waitReadyWrite (int timeout = 0) const noexcept
bool waitReadyWrite () const noexcept
{
return (wait (false, true, timeout) == 0);
return (waitUntil (false, true, TimePoint::max ()) == 0);
}

/**
* @brief block until at least one byte can be written, giving up after the given duration.
* @param timeout maximum time to wait.
* @return true if data can be written, false otherwise.
*/
bool waitReadyWrite (std::chrono::nanoseconds timeout) const noexcept
{
return (waitUntil (false, true, std::chrono::steady_clock::now () + timeout) == 0);
}

/**
* @brief block until at least one byte can be written, giving up at the given time point.
* @param deadline time point at which to give up, max to wait indefinitely.
* @return true if data can be written, false otherwise.
*/
bool waitReadyWrite (TimePoint deadline) const noexcept
{
return (waitUntil (false, true, deadline) == 0);
}

/**
Expand Down Expand Up @@ -612,6 +653,15 @@ namespace join
return _protocol.protocol ();
}

/**
* @brief get the blocking mode of the socket.
* @return the blocking mode of the socket.
*/
Mode mode () const noexcept
{
return _mode;
}

/**
* @brief get socket native handle.
* @return socket native handle.
Expand All @@ -625,10 +675,33 @@ namespace join
* @brief wait for the socket handle to become ready.
* @param wantRead set to true if want read
* @param wantWrite set to true if want write.
* @param timeout timeout in milliseconds.
* @return 0 on success, -1 on failure.
*/
int wait (bool wantRead, bool wantWrite, int timeout) const noexcept
int wait (bool wantRead, bool wantWrite) const noexcept
{
return waitUntil (wantRead, wantWrite, TimePoint::max ());
}

/**
* @brief wait for the socket handle to become ready, giving up after the given duration.
* @param wantRead set to true if want read
* @param wantWrite set to true if want write.
* @param timeout maximum time to wait.
* @return 0 on success, -1 on failure.
*/
int waitFor (bool wantRead, bool wantWrite, std::chrono::nanoseconds timeout) const noexcept
{
return waitUntil (wantRead, wantWrite, std::chrono::steady_clock::now () + timeout);
}

/**
* @brief wait for the socket handle to become ready, giving up at the given time point.
* @param wantRead set to true if want read
* @param wantWrite set to true if want write.
* @param deadline time point at which to give up, max to wait indefinitely.
* @return 0 on success, -1 on failure.
*/
int waitUntil (bool wantRead, bool wantWrite, TimePoint deadline) const noexcept
{
struct pollfd handle;
handle.fd = _handle;
Expand All @@ -645,7 +718,16 @@ namespace join
handle.events |= POLLOUT;
}

int nset = (handle.fd > -1) ? ::poll (&handle, 1, timeout == 0 ? -1 : timeout) : -1;
struct timespec ts = {};
const struct timespec* remaining = nullptr;

if (deadline != TimePoint::max ())
{
ts = toTimespec (std::max (deadline - std::chrono::steady_clock::now (), TimePoint::duration::zero ()));
remaining = &ts;
}

int nset = (handle.fd > -1) ? ::ppoll (&handle, 1, remaining, nullptr) : -1;
if (nset != 1)
{
if (nset == -1)
Expand Down
48 changes: 30 additions & 18 deletions core/include/join/socket_stream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
// C++.
#include <streambuf>
#include <utility>
#include <chrono>
#include <memory>

namespace join
Expand All @@ -44,6 +45,7 @@ namespace join
public:
using Endpoint = typename Protocol::Endpoint;
using Socket = typename Protocol::Socket;
using TimePoint = typename Socket::TimePoint;

/**
* @brief default constructor.
Expand Down Expand Up @@ -146,7 +148,7 @@ namespace join
return nullptr;
}

if (!_socket.waitConnected (_timeout))
if (!_socket.waitConnected (deadline ()))
{
_socket.close ();
return nullptr;
Expand Down Expand Up @@ -174,7 +176,7 @@ namespace join
return nullptr;
}

if (!_socket.waitDisconnected (_timeout))
if (!_socket.waitDisconnected (deadline ()))
{
return nullptr;
}
Expand All @@ -194,22 +196,32 @@ namespace join

/**
* @brief set the socket timeout.
* @param ms timeout in milliseconds.
* @param timeout maximum time granted to each stream operation, zero to remove the limit.
*/
void timeout (int ms)
void timeout (std::chrono::nanoseconds timeout)
{
_timeout = ms;
_timeout = timeout;
}

/**
* @brief get the current timeout in milliseconds.
* @return the current timeout.
* @brief get the current timeout duration.
* @return the current timeout duration.
*/
int timeout () const
std::chrono::nanoseconds timeout () const
{
return _timeout;
}

/**
* @brief get the deadline of an operation started now.
* @return the deadline, max when the stream operations are not time bounded.
*/
TimePoint deadline () const noexcept
{
return (_timeout == std::chrono::nanoseconds::zero ()) ? TimePoint::max ()
: std::chrono::steady_clock::now () + _timeout;
}

/**
* @brief get the nested socket.
* @return the nested socket.
Expand Down Expand Up @@ -246,7 +258,7 @@ namespace join
{
if (lastError == Errc::TemporaryError)
{
if (_socket.waitReadyRead (_timeout))
if (_socket.waitReadyRead (deadline ()))
{
continue;
}
Expand Down Expand Up @@ -286,7 +298,7 @@ namespace join
std::streamsize pending = pptr () - pbase ();
if (pending)
{
if (_socket.writeExactly (pbase (), pending, _timeout) == -1)
if (_socket.writeExactly (pbase (), pending, deadline ()) == -1)
{
_socket.close ();
return traits_type::eof ();
Expand Down Expand Up @@ -323,8 +335,8 @@ namespace join
/// internal buffer.
std::unique_ptr<char[]> _buf;

/// timeout.
int _timeout = 30000;
/// timeout, zero when the stream operations are not time bounded.
std::chrono::nanoseconds _timeout = std::chrono::seconds (30);

/// internal socket.
Socket _socket;
Expand Down Expand Up @@ -485,18 +497,18 @@ namespace join

/**
* @brief set the socket timeout.
* @param ms timeout in milliseconds.
* @param timeout maximum time granted to each stream operation, zero to remove the limit.
*/
void timeout (int ms)
void timeout (std::chrono::nanoseconds timeout)
{
_sockbuf.timeout (ms);
_sockbuf.timeout (timeout);
}

/**
* @brief get the current timeout in milliseconds.
* @return the current timeout.
* @brief get the current timeout duration.
* @return the current timeout duration.
*/
int timeout () const
std::chrono::nanoseconds timeout () const
{
return _sockbuf.timeout ();
}
Expand Down
Loading
Loading