6#include <condition_variable>
14#include <sys/socket.h>
15#include <netinet/in.h>
129 virtual void TurnOn()
override;
142 virtual void TurnOff()
override;
169 virtual std::string
ToString()
const override;
Abstract base class for every component (sensor, actuator, etc.) the library manages.
Definition: BaseComponent.h:60
Implements TCP client/server socket communication, running its accept, send, and receive loops on the...
Definition: Receiver.h:27
bool GetIsSendMessageReady() const
Gets whether a message is queued and waiting to be sent.
Definition: Receiver.cpp:494
virtual std::string ToString() const override
Serializes the receiver's connection state, buffers, port, byte counts, and socket descriptors to a s...
Definition: Receiver.cpp:402
std::atomic< int > mySocketFileDescriptor
The listening socket file descriptor.
Definition: Receiver.h:36
std::function< void(const char *theReceivedMessage)> OnMessageReceived
A callback invoked with each message received from the connected client.
Definition: Receiver.h:86
unsigned int GetNumCharactersWritten() const
Gets the number of characters written in the last send.
Definition: Receiver.cpp:545
void ClearReceiveMessageBuffer()
Clears the received-message buffer, under the receive lock.
Definition: Receiver.cpp:459
std::thread myReceiveThread
The thread that reads incoming messages.
Definition: Receiver.h:40
void SendMessage(std::string message)
Queues a message to send (rejecting it if one is already pending) and wakes the send thread.
Definition: Receiver.cpp:242
std::atomic< unsigned int > myNumCharactersRead
The number of characters read in the last receive.
Definition: Receiver.h:34
std::atomic< int > myNewSocketFileDescriptor
The connected client's socket file descriptor.
Definition: Receiver.h:37
std::mutex myReceiveLock
Guards the receive buffer.
Definition: Receiver.h:42
void SendViaThread()
Send worker: waits for a queued message (or shutdown), then writes it to the client with MSG_NOSIGNAL...
Definition: Receiver.cpp:274
std::mutex mySendLock
Guards the send buffer and ready flag.
Definition: Receiver.h:41
void ReceiveViaThread()
Receive worker: reads incoming messages in a loop, forwards each to the registered callback,...
Definition: Receiver.cpp:335
virtual void ProcessRawValues() override
No-op: the receiver delivers messages via its callback, so there are no raw values to process.
Definition: Receiver.cpp:189
int GetSocketFileDescriptor() const
Gets the listening socket file descriptor.
Definition: Receiver.cpp:562
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
std::atomic< bool > myIsSendMessageReady
True when a message is queued and waiting to be sent.
Definition: Receiver.h:30
unsigned int GetPortNumber() const
Gets the port the server listens on.
Definition: Receiver.cpp:511
void ConnectToSocket()
Accepts a client connection (blocking until one arrives or the socket is shut down),...
Definition: Receiver.cpp:205
std::thread myConnectionThread
The thread that accepts a client connection.
Definition: Receiver.h:38
unsigned int myPortNumber
The port the server listens on.
Definition: Receiver.h:33
std::condition_variable mySendCondition
Signals the send thread that a message is ready (or shutdown).
Definition: Receiver.h:43
virtual ~Receiver()
Destroys the receiver, stopping its threads and closing its sockets via TurnOff.
Definition: Receiver.cpp:45
void SetPortNumber(unsigned int portNum)
Sets the port the server listens on.
Definition: Receiver.cpp:596
std::string mySendMessageBuffer
The message currently queued to send.
Definition: Receiver.h:31
OnMessageReceived OnMessageReceivedCallback
The registered message-received callback.
Definition: Receiver.h:87
std::string myReceiveMessageBuffer
The most recently received message.
Definition: Receiver.h:32
std::atomic< unsigned int > myNumCharactersWritten
The number of characters written in the last send.
Definition: Receiver.h:35
virtual void TurnOn() override
Creates, binds, and listens on the server socket, then accepts a client connection on a background th...
Definition: Receiver.cpp:63
std::thread mySendThread
The thread that sends queued messages.
Definition: Receiver.h:39
std::atomic< bool > myIsConnected
True while a client is connected and the loops should run.
Definition: Receiver.h:29
void ClearSendMessage()
Clears the queued send message and marks no message as ready, under the send lock.
Definition: Receiver.cpp:442
bool GetIsConnected() const
Gets whether a client is currently connected.
Definition: Receiver.cpp:477
int GetNewSocketFileDescriptor() const
Gets the connected client's socket file descriptor.
Definition: Receiver.cpp:579
unsigned int GetNumCharactersRead() const
Gets the number of characters read in the last receive.
Definition: Receiver.cpp:528
virtual void TurnOff() override
Stops all loops, unblocks any threads parked in accept/read/wait, joins the connection,...
Definition: Receiver.cpp:123
RumPi is the library's top-level facade.
Definition: AlertManager.h:6