![]() |
RumPi 1.0
A self-assembling C++ sensor & actuator library for the Raspberry Pi 5
|
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>
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... | |
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.
| typedef std::function<void(const char* theReceivedMessage)> RumPi::Receiver::OnMessageReceived |
A callback invoked with each message received from the connected client.
| 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).
| [in] | portNum | The port to listen on. |
| [in] | messageReceivedHandler | The callback invoked with each received message. |
|
virtual |
| void RumPi::Receiver::ClearReceiveMessageBuffer | ( | ) |
Clears the received-message buffer, under the receive lock.
| void RumPi::Receiver::ClearSendMessage | ( | ) |
Clears the queued send message and marks no message as ready, under the send lock.
|
private |
Accepts a client connection (blocking until one arrives or the socket is shut down), then starts the receive and send worker threads.
| bool RumPi::Receiver::GetIsConnected | ( | ) | const |
Gets whether a client is currently connected.
| bool RumPi::Receiver::GetIsSendMessageReady | ( | ) | const |
Gets whether a message is queued and waiting to be sent.
| int RumPi::Receiver::GetNewSocketFileDescriptor | ( | ) | const |
Gets the connected client's socket file descriptor.
| unsigned int RumPi::Receiver::GetNumCharactersRead | ( | ) | const |
Gets the number of characters read in the last receive.
| unsigned int RumPi::Receiver::GetNumCharactersWritten | ( | ) | const |
Gets the number of characters written in the last send.
| unsigned int RumPi::Receiver::GetPortNumber | ( | ) | const |
Gets the port the server listens on.
| int RumPi::Receiver::GetSocketFileDescriptor | ( | ) | const |
Gets the listening socket file descriptor.
|
overridevirtual |
No-op: the receiver delivers messages via its callback, so there are no raw values to process.
Implements RumPi::BaseComponent.
|
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.
| void RumPi::Receiver::SendMessage | ( | std::string | message | ) |
Queues a message to send (rejecting it if one is already pending) and wakes the send thread.
| [in] | message | The message to queue for sending. |
|
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.
| void RumPi::Receiver::SetPortNumber | ( | unsigned int | portNum | ) |
Sets the port the server listens on.
| [in] | portNum | The port number to set. |
|
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.
Implements RumPi::BaseComponent.
|
overridevirtual |
Stops all loops, unblocks any threads parked in accept/read/wait, joins the connection, send, and receive threads, and closes both sockets.
Implements RumPi::BaseComponent.
|
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.
Implements RumPi::BaseComponent.
|
private |
The thread that accepts a client connection.
|
private |
True while a client is connected and the loops should run.
|
private |
True when a message is queued and waiting to be sent.
|
private |
The connected client's socket file descriptor.
|
private |
The number of characters read in the last receive.
|
private |
The number of characters written in the last send.
|
private |
The port the server listens on.
|
mutableprivate |
Guards the receive buffer.
|
private |
The most recently received message.
|
private |
The thread that reads incoming messages.
|
private |
Signals the send thread that a message is ready (or shutdown).
|
mutableprivate |
Guards the send buffer and ready flag.
|
private |
The message currently queued to send.
|
private |
The thread that sends queued messages.
|
private |
The listening socket file descriptor.
| OnMessageReceived RumPi::Receiver::OnMessageReceivedCallback |
The registered message-received callback.