25#ifndef __JOIN_SOCKETSTREAM_HPP__
26#define __JOIN_SOCKETSTREAM_HPP__
42 template <
class Protocol>
47 using Socket =
typename Protocol::Socket;
76 :
std::streambuf (
std::move (other)),
92 std::streambuf::operator= (std::move (other));
93 this->
_buf = std::move (other._buf);
95 this->
_socket = std::move (other._socket);
105 if (this->
_socket.connected ())
107 this->
overflow (traits_type::eof ());
118 if (this->
_socket.bind (endpoint) == -1)
133 if (this->
_socket.connect (endpoint) == -1)
140 if (!this->
_socket.waitConnected (this->_timeout))
156 if (this->
_socket.connected () && (this->overflow (traits_type::eof ()) == traits_type::eof ()))
161 if (this->
_socket.disconnect () == -1)
168 if (!this->
_socket.waitDisconnected (this->_timeout))
220 if (!this->
_socket.connected ())
223 return traits_type::eof ();
226 if (this->eback () ==
nullptr)
228 this->setg (this->
_buf.get (), this->_buf.get (), this->_buf.get ());
231 if (this->gptr () == this->egptr ())
240 if (this->
_socket.waitReadyRead (this->_timeout))
246 return traits_type::eof ();
249 this->setg (this->eback (), this->eback (), this->eback () + nread);
254 return traits_type::to_int_type (*this->gptr ());
262 virtual int_type
overflow (int_type c = traits_type::eof ())
override
264 if (!this->
_socket.connected ())
267 return traits_type::eof ();
270 if (this->pbase () ==
nullptr)
275 if ((this->pptr () == this->epptr ()) || (c == traits_type::eof ()))
277 std::streamsize pending = this->pptr () - this->pbase ();
280 if (this->
_socket.writeExactly (this->pbase (), pending, this->_timeout) == -1)
283 return traits_type::eof ();
287 this->setp (this->pbase (), this->pbase () +
_bufsize);
289 if (c == traits_type::eof ())
291 return traits_type::not_eof (c);
295 return this->sputc (traits_type::to_char_type (c));
302 virtual int_type
sync ()
override
304 if (!this->
_socket.connected () || (this->overflow () == traits_type::eof ()))
315 std::unique_ptr <char []>
_buf;
327 template <
class Protocol>
333 using Socket =
typename Protocol::Socket;
361 :
std::iostream (
std::move (other)),
374 std::iostream::operator= (std::move (other));
375 this->
_sockbuf = std::move (other._sockbuf);
393 this->setstate (std::ios_base::failbit);
406 this->setstate (std::ios_base::failbit);
418 this->setstate (std::ios_base::failbit);
511 template <
class Protocol>
517 using Socket =
typename Protocol::Socket;
556 BasicSocketStream <Protocol>::operator= (std::move (other));
580 this->setstate (std::ios_base::failbit);
593 this->setstate (std::ios_base::failbit);
socket stream class.
Definition socketstream.hpp:329
typename Protocol::Endpoint Endpoint
Definition socketstream.hpp:332
BasicSocketStream(const BasicSocketStream &other)=delete
copy constructor.
const Endpoint & remoteEndpoint()
determine the remote endpoint associated with this socket.
Definition socketstream.hpp:444
BasicSocketStream()
default constructor.
Definition socketstream.hpp:338
virtual ~BasicSocketStream()=default
destroy the socket stream instance.
BasicSocketStream(BasicSocketStream &&other)
move constructor.
Definition socketstream.hpp:360
Socket & socket()
get the nested socket.
Definition socketstream.hpp:498
typename Protocol::Socket Socket
Definition socketstream.hpp:333
SocketStreambuf _sockbuf
associated stream buffer.
Definition socketstream.hpp:505
BasicSocketStream & operator=(BasicSocketStream &&other)
move assignment operator.
Definition socketstream.hpp:372
bool opened()
check if the socket is opened.
Definition socketstream.hpp:453
Endpoint localEndpoint()
determine the local endpoint associated with this socket.
Definition socketstream.hpp:435
bool connected()
check if the socket is connected.
Definition socketstream.hpp:462
virtual void connect(const Endpoint &endpoint)
make a connection to the given endpoint.
Definition socketstream.hpp:402
virtual void bind(const Endpoint &endpoint)
assigns the specified endpoint to the socket.
Definition socketstream.hpp:389
BasicSocketStream & operator=(const BasicSocketStream &other)=delete
copy assignment operator.
virtual void close()
close the connection.
Definition socketstream.hpp:426
void timeout(int ms)
set the socket timeout.
Definition socketstream.hpp:480
virtual void disconnect()
shutdown the connection.
Definition socketstream.hpp:414
int timeout() const
get the current timeout in milliseconds.
Definition socketstream.hpp:489
bool encrypted()
check if the socket is secure.
Definition socketstream.hpp:471
socket stream buffer class.
Definition socketstream.hpp:44
BasicSocketStreambuf()
default constructor.
Definition socketstream.hpp:52
std::unique_ptr< char[]> _buf
internal buffer.
Definition socketstream.hpp:315
typename Protocol::Endpoint Endpoint
Definition socketstream.hpp:46
BasicSocketStreambuf * connect(const Endpoint &endpoint)
make a connection to the given endpoint.
Definition socketstream.hpp:131
int _timeout
timeout.
Definition socketstream.hpp:318
virtual ~BasicSocketStreambuf()
destroy the socket stream buffer instance.
Definition socketstream.hpp:103
Socket & socket()
get the nested socket.
Definition socketstream.hpp:208
static const std::streamsize _bufsize
internal buffer size.
Definition socketstream.hpp:312
Socket _socket
internal socket.
Definition socketstream.hpp:321
virtual int_type underflow() override
reads characters from the associated input sequence to the get area.
Definition socketstream.hpp:218
BasicSocketStreambuf * disconnect()
shutdown the connection.
Definition socketstream.hpp:154
BasicSocketStreambuf * bind(const Endpoint &endpoint)
assigns the specified endpoint to the socket.
Definition socketstream.hpp:116
void close()
close the connection.
Definition socketstream.hpp:181
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 socketstream.hpp:262
int timeout() const
get the current timeout in milliseconds.
Definition socketstream.hpp:199
void timeout(int ms)
set the socket timeout.
Definition socketstream.hpp:190
typename Protocol::Socket Socket
Definition socketstream.hpp:47
BasicSocketStreambuf(BasicSocketStreambuf &&other)
move constructor.
Definition socketstream.hpp:75
virtual int_type sync() override
synchronizes the buffers with the associated character sequence.
Definition socketstream.hpp:302
TLS stream class.
Definition socketstream.hpp:513
BasicTlsStream & operator=(BasicTlsStream &&other)
move assignment operator.
Definition socketstream.hpp:554
int setCaPath(const std::string &caPath)
set the location of the trusted CA certificates.
Definition socketstream.hpp:621
void startEncryption()
start socket encryption (perform TLS handshake).
Definition socketstream.hpp:570
BasicTlsStream()
default constructor.
Definition socketstream.hpp:522
int setCipher_1_3(const std::string &cipher)
set the cipher list (TLSv1.3).
Definition socketstream.hpp:661
int setCipher(const std::string &cipher)
set the cipher list (TLSv1.2 and below).
Definition socketstream.hpp:651
BasicTlsStream(BasicTlsStream &&other)
move constructor.
Definition socketstream.hpp:544
typename Protocol::Endpoint Endpoint
Definition socketstream.hpp:516
void connectEncrypted(const Endpoint &endpoint)
make an encrypted connection to the given endpoint.
Definition socketstream.hpp:589
BasicTlsStream(const BasicTlsStream &other)=delete
copy constructor.
BasicTlsStream & operator=(const BasicTlsStream &other)=delete
copy assignment operator.
int setCertificate(const std::string &cert, const std::string &key="")
set the certificate and the private key.
Definition socketstream.hpp:611
void setVerify(bool verify, int depth=-1)
Enable/Disable the verification of the peer certificate.
Definition socketstream.hpp:641
int setCaFile(const std::string &caFile)
set the location of the trusted CA certificate file.
Definition socketstream.hpp:631
typename Protocol::Socket Socket
Definition socketstream.hpp:517
virtual ~BasicTlsStream()=default
destroy the TLS stream instance.
const std::string key(65, 'a')
key.
Definition acceptor.hpp:32
std::error_code make_error_code(join::Errc code)
Create an std::error_code object.
Definition error.cpp:154