Net++
A class-based C++ encapsulation over the POSIX Sockets API
Loading...
Searching...
No Matches
tcp::Connection Class Reference

Represents a TCP connection. More...

#include <tcp.hpp>

Inheritance diagram for tcp::Connection:

Public Types

using connection_ptr = std::shared_ptr< Connection >
 
using event_ptr = std::unique_ptr< bufferevent, decltype(&bufferevent_free)>
 

Public Member Functions

 Connection (Connection &&other) noexcept
 The move constructor for the Connection class.
 
Connectionoperator= (Connection &&other) noexcept
 The move constructor for the Connection class.
 
void set_nonblocking ()
 Sets the underlying socket handle as nonblocking and creates a bufferevent for the socket.
 
bool is_nonblocking ()
 Checks if the underlying socket handle is nonblocking.
 
void send_async (std::string_view msg, std::function< void()> callback)
 Sends a message through the endpoint asynchronously.
 
void recv_async (std::vector< std::byte > buf, std::function< void()> callback)
 Receives a message from the endpoint asynchronously.
 
void send_sync (std::string_view msg)
 Sends a message through the endpoint synchronously.
 
void recv_sync (std::string &buf)
 Receives a message from the endpoint synchronously.
 

Static Public Member Functions

static connection_ptr create (int32_t socket_fd, async::IOContext::io_context_ptr io_context)
 Creates a shared ptr to a new Connection instance.
 

Detailed Description

Represents a TCP connection.

Encapsulates a connected socket, allowing send and receive capabilities, as well as I/O multiplexing using libevent.

Member Typedef Documentation

◆ connection_ptr

using tcp::Connection::connection_ptr = std::shared_ptr<Connection>

◆ event_ptr

using tcp::Connection::event_ptr = std::unique_ptr<bufferevent, decltype(&bufferevent_free)>

Constructor & Destructor Documentation

◆ Connection()

tcp::Connection::Connection ( Connection &&  other)
noexcept

The move constructor for the Connection class.

Moves the source socket handle, io context, and event to the current Connection instance, and invalidates the source instance's socket handle.

Parameters
otherAn rvalue reference to the object to steal resources from

Member Function Documentation

◆ create()

static connection_ptr tcp::Connection::create ( int32_t  socket_fd,
async::IOContext::io_context_ptr  io_context 
)
static

Creates a shared ptr to a new Connection instance.

Parameters
socket_fdThe Connected socket file descriptor to pass into the private constructor
io_contextAn optional IOContext object
Returns
A shared pointer to the new Connection instance

◆ is_nonblocking()

bool tcp::Connection::is_nonblocking ( )

Checks if the underlying socket handle is nonblocking.

Returns
A boolean indicating whether or not the socket is nonblocking

◆ operator=()

Connection & tcp::Connection::operator= ( Connection &&  other)
noexcept

The move constructor for the Connection class.

Closes the current Connection's socket handle if applicable, moves the source socket handle, io context, and event to the current connection instance , and invalidates the source instance's socket handle.

Parameters
otherAn rvalue reference to the object to steal resources from

◆ recv_async()

void tcp::Connection::recv_async ( std::vector< std::byte >  buf,
std::function< void()>  callback 
)

Receives a message from the endpoint asynchronously.

Only use when the socket is set to nonblocking. Asynchronously receives all bytes into buf and calls the callback function when the operation is completed

Parameters
bufThe buffer of bytes to receive data
callbackThe function to call once the receive operation completes

◆ recv_sync()

void tcp::Connection::recv_sync ( std::string &  buf)

Receives a message from the endpoint synchronously.

Only use when is_nonblocking() returns false. Synchronously receives all bytes into buf.

Parameters
bufThe buffer of bytes to receive data

◆ send_async()

void tcp::Connection::send_async ( std::string_view  msg,
std::function< void()>  callback 
)

Sends a message through the endpoint asynchronously.

Only use when the socket is set to nonblocking. Asynchronously sends all bytes in msg and calls the callback function when the operation is completed

Parameters
msgThe buffer of bytes to send
callbackThe function to call once the send operation completes

◆ send_sync()

void tcp::Connection::send_sync ( std::string_view  msg)

Sends a message through the endpoint synchronously.

Only use when is_nonblocking() returns false. Synchronously sends all bytes in msg.

Parameters
msgthe buffer of bytes to send

◆ set_nonblocking()

void tcp::Connection::set_nonblocking ( )

Sets the underlying socket handle as nonblocking and creates a bufferevent for the socket.


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