Core Library  1.7.0.0
Library containing core utilities and tools for threading, networking, logging, INI and CSV file management etc.
core_lib::asio::tcp::TcpConnection Class Referencefinal

TCP connection class. More...

#include <TcpConnection.h>

Inheritance diagram for core_lib::asio::tcp::TcpConnection:

Public Member Functions

 TcpConnection ()=delete
 Default constructor - deleted.
 
 TcpConnection (boost_iocontext_t &ioContext, TcpConnections &connections, size_t minAmountToRead, const defs::check_bytes_left_to_read_t &checkBytesLeftToRead, const defs::message_received_handler_t &messageReceivedHandler, eSendOption sendOption=eSendOption::nagleOn, size_t maxAllowedUnsentAsyncMessages=MAX_UNSENT_ASYNC_MSG_COUNT)
 Initialisation constructor. More...
 
 TcpConnection (const TcpConnection &)=delete
 Copy constructor deleted.
 
TcpConnectionoperator= (const TcpConnection &)=delete
 Copy assignment operator - deleted.
 
 TcpConnection (TcpConnection &&)=delete
 Move constructor deleted.
 
TcpConnectionoperator= (TcpConnection &&)=delete
 Move assignment operator - deleted.
 
virtual ~TcpConnection ()=default
 Default virtual destructor.
 
boost_tcp_t::socket & Socket ()
 Get mutable reference to the socket object. More...
 
const boost_tcp_t::socket & Socket () const
 Get a const reference to the socket object. More...
 
void Connect (const defs::connection_t &endPoint)
 Connect to an endpoint. More...
 
void CloseConnection ()
 Close this connection.
 
void StartAsyncRead ()
 Start aysnchronously reading data when available.
 
bool SendMessageAsync (const defs::char_buffer_t &message)
 Send an asynchronous message. More...
 
bool SendMessageSync (const defs::char_buffer_t &message)
 Send a synchronous message. More...
 
size_t NumberOfUnsentAsyncMessages () const
 Get number of unsent async messages. More...
 

Private Member Functions

void SetClosing (bool closing)
 Set closing flag. More...
 
bool IsClosing () const
 Is the connection closing? More...
 
void ProcessCloseSocket ()
 Process closing of socket.
 
void DestroySelf ()
 Self destruct this object.
 
void AsyncReadFromSocket (size_t amountToRead)
 Asynchronously read an amount. More...
 
void ReadComplete (const boost_sys::error_code &error, size_t bytesReceived, size_t bytesExpected)
 Read completion handler. More...
 
void AsyncWriteToSocket (defs::char_buffer_t const &message)
 Asynchronously write to the socket. More...
 
bool IncrementUnsentAsyncCounter ()
 Increment unsent async message counter. More...
 
void DecrementUnsentAsyncCounter ()
 Decrement unsent async message counter.
 

Private Attributes

std::mutex m_mutex
 Access mutex for thread safety.
 
threads::SyncEvent m_closedEvent {}
 Connection close event.
 
bool m_closing {false}
 Closing connection flag.
 
boost_iocontext_t::strand m_strand
 I/O context strand.
 
TcpConnectionsm_connections
 Reference to TCP connections object.
 
size_t m_minAmountToRead {0}
 Minimum amount to read from socket.
 
defs::check_bytes_left_to_read_t m_checkBytesLeftToRead {}
 Check bytes left to read callback.
 
defs::message_received_handler_t m_messageReceivedHandler {}
 Message received handler callback.
 
eSendOption m_sendOption {eSendOption::nagleOn}
 Socket send option.
 
defs::char_buffer_t m_receiveBuffer {}
 Socket receive buffer.
 
defs::char_buffer_t m_messageBuffer {}
 Message buffer.
 
size_t m_maxAllowedUnsentAsyncMessages {MAX_UNSENT_ASYNC_MSG_COUNT}
 Max allowed unsent async message counter.
 
size_t m_numUnsentAsyncMessages {0}
 Unsent async message counter.
 
