25#ifndef JOIN_CORE_SOCKET_STREAM_HPP
26#define JOIN_CORE_SOCKET_STREAM_HPP
42 template <
class Protocol>
47 using Socket =
typename Protocol::Socket;
87 :
std::streambuf (
std::move (other))
103 std::streambuf::operator= (std::move (other));
104 _buf = std::move (other._buf);
106 _socket = std::move (other._socket);
129 if (
_socket.bind (endpoint) == -1)
144 if (
_socket.connect (endpoint) == -1)
167 if (
_socket.connected () && (
overflow (traits_type::eof ()) == traits_type::eof ()))
172 if (
_socket.disconnect () == -1)
221 return (
_timeout == std::chrono::nanoseconds::zero ()) ? TimePoint::max ()
222 : std::chrono::steady_clock::now () +
_timeout;
244 return traits_type::eof ();
247 if (eback () ==
nullptr)
252 if (gptr () == egptr ())
267 return traits_type::eof ();
270 setg (eback (), eback (), eback () + nread);
275 return traits_type::to_int_type (*gptr ());
283 virtual int_type
overflow (int_type c = traits_type::eof ())
override
288 return traits_type::eof ();
291 if (pbase () ==
nullptr)
296 if ((pptr () == epptr ()) || (c == traits_type::eof ()))
298 std::streamsize pending = pptr () - pbase ();
304 return traits_type::eof ();
308 setp (pbase (), pbase () +
_bufsize);
310 if (c == traits_type::eof ())
312 return traits_type::not_eof (c);
316 return sputc (traits_type::to_char_type (c));
323 virtual int_type
sync ()
override
339 std::chrono::nanoseconds
_timeout = std::chrono::seconds (30);
348 template <
class Protocol>
354 using Socket =
typename Protocol::Socket;
392 :
std::iostream (
std::move (other))
405 std::iostream::operator= (std::move (other));
406 _sockbuf = std::move (other._sockbuf);
424 setstate (std::ios_base::failbit);
437 setstate (std::ios_base::failbit);
449 setstate (std::ios_base::failbit);
socket stream class.
Definition socket_stream.hpp:350
typename Protocol::Endpoint Endpoint
Definition socket_stream.hpp:353
Endpoint remoteEndpoint()
determine the remote endpoint associated with this socket.
Definition socket_stream.hpp:475
BasicSocketStream(const BasicSocketStream &other)=delete
copy constructor.
BasicSocketStream()
default constructor.
Definition socket_stream.hpp:359
virtual ~BasicSocketStream()=default
destroy the socket stream instance.
BasicSocketStream(BasicSocketStream &&other)
move constructor.
Definition socket_stream.hpp:391
Socket & socket()
get the nested socket.
Definition socket_stream.hpp:520
typename Protocol::Socket Socket
Definition socket_stream.hpp:354
SocketStreambuf _sockbuf
associated stream buffer.
Definition socket_stream.hpp:527
BasicSocketStream(Socket &&socket)
construct the socket stream by moving an existing socket in.
Definition socket_stream.hpp:368
bool opened()
check if the socket is opened.
Definition socket_stream.hpp:484
void timeout(std::chrono::nanoseconds timeout)
set the socket timeout.
Definition socket_stream.hpp:502
Endpoint localEndpoint()
determine the local endpoint associated with this socket.
Definition socket_stream.hpp:466
bool connected()
check if the socket is connected.
Definition socket_stream.hpp:493
virtual void connect(const Endpoint &endpoint)
make a connection to the given endpoint.
Definition socket_stream.hpp:433
virtual void bind(const Endpoint &endpoint)
assigns the specified endpoint to the socket.
Definition socket_stream.hpp:420
BasicSocketStream & operator=(const BasicSocketStream &other)=delete
copy assignment operator.
virtual void close()
close the connection.
Definition socket_stream.hpp:457
virtual void disconnect()
shutdown the connection.
Definition socket_stream.hpp:445
std::chrono::nanoseconds timeout() const
get the current timeout duration.
Definition socket_stream.hpp:511
socket stream buffer class.
Definition socket_stream.hpp:44
std::chrono::nanoseconds timeout() const
get the current timeout duration.
Definition socket_stream.hpp:210
BasicSocketStreambuf()
default constructor.
Definition socket_stream.hpp:53
typename Protocol::Endpoint Endpoint
Definition socket_stream.hpp:46
BasicSocketStreambuf * connect(const Endpoint &endpoint)
make a connection to the given endpoint.
Definition socket_stream.hpp:142
virtual ~BasicSocketStreambuf()
destroy the socket stream buffer instance.
Definition socket_stream.hpp:114
BasicSocketStreambuf(Socket &&socket)
construct the socket stream buffer by moving an existing socket in.
Definition socket_stream.hpp:63
Socket & socket()
get the nested socket.
Definition socket_stream.hpp:229
TimePoint deadline() const noexcept
get the deadline of an operation started now.
Definition socket_stream.hpp:219
static const std::streamsize _bufsize
internal buffer size.
Definition socket_stream.hpp:333
Socket _socket
internal socket.
Definition socket_stream.hpp:342
virtual int_type underflow() override
reads characters from the associated input sequence to the get area.
Definition socket_stream.hpp:239
BasicSocketStreambuf * disconnect()
shutdown the connection.
Definition socket_stream.hpp:165
BasicSocketStreambuf * bind(const Endpoint &endpoint)
assigns the specified endpoint to the socket.
Definition socket_stream.hpp:127
typename Socket::TimePoint TimePoint
Definition socket_stream.hpp:48
void close()
close the connection.
Definition socket_stream.hpp:192
BasicSocketStreambuf & operator=(const BasicSocketStreambuf &other)=delete
copy assignment operator.
BasicSocketStreambuf(const BasicSocketStreambuf &other)=delete
copy constructor.
virtual int_type overflow(int_type c=traits_type::eof()) override
writes characters to the associated output sequence from the put area.
Definition socket_stream.hpp:283
typename Protocol::Socket Socket
Definition socket_stream.hpp:47
void timeout(std::chrono::nanoseconds timeout)
set the socket timeout.
Definition socket_stream.hpp:201
std::chrono::nanoseconds _timeout
timeout, zero when the stream operations are not time bounded.
Definition socket_stream.hpp:339
std::unique_ptr< char[]> _buf
internal buffer.
Definition socket_stream.hpp:336
BasicSocketStreambuf(BasicSocketStreambuf &&other)
move constructor.
Definition socket_stream.hpp:86
virtual int_type sync() override
synchronizes the buffers with the associated character sequence.
Definition socket_stream.hpp:323
Definition acceptor.hpp:32
std::error_code make_error_code(join::Errc code) noexcept
Create an std::error_code object.
Definition error.cpp:195