Midnight Sun Firmware
Loading...
Searching...
No Matches
client.h
1#pragma once
2
3/************************************************************************************************
4 * @file client.h
5 *
6 * @brief Header file defining the Client class
7 *
8 * @date 2025-01-04
9 * @author Aryan Kashem
10 ************************************************************************************************/
11
12/* Standard library Headers */
13#include <atomic>
14#include <functional>
15#include <queue>
16#include <string>
17
18/* Inter-component Headers */
19#include <arpa/inet.h>
20#include <fcntl.h>
21#include <pthread.h>
22#include <semaphore.h>
23#include <sys/socket.h>
24#include <unistd.h>
25
26/* Intra-component Headers */
27
40class Client {
41 private:
43 using messageCallback = std::function<void(Client *client, std::string &)>;
45 using connectCallback = std::function<void(Client *client)>;
46
47 static constexpr size_t MAX_BUFFER_SIZE = 256;
51 pthread_mutex_t m_mutex;
57 std::atomic<bool> m_isConnected{ false };
59 std::queue<std::string> m_messageQueue;
62 std::string m_host;
63 int m_port;
64 struct sockaddr_in m_serverAddress;
66 public:
77 Client(const std::string &host, int port, messageCallback messageCallback, connectCallback connectCallback);
78
84 ~Client();
85
90 void receiverProcedure();
91
98
104 void connectServer();
105
111 void disconnectServer();
112
118 bool isConnected() const;
119
124 void sendMessage(const std::string &message);
125};
126
Class that represents a client that connects to a main server.
Definition: client.h:40
messageCallback m_messageCallback
Definition: client.h:54
std::atomic< bool > m_isConnected
Definition: client.h:57
std::queue< std::string > m_messageQueue
Definition: client.h:59
void receiverProcedure()
Thread procedure for caching/receiving incoming server data.
Definition: client.cc:38
~Client()
Destructs a Client object.
Definition: client.cc:154
struct sockaddr_in m_serverAddress
Definition: client.h:64
std::function< void(Client *client, std::string &)> messageCallback
The message callback function definition.
Definition: client.h:43
pthread_t m_receiverThreadId
Definition: client.h:49
int m_port
Definition: client.h:63
std::string m_host
Definition: client.h:62
bool isConnected() const
Get the connection status of the clienet.
Definition: client.cc:134
void sendMessage(const std::string &message)
Function wrapper to transmit a message.
Definition: client.cc:129
pthread_t m_processMessageThreadId
Definition: client.h:50
void connectServer()
Connect to the server.
Definition: client.cc:83
int m_clientSocket
Definition: client.h:61
connectCallback m_connectCallback
Definition: client.h:55
sem_t m_messageSemaphore
Definition: client.h:52
void disconnectServer()
Disconnect from the server.
Definition: client.cc:123
std::function< void(Client *client)> connectCallback
The connection callback function definition.
Definition: client.h:45
static constexpr size_t MAX_BUFFER_SIZE
Definition: client.h:47
pthread_mutex_t m_mutex
Definition: client.h:51
void processMessagesProcedure()
Thread procedure for processing cached/stored server data.
Definition: client.cc:19