trantor
Non-blocking I/O cross-platform TCP network library, using C++14
Loading...
Searching...
No Matches
trantor::TcpConnection Class Referenceabstract

This class represents a TCP connection. More...

#include <trantor/net/TcpConnection.h>

Collaboration diagram for trantor::TcpConnection:

Public Member Functions

virtual void send (const char *msg, size_t len)=0
 Send some data to the peer.
virtual void send (const void *msg, size_t len)=0
virtual void send (const std::string &msg)=0
virtual void send (std::string &&msg)=0
virtual void send (const MsgBuffer &buffer)=0
virtual void send (MsgBuffer &&buffer)=0
virtual void send (const std::shared_ptr< std::string > &msgPtr)=0
virtual void send (const std::shared_ptr< MsgBuffer > &msgPtr)=0
virtual void sendFile (const char *fileName, long long offset=0, long long length=0)=0
 Send a file to the peer.
virtual void sendFile (const wchar_t *fileName, long long offset=0, long long length=0)=0
 Send a file to the peer.
virtual void sendStream (std::function< std::size_t(char *, std::size_t)> callback)=0
 Send a stream to the peer.
virtual AsyncStreamPtr sendAsyncStream (bool disableKickoff=false)=0
 Send a stream to the peer asynchronously.
virtual const InetAddresslocalAddr () const =0
 Get the local address of the connection.
virtual const InetAddresspeerAddr () const =0
 Get the remote address of the connection.
virtual bool connected () const =0
 Return true if the connection is established.
virtual bool disconnected () const =0
 Return false if the connection is established.
virtual void setHighWaterMarkCallback (const HighWaterMarkCallback &cb, size_t markLen)=0
 Set the high water mark callback.
virtual void setTcpNoDelay (bool on)=0
 Set the TCP_NODELAY option to the socket.
virtual void shutdown ()=0
 Shutdown the connection.
virtual void forceClose ()=0
 Close the connection forcefully.
virtual EventLoopgetLoop ()=0
 Get the event loop in which the connection I/O is handled.
void setContext (const std::shared_ptr< void > &context)
 Set the custom data on the connection.
void setContext (std::shared_ptr< void > &&context)
virtual std::string applicationProtocol () const =0
template<typename T>
std::shared_ptr< T > getContext () const
 Get the custom data from the connection.
bool hasContext () const
 Return true if the custom data is set by user.
void clearContext ()
 Clear the custom data.
virtual void keepAlive ()=0
 Call this method to avoid being kicked off by TcpServer, refer to the kickoffIdleConnections method in the TcpServer class.
virtual bool isKeepAlive ()=0
 Return true if the keepAlive() method is called.
virtual size_t bytesSent () const =0
 Return the number of bytes sent.
virtual size_t bytesReceived () const =0
 Return the number of bytes received.
virtual bool isSSLConnection () const =0
 Check whether the connection is SSL encrypted.
virtual MsgBuffergetRecvBuffer ()=0
 Get buffer of unprompted data.
virtual CertificatePtr peerCertificate () const =0
 Get peer certificate (if any).
virtual std::string sniName () const =0
 Get the SNI name (for server connections only).
virtual void startEncryption (TLSPolicyPtr policy, bool isServer, std::function< void(const TcpConnectionPtr &)> upgradeCallback=nullptr)=0
 Start TLS. If the connection is specified as a server, the connection will be upgraded to a TLS server connection. If the connection is specified as a client, the connection will be upgraded to a TLS client.
void startClientEncryption (std::function< void(const TcpConnectionPtr &)> &&callback, bool useOldTLS=false, bool validateCert=true, const std::string &hostname="", const std::vector< std::pair< std::string, std::string > > &sslConfCmds={})
 Start TLS as a client.
void setValidationPolicy (TLSPolicy &&policy)
void setRecvMsgCallback (const RecvMessageCallback &cb)
void setRecvMsgCallback (RecvMessageCallback &&cb)
void setConnectionCallback (const ConnectionCallback &cb)
void setConnectionCallback (ConnectionCallback &&cb)
void setWriteCompleteCallback (const WriteCompleteCallback &cb)
void setWriteCompleteCallback (WriteCompleteCallback &&cb)
void setCloseCallback (const CloseCallback &cb)
void setCloseCallback (CloseCallback &&cb)
CloseCallback getCloseCallback () const
void setSSLErrorCallback (const SSLErrorCallback &cb)
void setSSLErrorCallback (SSLErrorCallback &&cb)
virtual void connectEstablished ()=0
virtual void connectDestroyed ()=0
virtual void enableKickingOff (size_t timeout, const std::shared_ptr< TimingWheel > &timingWheel)=0
virtual void forwardToTLSBuffer (MsgBuffer *buffer)=0

Protected Attributes

RecvMessageCallback recvMsgCallback_
ConnectionCallback connectionCallback_
CloseCallback closeCallback_
WriteCompleteCallback writeCompleteCallback_
HighWaterMarkCallback highWaterMarkCallback_
SSLErrorCallback sslErrorCallback_
TLSPolicy tlsPolicy_

Friends

class TcpServer
class TcpConnectionImpl
class TcpClient

Detailed Description

This class represents a TCP connection.

Member Function Documentation

◆ bytesReceived()

virtual size_t trantor::TcpConnection::bytesReceived ( ) const
pure virtual

Return the number of bytes received.

Returns
size_t

