Midnight Sun Firmware
|
Class that represents a client that connects to a main server. More...
#include <client.h>
Public Member Functions | |
Client (const std::string &host, int port, messageCallback messageCallback, connectCallback connectCallback) | |
Constructs a Client object. More... | |
~Client () | |
Destructs a Client object. More... | |
void | receiverProcedure () |
Thread procedure for caching/receiving incoming server data. More... | |
void | processMessagesProcedure () |
Thread procedure for processing cached/stored server data. More... | |
void | connectServer () |
Connect to the server. More... | |
void | disconnectServer () |
Disconnect from the server. More... | |
bool | isConnected () const |
Get the connection status of the clienet. More... | |
void | sendMessage (const std::string &message) |
Function wrapper to transmit a message. More... | |
Private Types | |
using | messageCallback = std::function< void(Client *client, std::string &)> |
The message callback function definition. | |
using | connectCallback = std::function< void(Client *client)> |
The connection callback function definition. | |
Private Attributes | |
pthread_t | m_receiverThreadId |
pthread_t | m_processMessageThreadId |
pthread_mutex_t | m_mutex |
sem_t | m_messageSemaphore |
messageCallback | m_messageCallback |
connectCallback | m_connectCallback |
std::atomic< bool > | m_isConnected { false } |
std::queue< std::string > | m_messageQueue |
int | m_clientSocket |
std::string | m_host |
int | m_port |
struct sockaddr_in | m_serverAddress |
Static Private Attributes | |
static constexpr size_t | MAX_BUFFER_SIZE = 256 |
Class that represents a client that connects to a main server.
This class is responsible for managing the client connection, binding to the server, monitoring server activity, and sending messages between the server and the client
Client | ( | const std::string & | host, |
int | port, | ||
messageCallback | messageCallback, | ||
connectCallback | connectCallback | ||
) |
Constructs a Client object.
Initializes the client. The constructor sets up internal variables and prepares the client to accept and communicate This shall set the host address, listening port and callback functions
host | Server address to connect to |
port | Port for the client to listen on |
messageCallback | Function pointer to a message callback |
connectCallback | Function pointer to a connection callback |
~Client | ( | ) |
Destructs a Client object.
If using TCP this closes the existing socket connection This shall also delete the mutex, and terminate both running threads
void connectServer | ( | ) |
Connect to the server.
This shall throw an exception if the server does not exist or is not accepting clients Upon connection, this shall spawn the receiverProcedure and processMessages threads
void disconnectServer | ( | ) |
Disconnect from the server.
If the server is connected, this shall safely terminate both receiverProcedure and processMessages threads This shall close the existing socket connection
bool isConnected | ( | ) | const |
Get the connection status of the clienet.
void processMessagesProcedure | ( | ) |
Thread procedure for processing cached/stored server data.
This thread shall be blocked while no new server data is available in storage Upon receiving a message, the message callback shall be called
void receiverProcedure | ( | ) |
Thread procedure for caching/receiving incoming server data.
This thread shall be blocked while no new server data is available
void sendMessage | ( | const std::string & | message | ) |
Function wrapper to transmit a message.
message | String message value to be sent |
|
private |
The clients socket FD
|
private |
Function pointer to store the connection callback
|
private |
The servers host address (ie: 127.0.0.1)
|
private |
Boolean flag to indicate the servers status
|
private |
Function pointer to store the message callback
|
private |
Queue to cache and input-buffer received server data
|
private |
Semaphore to signal the processMessageThread when new data is received
|
private |
Mutex to protect m_messageQueue
|
private |
The clients port to connect on
|
private |
Thread Id for processing cached server data
|
private |
Thread Id for reading incoming server data
|
private |
The servers address
|
staticconstexprprivate |
Maximum permitted read size for all clients