Midnight Sun Firmware
Loading...
Searching...
No Matches

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
 

Detailed Description

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

Constructor & Destructor Documentation

◆ 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

Parameters
hostServer address to connect to
portPort for the client to listen on
messageCallbackFunction pointer to a message callback
connectCallbackFunction pointer to a connection callback

◆ ~Client()

~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

Member Function Documentation

◆ connectServer()

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

◆ disconnectServer()

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

◆ isConnected()

bool isConnected ( ) const

Get the connection status of the clienet.

Returns
TRUE if the client is connected FALSE if the client is disconnected

◆ processMessagesProcedure()

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

◆ receiverProcedure()

void receiverProcedure ( )

Thread procedure for caching/receiving incoming server data.

This thread shall be blocked while no new server data is available

◆ sendMessage()

void sendMessage ( const std::string &  message)

Function wrapper to transmit a message.

Parameters
messageString message value to be sent

Member Data Documentation

◆ m_clientSocket

int m_clientSocket
private

The clients socket FD

◆ m_connectCallback

connectCallback m_connectCallback
private

Function pointer to store the connection callback

◆ m_host

std::string m_host
private

The servers host address (ie: 127.0.0.1)

◆ m_isConnected

std::atomic<bool> m_isConnected { false }
private

Boolean flag to indicate the servers status

◆ m_messageCallback

messageCallback m_messageCallback
private

Function pointer to store the message callback

◆ m_messageQueue

std::queue<std::string> m_messageQueue
private

Queue to cache and input-buffer received server data

◆ m_messageSemaphore

sem_t m_messageSemaphore
private

Semaphore to signal the processMessageThread when new data is received

◆ m_mutex

pthread_mutex_t m_mutex
private

Mutex to protect m_messageQueue

◆ m_port

int m_port
private

The clients port to connect on

◆ m_processMessageThreadId

pthread_t m_processMessageThreadId
private

Thread Id for processing cached server data

◆ m_receiverThreadId

pthread_t m_receiverThreadId
private

Thread Id for reading incoming server data

◆ m_serverAddress

struct sockaddr_in m_serverAddress
private

The servers address

◆ MAX_BUFFER_SIZE

constexpr size_t MAX_BUFFER_SIZE = 256
staticconstexprprivate

Maximum permitted read size for all clients


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