boost_tcp_t::socket m_socket
 TCP socket.
 

Detailed Description

TCP connection class.

This class is the one of the fundamental building blocks for the other TCP networking classes.

Constructor & Destructor Documentation

◆ TcpConnection()

core_lib::asio::tcp::TcpConnection::TcpConnection ( boost_iocontext_t ioContext,
TcpConnections connections,
size_t  minAmountToRead,
const defs::check_bytes_left_to_read_t checkBytesLeftToRead,
const defs::message_received_handler_t messageReceivedHandler,
eSendOption  sendOption = eSendOption::nagleOn,
size_t  maxAllowedUnsentAsyncMessages = MAX_UNSENT_ASYNC_MSG_COUNT 
)

Initialisation constructor.

Parameters
[in]ioContext- Reference to I/O context.
[in]connections- Reference to TCP connections object.
[in]minAmountToRead- Minimum amount to read.
[in]checkBytesLeftToRead- Check bytes left to read callback.
[in]messageReceivedHandler- Message received handler callback.
[in]sendOption- Socket send option.
[in]maxAllowedUnsentAsyncMessages- Maximum allowed number of unsent async messages.

Member Function Documentation

◆ AsyncReadFromSocket()

void core_lib::asio::tcp::TcpConnection::AsyncReadFromSocket ( size_t  amountToRead)
private

Asynchronously read an amount.

Parameters
[in]amountToRead- Number of bytes to read from socket.

◆ AsyncWriteToSocket()

void core_lib::asio::tcp::TcpConnection::AsyncWriteToSocket ( defs::char_buffer_t const &  message)
private

Asynchronously write to the socket.

Parameters
[in]message- Message buffer to write.

◆ Connect()

void core_lib::asio::tcp::TcpConnection::Connect ( const defs::connection_t endPoint)

Connect to an endpoint.

Parameters
[in]endPoint- Connection details of some endpoint.

◆ IncrementUnsentAsyncCounter()

bool core_lib::asio::tcp::TcpConnection::IncrementUnsentAsyncCounter ( )
private

Increment unsent async message counter.

Returns
Returns success flag based on whether we can increment.

◆ IsClosing()

bool core_lib::asio::tcp::TcpConnection::IsClosing ( ) const
private

Is the connection closing?

Returns
True if closing, false otherwise.

◆ NumberOfUnsentAsyncMessages()

size_t core_lib::asio::tcp::TcpConnection::NumberOfUnsentAsyncMessages ( ) const

Get number of unsent async messages.

Returns
Number of pending queued async messages

◆ ReadComplete()

void core_lib::asio::tcp::TcpConnection::ReadComplete ( const boost_sys::error_code &  error,
size_t  bytesReceived,
size_t  bytesExpected 
)
private

Read completion handler.

Parameters
[in]error- Error flag if fault occurred.
[in]bytesReceived- Number of bytes received.
[in]bytesExpected- Number of bytes expected.

◆ SendMessageAsync()

bool core_lib::asio::tcp::TcpConnection::SendMessageAsync ( const defs::char_buffer_t message)

Send an asynchronous message.

Parameters
[in]message- Message buffer to send.
Returns
Returns true if posted async message, retruns false if failed to post message.

◆ SendMessageSync()

bool core_lib::asio::tcp::TcpConnection::SendMessageSync ( const defs::char_buffer_t message)

Send a synchronous message.

Parameters
[in]message- Message buffer to send.
Returns
True if sent successfully, false otherwise.

◆ SetClosing()

void core_lib::asio::tcp::TcpConnection::SetClosing ( bool  closing)
private

Set closing flag.

Parameters
[in]closing- Closing state flag.

◆ Socket() [1/2]

boost_tcp_t::socket & core_lib::asio::tcp::TcpConnection::Socket ( )

Get mutable reference to the socket object.

Returns
Socket reference.

◆ Socket() [2/2]

const boost_tcp_t::socket & core_lib::asio::tcp::TcpConnection::Socket ( ) const

Get a const reference to the socket object.

Returns
Const socket reference.

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