RADLib
RADical C++ application framework
RADNet::RADTcpSocket Class Reference

Non-blocking TCP socket that also implements RADCore::RADIODevice. More...

#include <RADNet.h>

+ Inheritance diagram for RADNet::RADTcpSocket:
+ Collaboration diagram for RADNet::RADTcpSocket:

Public Member Functions

 RADTcpSocket (RADCore::EventLoop &loop)
 Creates an unconnected TCP socket using loop for events. More...
 
 ~RADTcpSocket ()
 Closes the socket if needed. More...
 
bool open (RADCore::OpenMode mode) override
 Opens the IODevice facet; networking is established with connectToHost(). More...
 
bool connectToHost (const std::string &address, uint16_t port)
 Starts a non-blocking connection to address:port. More...
 
void close () override
 Closes the TCP descriptor and updates state. More...
 
bool isOpen () const override
 Returns true when the descriptor is open. More...
 
ssize_t readData (void *buffer, size_t maxBytes) override
 Reads up to maxBytes from the socket into buffer. More...
 
ssize_t writeData (const void *data, size_t size) override
 Writes size bytes from data to the socket. More...
 
SocketState state () const
 Returns the current TCP socket state. More...
 
- Public Member Functions inherited from RADCore::RADIODevice
virtual ~RADIODevice ()=default
 Destroys the IO device. More...
 
std::vector< uint8_t > read (size_t maxBytes)
 Reads up to maxBytes into a byte vector. More...
 
std::vector< uint8_t > readAll (size_t chunkSize=8192)
 Reads all available data in chunks. More...
 
ssize_t write (const std::vector< uint8_t > &data)
 Writes a byte vector. More...
 
ssize_t write (const std::string &text)
 Writes string bytes. More...
 
- Public Member Functions inherited from RADCore::RADObject
 RADObject ()
 Creates a RADObject bound to the current thread. More...
 
virtual ~RADObject ()
 Disconnects tracked event links and releases lifetime token. More...
 
void registerLink (const ConnectionRecord &record)
 Registers a connection for cleanup when this object is destroyed. More...
 
std::thread::id threadId () const
 Returns the thread id this object is currently associated with. More...
 
EventLoopassociatedEventLoop () const
 Returns the associated event loop, if any. More...
 
ThreadAffinity threadAffinity () const
 Returns both thread id and associated loop atomically. More...
 
std::weak_ptr< void > lifetimeToken () const
 Returns a weak token used to drop queued callbacks after destruction. More...
 
void moveToThread (std::thread::id tId, EventLoop *loop=nullptr)
 Moves the object's affinity to tId and optional loop. More...
 

Public Attributes

RADCore::Event< void() > connected
 Raised when a connection is established. More...
 
RADCore::Event< void() > disconnected
 Raised when the peer disconnects or the socket closes. More...
 
- Public Attributes inherited from RADCore::RADIODevice
RADCore::Event< void() > readyRead
 Raised when data is available to read. More...
 
RADCore::Event< void(size_t) > bytesWritten
 Raised after bytes are written. More...
 
RADCore::Event< void(std::string) > errorOccurred
 Raised when an IO error occurs. More...
 

Friends

class RADTcpServer
 

Detailed Description

Non-blocking TCP socket that also implements RADCore::RADIODevice.

Constructor & Destructor Documentation

◆ RADTcpSocket()

RADNet::RADTcpSocket::RADTcpSocket ( RADCore::EventLoop loop)
explicit

Creates an unconnected TCP socket using loop for events.

◆ ~RADTcpSocket()

RADNet::RADTcpSocket::~RADTcpSocket ( )

Closes the socket if needed.

Member Function Documentation

◆ close()

void RADNet::RADTcpSocket::close ( )
overridevirtual

Closes the TCP descriptor and updates state.

Implements RADCore::RADIODevice.

◆ connectToHost()

bool RADNet::RADTcpSocket::connectToHost ( const std::string &  address,
uint16_t  port 
)

Starts a non-blocking connection to address:port.

◆ isOpen()

bool RADNet::RADTcpSocket::isOpen ( ) const
overridevirtual

Returns true when the descriptor is open.

Implements RADCore::RADIODevice.

◆ open()

bool RADNet::RADTcpSocket::open ( RADCore::OpenMode  mode)
overridevirtual

Opens the IODevice facet; networking is established with connectToHost().

Implements RADCore::RADIODevice.

◆ readData()

ssize_t RADNet::RADTcpSocket::readData ( void *  buffer,
size_t  maxBytes 
)
overridevirtual

Reads up to maxBytes from the socket into buffer.

Implements RADCore::RADIODevice.

◆ state()

SocketState RADNet::RADTcpSocket::state ( ) const

Returns the current TCP socket state.

◆ writeData()

ssize_t RADNet::RADTcpSocket::writeData ( const void *  data,
size_t  size 
)
overridevirtual

Writes size bytes from data to the socket.

Implements RADCore::RADIODevice.

Friends And Related Function Documentation

◆ RADTcpServer

friend class RADTcpServer
friend

Member Data Documentation

◆ connected

RADCore::Event<void () > RADNet::RADTcpSocket::connected

Raised when a connection is established.

◆ disconnected

RADCore::Event<void () > RADNet::RADTcpSocket::disconnected

Raised when the peer disconnects or the socket closes.


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