◆ bytesSent()

virtual size_t trantor::TcpConnection::bytesSent ( ) const
pure virtual

Return the number of bytes sent.

Returns
size_t

◆ connected()

virtual bool trantor::TcpConnection::connected ( ) const
pure virtual

Return true if the connection is established.

Returns
true
false

◆ disconnected()

virtual bool trantor::TcpConnection::disconnected ( ) const
pure virtual

Return false if the connection is established.

Returns
true
false

◆ getContext()

template<typename T>
std::shared_ptr< T > trantor::TcpConnection::getContext ( ) const
inline

Get the custom data from the connection.

Template Parameters
T
Returns
std::shared_ptr<T>

◆ getLoop()

virtual EventLoop * trantor::TcpConnection::getLoop ( )
pure virtual

Get the event loop in which the connection I/O is handled.

Returns
EventLoop*

◆ hasContext()

bool trantor::TcpConnection::hasContext ( ) const
inline

Return true if the custom data is set by user.

Returns
true
false

◆ isKeepAlive()

virtual bool trantor::TcpConnection::isKeepAlive ( )
pure virtual

Return true if the keepAlive() method is called.

Returns
true
false

◆ isSSLConnection()

virtual bool trantor::TcpConnection::isSSLConnection ( ) const
pure virtual

Check whether the connection is SSL encrypted.

Returns
true
false

◆ localAddr()

virtual const InetAddress & trantor::TcpConnection::localAddr ( ) const
pure virtual

Get the local address of the connection.

Returns
const InetAddress&

◆ peerAddr()

virtual const InetAddress & trantor::TcpConnection::peerAddr ( ) const
pure virtual

Get the remote address of the connection.

Returns
const InetAddress&

◆ peerCertificate()

virtual CertificatePtr trantor::TcpConnection::peerCertificate ( ) const
pure virtual

Get peer certificate (if any).

Returns
pointer to Certificate object or nullptr if no certificate was provided

◆ send()

virtual void trantor::TcpConnection::send ( const char * msg,
size_t len )
pure virtual

Send some data to the peer.

Parameters
msg
len

◆ sendAsyncStream()

virtual AsyncStreamPtr trantor::TcpConnection::sendAsyncStream ( bool disableKickoff = false)
pure virtual

Send a stream to the peer asynchronously.

Parameters
disableKickoffDisable the kickoff mechanism. If this parameter is enabled, the connection will not be closed after the inactive timeout.
Note
The subsequent data sent after the async stream will be sent after the stream is closed.

◆ sendFile() [1/2]

virtual void trantor::TcpConnection::sendFile ( const char * fileName,
long long offset = 0,
long long length = 0 )
pure virtual

Send a file to the peer.

Parameters
fileNamein UTF-8
offset
length

◆ sendFile() [2/2]

virtual void trantor::TcpConnection::sendFile ( const wchar_t * fileName,
long long offset = 0,
long long length = 0 )
pure virtual

Send a file to the peer.

Parameters
fileNamein wide string (eg. windows native UCS-2)
offset
length

◆ sendStream()

virtual void trantor::TcpConnection::sendStream ( std::function< std::size_t(char *, std::size_t)> callback)
pure virtual

Send a stream to the peer.

Parameters
callbackfunction to retrieve the stream data (stream ends when a zero size is returned) the callback will be called with nullptr when the send is finished/interrupted, so that it cleans up any internal data (ex: close file).
Warning
The buffer size should be >= 10 to allow http chunked-encoding data stream

◆ setContext()

void trantor::TcpConnection::setContext ( const std::shared_ptr< void > & context)
inline

Set the custom data on the connection.

Parameters
context

◆ setHighWaterMarkCallback()

virtual void trantor::TcpConnection::setHighWaterMarkCallback ( const HighWaterMarkCallback & cb,
size_t markLen )
pure virtual

Set the high water mark callback.

Parameters
cbThe callback is called when the data in sending buffer is larger than the water mark.
markLenThe water mark in bytes.

◆ setTcpNoDelay()

virtual void trantor::TcpConnection::setTcpNoDelay ( bool on)
pure virtual

Set the TCP_NODELAY option to the socket.

Parameters
on

◆ shutdown()

virtual void trantor::TcpConnection::shutdown ( )
pure virtual

Shutdown the connection.

Note
This method only closes the writing direction.

◆ sniName()

virtual std::string trantor::TcpConnection::sniName ( ) const
pure virtual

Get the SNI name (for server connections only).

Returns
Empty string if no SNI name was provided (not an SSL connection or peer did not provide SNI)

◆ startClientEncryption()

void trantor::TcpConnection::startClientEncryption ( std::function< void(const TcpConnectionPtr &)> && callback,
bool useOldTLS = false,
bool validateCert = true,
const std::string & hostname = "",
const std::vector< std::pair< std::string, std::string > > & sslConfCmds = {} )
inline

Start TLS as a client.

Note
This method is only available for non-SSL connections.

◆ startEncryption()

virtual void trantor::TcpConnection::startEncryption ( TLSPolicyPtr policy,
bool isServer,
std::function< void(const TcpConnectionPtr &)> upgradeCallback = nullptr )
pure virtual

Start TLS. If the connection is specified as a server, the connection will be upgraded to a TLS server connection. If the connection is specified as a client, the connection will be upgraded to a TLS client.

Note
This method is only available for non-SSL connections.

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