Github   Telegram
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | List of all members
engine::io::TlsWrapper Class Referencefinal

#include <userver/engine/io/tls_wrapper.hpp>

+ Inheritance diagram for engine::io::TlsWrapper:
+ Collaboration diagram for engine::io::TlsWrapper:

Public Member Functions

 TlsWrapper (const TlsWrapper &)=delete
 
 TlsWrapper (TlsWrapper &&) noexcept
 
 operator bool () const
 Whether the socket is valid. More...
 
bool IsValid () const override
 Whether the socket is valid. More...
 
bool WaitReadable (Deadline) override
 Suspends current task until the socket has data available. More...
 
bool WaitWriteable (Deadline) override
 Suspends current task until the socket can accept more data. More...
 
size_t RecvSome (void *buf, size_t len, Deadline deadline)
 Receives at least one byte from the socket. More...
 
size_t RecvAll (void *buf, size_t len, Deadline deadline)
 Receives exactly len bytes from the socket. More...
 
size_t SendAll (const void *buf, size_t len, Deadline deadline)
 Sends exactly len bytes to the socket. More...
 
Socket StopTls (Deadline deadline)
 Finishes TLS session and returns the socket. More...
 
size_t ReadSome (void *buf, size_t len, Deadline deadline) override
 Receives at least one byte from the socket. More...
 
size_t ReadAll (void *buf, size_t len, Deadline deadline) override
 Receives exactly len bytes from the socket. More...
 
size_t WriteAll (const void *buf, size_t len, Deadline deadline) override
 Writes exactly len bytes to the socket. More...
 
virtual bool IsValid () const =0
 Whether the stream is valid. More...
 
virtual bool WaitReadable (Deadline)=0
 Suspends current task until the stream has data available. More...
 
virtual size_t ReadSome (void *buf, size_t len, Deadline deadline)=0
 Receives at least one byte from the stream. More...
 
virtual size_t ReadAll (void *buf, size_t len, Deadline deadline)=0
 
virtual bool WaitWriteable (Deadline deadline)=0
 Suspends current task until the data is available. More...
 
virtual size_t WriteAll (const void *buf, size_t len, Deadline deadline)=0
 Sends exactly len bytes of buf. More...
 

Static Public Member Functions

static TlsWrapper StartTlsClient (Socket &&socket, const std::string &server_name, Deadline deadline)
 Starts a TLS client on an opened socket.
 
static TlsWrapper StartTlsServer (Socket &&socket, const crypto::Certificate &cert, const crypto::PrivateKey &key, Deadline deadline, const std::vector< crypto::Certificate > &cert_authorities={})
 Starts a TLS server on an opened socket.
 

Detailed Description

Class for TLS communications over a Socket.

Not thread safe.

Usage example:

TcpListener tcp_listener;
auto [server, client] = tcp_listener.MakeSocketPair(deadline);
auto server_task = utils::Async(
"tls-server",
[deadline](auto&& server) {
auto tls_server = io::TlsWrapper::StartTlsServer(
std::forward<decltype(server)>(server),
if (tls_server.SendAll(kData.data(), kData.size(), deadline) !=
kData.size()) {
throw std::runtime_error("Couldn't send data");
}
},
std::move(server));
auto tls_client =
io::TlsWrapper::StartTlsClient(std::move(client), {}, deadline);
std::vector<char> buffer(kData.size());
const auto bytes_rcvd =
tls_client.RecvAll(buffer.data(), buffer.size(), deadline);

Definition at line 27 of file tls_wrapper.hpp.

Member Function Documentation

◆ IsValid()

bool engine::io::TlsWrapper::IsValid ( ) const
overridevirtual

Whether the socket is valid.

Implements engine::io::ReadableBase.

◆ operator bool()

engine::io::TlsWrapper::operator bool ( ) const
inlineexplicit

Whether the socket is valid.

Definition at line 46 of file tls_wrapper.hpp.

◆ ReadAll()

size_t engine::io::TlsWrapper::ReadAll ( void *  buf,
size_t  len,
Deadline  deadline 
)
inlineoverridevirtual

Receives exactly len bytes from the socket.

Note
Can return less than len if socket is closed by peer.

Implements engine::io::ReadableBase.

Definition at line 85 of file tls_wrapper.hpp.

◆ ReadSome()

size_t engine::io::TlsWrapper::ReadSome ( void *  buf,
size_t  len,
Deadline  deadline 
)
inlineoverridevirtual

Receives at least one byte from the socket.

Returns
0 if connection is closed on one side and no data could be received any more, received bytes count otherwise.

Implements engine::io::ReadableBase.

Definition at line 78 of file tls_wrapper.hpp.

◆ RecvAll()

size_t engine::io::TlsWrapper::RecvAll ( void *  buf,
size_t  len,
Deadline  deadline 
)

Receives exactly len bytes from the socket.

Note
Can return less than len if socket is closed by peer.

◆ RecvSome()

size_t engine::io::TlsWrapper::RecvSome ( void *  buf,
size_t  len,
Deadline  deadline 
)

Receives at least one byte from the socket.

Returns
0 if connection is closed on one side and no data could be received any more, received bytes count otherwise.

◆ SendAll()

size_t engine::io::TlsWrapper::SendAll ( const void *  buf,
size_t  len,
Deadline  deadline 
)

Sends exactly len bytes to the socket.

Note
Can return less than len if socket is closed by peer.

◆ StopTls()

Socket engine::io::TlsWrapper::StopTls ( Deadline  deadline)

Finishes TLS session and returns the socket.

Warning
Wrapper becomes invalid on entry and can only be used to retry socket extraction if interrupted.

◆ WaitReadable()

bool engine::io::TlsWrapper::WaitReadable ( Deadline  )
overridevirtual

Suspends current task until the socket has data available.

Implements engine::io::ReadableBase.

◆ WaitWriteable()

bool engine::io::TlsWrapper::WaitWriteable ( Deadline  )
overridevirtual

Suspends current task until the socket can accept more data.

Implements engine::io::WritableBase.

◆ WriteAll()

size_t engine::io::TlsWrapper::WriteAll ( const void *  buf,
size_t  len,
Deadline  deadline 
)
inlineoverridevirtual

Writes exactly len bytes to the socket.

Note
Can return less than len if socket is closed by peer.

Implements engine::io::WritableBase.

Definition at line 92 of file tls_wrapper.hpp.


The documentation for this class was generated from the following file: