COROIO: NNet::TSslSocket< TSocket > Class Template Reference
COROIO
 
Loading...
Searching...
No Matches
NNet::TSslSocket< TSocket > Class Template Reference

Implements an SSL/TLS layer on top of an underlying connection. More...

#include <ssl.hpp>

Public Types

using TPoller = typename TSocket::TPoller
 

Public Member Functions

 TSslSocket (TSocket &&socket, TSslContext &ctx)
 Constructs a TSslSocket from an underlying socket and an SSL context.
 
 TSslSocket (TSslSocket &&other)
 
TSslSocketoperator= (TSslSocket &&other)
 
 TSslSocket (const TSslSocket &)=delete
 
TSslSocketoperator= (const TSslSocket &)=delete
 
 ~TSslSocket ()
 Destructor.
 
void SslSetTlsExtHostName (const std::string &host)
 Sets the TLS SNI (Server Name Indication) extension host name.
 
TFuture< TSslSocket< TSocket > > Accept ()
 Asynchronously accepts an incoming SSL connection.
 
TFuture< void > AcceptHandshake ()
 Performs the server-side SSL handshake.
 
TFuture< void > Connect (const TAddress &address, TTime deadline=TTime::max())
 Initiates the client-side SSL handshake.
 
TFuture< ssize_t > ReadSome (void *data, size_t size)
 Asynchronously reads data from the SSL connection.
 
TFuture< ssize_t > WriteSome (const void *data, size_t size)
 Asynchronously writes data to the SSL connection.
 
auto Poller ()
 Returns the underlying poller.
 

Detailed Description

template<typename TSocket>
class NNet::TSslSocket< TSocket >

Implements an SSL/TLS layer on top of an underlying connection.

TSslSocket wraps an existing connection (of type TSocket) with SSL/TLS functionality. It creates a new SSL instance (via SSL_new()) using the provided TSslContext, and sets up memory BIOs for reading (Rbio) and writing (Wbio).

The class provides asynchronous operations for both server and client handshakes:

Once the handshake is complete, TSslSocket exposes asynchronous read and write methods (ReadSome() and WriteSome()) that perform SSL_read() and SSL_write(), using an internal I/O loop (via DoIO() and DoHandshake()).

Additionally, TSslSocket allows setting the TLS SNI (via SslSetTlsExtHostName).

Template Parameters
TSocketThe underlying socket type over which SSL/TLS is layered.

Constructor & Destructor Documentation

◆ TSslSocket()

template<typename TSocket>
NNet::TSslSocket< TSocket >::TSslSocket ( TSocket && socket,
TSslContext & ctx )
inline

Constructs a TSslSocket from an underlying socket and an SSL context.

Creates a new SSL instance using the provided context, sets up memory BIOs for I/O, and configures SSL for partial writes.

Parameters
socketAn rvalue reference to the underlying connection handle.
ctxReference to the TSslContext to use.

◆ ~TSslSocket()

template<typename TSocket>
NNet::TSslSocket< TSocket >::~TSslSocket ( )
inline

Destructor.

Frees the SSL instance (and associated BIOs) and destroys any active handshake task.

Member Function Documentation

◆ Accept()

template<typename TSocket>
TFuture< TSslSocket< TSocket > > NNet::TSslSocket< TSocket >::Accept ( )
inline

Asynchronously accepts an incoming SSL connection.

Waits for an incoming connection on the underlying socket, wraps it in a TSslSocket, and performs the handshake.

Returns
A TFuture yielding a TSslSocket representing the accepted connection.

◆ AcceptHandshake()

template<typename TSocket>
TFuture< void > NNet::TSslSocket< TSocket >::AcceptHandshake ( )
inline

Performs the server-side SSL handshake.

Configures the SSL state to accept a connection, then performs the handshake asynchronously.

Returns
A TFuture that completes when the handshake is successful.

◆ Connect()

template<typename TSocket>
TFuture< void > NNet::TSslSocket< TSocket >::Connect ( const TAddress & address,
TTime deadline = TTime::max() )
inline

Initiates the client-side SSL handshake.

Connects to the remote address, sets the SSL state to connect, and performs the handshake.

Parameters
addressThe remote address to connect to.
deadlineOptional timeout for the connection attempt.
Returns
A TFuture that completes when the handshake is successful.

◆ Poller()

template<typename TSocket>
auto NNet::TSslSocket< TSocket >::Poller ( )
inline

Returns the underlying poller.

Returns
The poller associated with the underlying socket.

◆ ReadSome()

template<typename TSocket>
TFuture< ssize_t > NNet::TSslSocket< TSocket >::ReadSome ( void * data,
size_t size )
inline

Asynchronously reads data from the SSL connection.

Performs SSL_read() and, if needed, loops using asynchronous I/O via DoIO() until data is available.

Parameters
dataPointer to the buffer.
sizeMaximum number of bytes to read.
Returns
A TFuture yielding the number of bytes read.

◆ SslSetTlsExtHostName()

template<typename TSocket>
void NNet::TSslSocket< TSocket >::SslSetTlsExtHostName ( const std::string & host)
inline

Sets the TLS SNI (Server Name Indication) extension host name.

This is useful for virtual hosting when connecting to servers that rely on SNI.

Parameters
hostThe server host name.

◆ WriteSome()

template<typename TSocket>
TFuture< ssize_t > NNet::TSslSocket< TSocket >::WriteSome ( const void * data,
size_t size )
inline

Asynchronously writes data to the SSL connection.

Writes the full buffer in a loop using SSL_write() and asynchronous I/O until completion.

Parameters
dataPointer to the data.
sizeThe number of bytes to write.
Returns
A TFuture yielding the total number of bytes written.

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