RumPi 1.0
A self-assembling C++ sensor & actuator library for the Raspberry Pi 5
RumPi::Receiver Class Reference

Implements TCP client/server socket communication, running its accept, send, and receive loops on their own threads and invoking a callback for each message received. More...

#include <Receiver.h>

Inheritance diagram for RumPi::Receiver:
[legend]
Collaboration diagram for RumPi::Receiver:
[legend]

Public Types

typedef std::function< void(const char *theReceivedMessage)> OnMessageReceived
 A callback invoked with each message received from the connected client. More...
 

Public Member Functions

 Receiver (unsigned int portNum, OnMessageReceived messageReceivedHandler)
 Builds a receiver bound to the given port with the given message-received callback (sockets/threads start on TurnOn). More...
 
virtual ~Receiver ()
 Destroys the receiver, stopping its threads and closing its sockets via TurnOff. More...
 
virtual void TurnOn () override
 Creates, binds, and listens on the server socket, then accepts a client connection on a background thread so startup does not block; reports failures through the AlertManager. More...
 
virtual void TurnOff () override
 Stops all loops, unblocks any threads parked in accept/read/wait, joins the connection, send, and receive threads, and closes both sockets. More...
 
virtual void ProcessRawValues () override
 No-op: the receiver delivers messages via its callback, so there are no raw values to process. More...
 
virtual std::string ToString () const override
 Serializes the receiver's connection state, buffers, port, byte counts, and socket descriptors to a string, copying the shared buffers under their locks to avoid racing the worker threads. More...
 
void ClearSendMessage ()
 Clears the queued send message and marks no message as ready, under the send lock. More...
 
void ClearReceiveMessageBuffer ()
 Clears the received-message buffer, under the receive lock. More...
 
void SendMessage (std::string message)
 Queues a message to send (rejecting it if one is already pending) and wakes the send thread. More...
 
bool GetIsConnected () const
 Gets whether a client is currently connected. More...
 
bool GetIsSendMessageReady () const
 Gets whether a message is queued and waiting to be sent. More...
 
unsigned int GetPortNumber () const
 Gets the port the server listens on. More...
 
unsigned int GetNumCharactersRead () const
 Gets the number of characters read in the last receive. More...
 
unsigned int GetNumCharactersWritten () const
 Gets the number of characters written in the last send. More...
 
int GetSocketFileDescriptor () const
 Gets the listening socket file descriptor. More...
 
int GetNewSocketFileDescriptor () const
 Gets the connected client's socket file descriptor. More...
 
void SetPortNumber (unsigned int portNum)
 Sets the port the server listens on. More...
 
- Public Member Functions inherited from RumPi::BaseComponent
 BaseComponent ()
 Builds a base component with an unknown type and an empty name. More...
 
virtual ~BaseComponent ()
 Virtual destructor so deleting a component through a BaseComponent* also cleans up the concrete subclass. More...
 
virtual void TurnOn ()=0
 Turns the component on (e.g. More...
 
virtual void TurnOff ()=0
 Turns the component off. More...
 
virtual std::string ToString () const =0
 Serializes the component's current readings to a string (usually JSON). More...
 
virtual void ProcessRawValues ()=0
 Reads and processes the component's latest raw values (called once per poll). More...
 
virtual EComponentType GetComponentType () const
 Gets the component's type. More...
 
const std::string & GetName () const
 Gets the component's unique per-instance name. More...
 
void SetName (const std::string &name)
 Sets the component's unique per-instance name. More...
 

Public Attributes

OnMessageReceived OnMessageReceivedCallback
 The registered message-received callback. More...
 

Private Member Functions

void ConnectToSocket ()
 Accepts a client connection (blocking until one arrives or the socket is shut down), then starts the receive and send worker threads. More...
 
void SendViaThread ()
 Send worker: waits for a queued message (or shutdown), then writes it to the client with MSG_NOSIGNAL so a mid-send disconnect returns an error instead of raising SIGPIPE. More...
 
void ReceiveViaThread ()
 Receive worker: reads incoming messages in a loop, forwards each to the registered callback, acknowledges the client, and stops when the client disconnects or errors. More...
 

Private Attributes

std::atomic< bool > myIsConnected
 True while a client is connected and the loops should run. More...
 
std::atomic< bool > myIsSendMessageReady
 True when a message is queued and waiting to be sent. More...
 
std::string mySendMessageBuffer
 The message currently queued to send. More...
 
std::string myReceiveMessageBuffer
 The most recently received message. More...
 
unsigned int myPortNumber
 The port the server listens on. More...
 
std::atomic< unsigned int > myNumCharactersRead
 The number of characters read in the last receive. More...
 
std::atomic< unsigned int > myNumCharactersWritten
 The number of characters written in the last send. More...
 
std::atomic< int > mySocketFileDescriptor
 The listening socket file descriptor. More...
 
std::atomic< int > myNewSocketFileDescriptor
 The connected client's socket file descriptor. More...
 
std::thread myConnectionThread
 The thread that accepts a client connection. More...
 
std::thread mySendThread
 The thread that sends queued messages. More...
 
std::thread myReceiveThread
 The thread that reads incoming messages. More...
 
std::mutex mySendLock
 Guards the send buffer and ready flag. More...
 
std::mutex myReceiveLock
 Guards the receive buffer. More...
 
std::condition_variable mySendCondition
 Signals the send thread that a message is ready (or shutdown). More...
 

Additional Inherited Members

- Static Public Member Functions inherited from RumPi::BaseComponent
static std::string ComponentTypeToString (const EComponentType theComponentTypeToConvert)
 Maps an EComponentType to its string name. More...
 
static EComponentType StringToComponentType (const std::string &theComponentTypeName)
 Maps a type name back to its EComponentType (the inverse of ComponentTypeToString), so the client can turn the type carried on the wire back into a value it can look an icon up with. More...
 
- Protected Attributes inherited from RumPi::BaseComponent
EComponentType myComponentType
 The kind of component this is (set by each concrete subclass). More...
 
std::string myName
 A unique per-instance name, assigned when the component is added to the manager. More...
 

Detailed Description

Implements TCP client/server socket communication, running its accept, send, and receive loops on their own threads and invoking a callback for each message received.

Author
Eddie O'Hagan
Date
8/13/2026

Member Typedef Documentation

◆ OnMessageReceived

typedef std::function<void(const char* theReceivedMessage)> RumPi::Receiver::OnMessageReceived

A callback invoked with each message received from the connected client.

Constructor & Destructor Documentation

◆ Receiver()

RumPi::Receiver::Receiver ( unsigned int  portNum,
OnMessageReceived  messageReceivedHandler 
)

Builds a receiver bound to the given port with the given message-received callback (sockets/threads start on TurnOn).

Author
Eddie O'Hagan
Date
8/13/2026
Receiver receiver(8080, OnMessage);
Receiver(unsigned int portNum, OnMessageReceived messageReceivedHandler)
Builds a receiver bound to the given port with the given message-received callback (sockets/threads s...
Definition: Receiver.cpp:20
Parameters
[in]portNumThe port to listen on.
[in]messageReceivedHandlerThe callback invoked with each received message.

◆ ~Receiver()

RumPi::Receiver::~Receiver ( )
virtual

Destroys the receiver, stopping its threads and closing its sockets via TurnOff.

Author
Eddie O'Hagan
Date
8/13/2026
Receiver* receiver = new Receiver(8080, OnMessage);
delete receiver;

Member Function Documentation

◆ ClearReceiveMessageBuffer()

void RumPi::Receiver::ClearReceiveMessageBuffer ( )

Clears the received-message buffer, under the receive lock.

Author
Eddie O'Hagan
Date
8/13/2026
receiver.ClearReceiveMessageBuffer();

◆ ClearSendMessage()

void RumPi::Receiver::ClearSendMessage ( )

Clears the queued send message and marks no message as ready, under the send lock.

Author
Eddie O'Hagan
Date
8/13/2026
receiver.ClearSendMessage();

◆ ConnectToSocket()

void RumPi::Receiver::ConnectToSocket ( )
private

Accepts a client connection (blocking until one arrives or the socket is shut down), then starts the receive and send worker threads.

Author
Eddie O'Hagan
Date
8/13/2026
//Started on its own thread by TurnOn().

◆ GetIsConnected()

bool RumPi::Receiver::GetIsConnected ( ) const

Gets whether a client is currently connected.

Author
Eddie O'Hagan
Date
8/13/2026
bool connected = receiver.GetIsConnected();
Returns
True if a client is connected.

◆ GetIsSendMessageReady()

bool RumPi::Receiver::GetIsSendMessageReady ( ) const

Gets whether a message is queued and waiting to be sent.

Author
Eddie O'Hagan
Date
8/13/2026
bool ready = receiver.GetIsSendMessageReady();
Returns
True if a message is queued to send.

◆ GetNewSocketFileDescriptor()

int RumPi::Receiver::GetNewSocketFileDescriptor ( ) const

Gets the connected client's socket file descriptor.

Author
Eddie O'Hagan
Date
8/13/2026
int fd = receiver.GetNewSocketFileDescriptor();
Returns
The client socket file descriptor.

◆ GetNumCharactersRead()

unsigned int RumPi::Receiver::GetNumCharactersRead ( ) const

Gets the number of characters read in the last receive.

Author
Eddie O'Hagan
Date
8/13/2026
unsigned int read = receiver.GetNumCharactersRead();
Returns
The number of characters read.

◆ GetNumCharactersWritten()

unsigned int RumPi::Receiver::GetNumCharactersWritten ( ) const

Gets the number of characters written in the last send.

Author
Eddie O'Hagan
Date
8/13/2026
unsigned int written = receiver.GetNumCharactersWritten();
Returns
The number of characters written.

◆ GetPortNumber()

unsigned int RumPi::Receiver::GetPortNumber ( ) const

Gets the port the server listens on.

Author
Eddie O'Hagan
Date
8/13/2026
unsigned int port = receiver.GetPortNumber();
Returns
The port number.

◆ GetSocketFileDescriptor()

int RumPi::Receiver::GetSocketFileDescriptor ( ) const

Gets the listening socket file descriptor.

Author
Eddie O'Hagan
Date
8/13/2026
int fd = receiver.GetSocketFileDescriptor();
Returns
The listening socket file descriptor.

◆ ProcessRawValues()

void RumPi::Receiver::ProcessRawValues ( )
overridevirtual

No-op: the receiver delivers messages via its callback, so there are no raw values to process.

Author
Eddie O'Hagan
Date
8/13/2026
receiver.ProcessRawValues();

Implements RumPi::BaseComponent.

◆ ReceiveViaThread()

void RumPi::Receiver::ReceiveViaThread ( )
private

Receive worker: reads incoming messages in a loop, forwards each to the registered callback, acknowledges the client, and stops when the client disconnects or errors.

Author
Eddie O'Hagan
Date
8/13/2026
//Started on its own thread by ConnectToSocket().

◆ SendMessage()

void RumPi::Receiver::SendMessage ( std::string  message)

Queues a message to send (rejecting it if one is already pending) and wakes the send thread.

Author
Eddie O'Hagan
Date
8/13/2026
receiver.SendMessage("Hello");
Parameters
[in]messageThe message to queue for sending.

◆ SendViaThread()

void RumPi::Receiver::SendViaThread ( )
private

Send worker: waits for a queued message (or shutdown), then writes it to the client with MSG_NOSIGNAL so a mid-send disconnect returns an error instead of raising SIGPIPE.

Author
Eddie O'Hagan
Date
8/13/2026
//Started on its own thread by ConnectToSocket().

◆ SetPortNumber()

void RumPi::Receiver::SetPortNumber ( unsigned int  portNum)

Sets the port the server listens on.

Author
Eddie O'Hagan
Date
8/13/2026
receiver.SetPortNumber(9090);
Parameters
[in]portNumThe port number to set.

◆ ToString()

std::string RumPi::Receiver::ToString ( ) const
overridevirtual

Serializes the receiver's connection state, buffers, port, byte counts, and socket descriptors to a string, copying the shared buffers under their locks to avoid racing the worker threads.

Author
Eddie O'Hagan
Date
8/13/2026
std::string data = receiver.ToString();
Returns
A multi-line string describing the receiver's state.

Implements RumPi::BaseComponent.

◆ TurnOff()

void RumPi::Receiver::TurnOff ( )
overridevirtual

Stops all loops, unblocks any threads parked in accept/read/wait, joins the connection, send, and receive threads, and closes both sockets.

Author
Eddie O'Hagan
Date
8/13/2026
receiver.TurnOff();

Implements RumPi::BaseComponent.

◆ TurnOn()

void RumPi::Receiver::TurnOn ( )
overridevirtual

Creates, binds, and listens on the server socket, then accepts a client connection on a background thread so startup does not block; reports failures through the AlertManager.

Author
Eddie O'Hagan
Date
8/13/2026
receiver.TurnOn();

Implements RumPi::BaseComponent.

Member Data Documentation

◆ myConnectionThread

std::thread RumPi::Receiver::myConnectionThread
private

The thread that accepts a client connection.

◆ myIsConnected

std::atomic<bool> RumPi::Receiver::myIsConnected
private

True while a client is connected and the loops should run.

◆ myIsSendMessageReady

std::atomic<bool> RumPi::Receiver::myIsSendMessageReady
private

True when a message is queued and waiting to be sent.

◆ myNewSocketFileDescriptor

std::atomic<int> RumPi::Receiver::myNewSocketFileDescriptor
private

The connected client's socket file descriptor.

◆ myNumCharactersRead

std::atomic<unsigned int> RumPi::Receiver::myNumCharactersRead
private

The number of characters read in the last receive.

◆ myNumCharactersWritten

std::atomic<unsigned int> RumPi::Receiver::myNumCharactersWritten
private

The number of characters written in the last send.

◆ myPortNumber

unsigned int RumPi::Receiver::myPortNumber
private

The port the server listens on.

◆ myReceiveLock

std::mutex RumPi::Receiver::myReceiveLock
mutableprivate

Guards the receive buffer.

◆ myReceiveMessageBuffer

std::string RumPi::Receiver::myReceiveMessageBuffer
private

The most recently received message.

◆ myReceiveThread

std::thread RumPi::Receiver::myReceiveThread
private

The thread that reads incoming messages.

◆ mySendCondition

std::condition_variable RumPi::Receiver::mySendCondition
private

Signals the send thread that a message is ready (or shutdown).

◆ mySendLock

std::mutex RumPi::Receiver::mySendLock
mutableprivate

Guards the send buffer and ready flag.

◆ mySendMessageBuffer

std::string RumPi::Receiver::mySendMessageBuffer
private

The message currently queued to send.

◆ mySendThread

std::thread RumPi::Receiver::mySendThread
private

The thread that sends queued messages.

◆ mySocketFileDescriptor

std::atomic<int> RumPi::Receiver::mySocketFileDescriptor
private

The listening socket file descriptor.

◆ OnMessageReceivedCallback

OnMessageReceived RumPi::Receiver::OnMessageReceivedCallback

The registered message-received callback.


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