21#ifndef G_NET_SOCKET_PROTOCOL_H
22#define G_NET_SOCKET_PROTOCOL_H
37 class SocketProtocol ;
38 class SocketProtocolImp ;
39 class SocketProtocolSink ;
61 G_EXCEPTION_CLASS( ReadError ,
tx(
"peer disconnected") )
62 G_EXCEPTION( SendError ,
tx(
"peer disconnected") )
63 G_EXCEPTION( ShutdownError ,
tx(
"shutdown error") )
64 G_EXCEPTION( SecureConnectionTimeout ,
tx(
"secure connection timeout") )
65 G_EXCEPTION( Shutdown ,
tx(
"peer shutdown") )
66 G_EXCEPTION( OtherEventError ,
tx(
"network event") )
67 G_EXCEPTION( ProtocolError ,
tx(
"socket protocol error") )
72 unsigned int secure_connection_timeout {0U} ;
73 std::string server_tls_profile ;
74 std::string client_tls_profile ;
75 Config & set_read_buffer_size( std::size_t n ) noexcept ;
76 Config & set_secure_connection_timeout(
unsigned int t ) noexcept ;
77 Config & set_server_tls_profile(
const std::string & s ) ;
78 Config & set_client_tls_profile(
const std::string & s ) ;
88 bool readEvent(
bool no_throw_on_peer_disconnect =
false ) ;
100 void otherEvent( EventHandler::Reason ,
bool no_throw_on_peer_disconnect =
false ) ;
108 bool send(
const std::string & data , std::size_t offset ) ;
121 bool send( std::string_view data ) ;
124 bool send(
const std::vector<std::string_view> & data , std::size_t offset = 0U ) ;
169 std::unique_ptr<SocketProtocolImp> m_imp ;
182 virtual void onData(
const char * , std::size_t ) = 0 ;
185 virtual void onSecure(
const std::string & peer_certificate ,
186 const std::string & protocol ,
const std::string & cipher ) = 0 ;
200inline GNet::SocketProtocol::Config & GNet::SocketProtocol::Config::set_read_buffer_size( std::size_t n )
noexcept { read_buffer_size = n ;
return *this ; }
201inline GNet::SocketProtocol::Config & GNet::SocketProtocol::Config::set_secure_connection_timeout(
unsigned int t )
noexcept { secure_connection_timeout = t ;
return *this ; }
202inline GNet::SocketProtocol::Config & GNet::SocketProtocol::Config::set_server_tls_profile(
const std::string & s ) { server_tls_profile = s ;
return *this ; }
203inline GNet::SocketProtocol::Config & GNet::SocketProtocol::Config::set_client_tls_profile(
const std::string & s ) { client_tls_profile = s ;
return *this ; }
A base class for classes that have a file descriptor and handle asynchronous events from the event lo...
A lightweight object containing an ExceptionHandler pointer, optional ExceptionSource pointer and opt...
An interface used by GNet::SocketProtocol to deliver data from a socket.
virtual ~SocketProtocolSink()=default
Destructor.
virtual void onPeerDisconnect()=0
Called, if enabled by the readEvent()/otherEvent() parameter, when the peer disconnects with a socket...
virtual void onSecure(const std::string &peer_certificate, const std::string &protocol, const std::string &cipher)=0
Called once the secure socket protocol has been successfully negotiated.
virtual void onData(const char *, std::size_t)=0
Called when data is read from the socket.
An interface for implementing a low-level TLS/SSL protocol layer on top of a connected non-blocking s...
bool readEvent(bool no_throw_on_peer_disconnect=false)
Called on receipt of a read event.
bool send(const std::string &data, std::size_t offset)
Sends data.
SocketProtocol(EventHandler &, EventState, Sink &, StreamSocket &, const Config &)
Constructor.
bool writeEvent()
Called on receipt of a write event.
bool secure() const
Returns true if the connection is currently secure ie.
bool secureAcceptCapable() const
Returns true if the implementation supports TLS/SSL and a "server" profile has been configured.
void secureAccept()
Waits for the TLS/SSL handshake protocol, acting as a server.
~SocketProtocol()
Destructor.
void otherEvent(EventHandler::Reason, bool no_throw_on_peer_disconnect=false)
Called on receipt of an 'other' event.
void shutdown()
Initiates a TLS-close if secure, together with a Socket::shutdown(1).
std::string peerCertificate() const
Returns the peer's TLS/SSL certificate or the empty string.
bool secureConnectCapable() const
Returns true if the implementation supports TLS/SSL and a "client" profile has been configured.
bool raw() const
Returns true if no TLS/SSL.
void secureConnect()
Initiates the TLS/SSL handshake, acting as a client.
A derivation of GNet::Socket for a stream socket.
constexpr const char * tx(const char *p) noexcept
A briefer alternative to G::gettext_noop().
A configuration structure for GNet::SocketProtocol.
A set of compile-time buffer sizes.