-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.h
89 lines (75 loc) · 2.35 KB
/
Client.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
* @file Client.h
* @brief This file contains the Client class which is used to connect to a server and send/receive messages.
*/
#ifndef MYLAPS_CLIENT_H
#define MYLAPS_CLIENT_H
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <cstring>
#include <iostream>
#include <future>
#include <regex>
#include <chrono>
#include <thread>
#include <mutex>
#define BUFFER_SIZE 1024
#define TIMEOUT 300
#ifdef DEBUG
#define TRACE(m) std::cout << __func__ << " " << m << std::endl;
#else
#define TRACE(m)
#endif
/**
* @class Client
* @brief This class represents a client that connects to a server and sends/receives messages.
*/
class Client {
private:
int sock = 0; ///< Socket descriptor for the client
struct sockaddr_in serv_addr{}; ///< Server address structure
std::mutex mtx; ///< Mutex for synchronizing access to the socket
public:
/**
* @brief Constructs a new Client object.
* @param serverIP The IP address of the server to connect to.
* @param port The port number of the server to connect to.
*/
Client(const std::string &serverIP, int port);
/**
* @brief Destructor for the Client object. Closes the socket connection.
*/
~Client();
/**
* @brief Connects the client to the server.
*/
void connectToServer();
/**
* @brief Sends a request to the server for the average lap time of a specific driver.
* @param driverNumber The number of the driver to request the average lap time for.
*/
void requestAverageLapTime(int driverNumber);
/**
* @brief Sends a request to the server for race data.
*/
void requestRaceData();
/**
* @brief Runs the client, allowing the user to send requests to the server and receive responses.
*/
void run();
/**
* @brief Sends raw data to the server.
* @param data The data to send.
*/
void sendRawSocket(const std::string &data);
/**
* @brief Reads raw data from the server.
* @param response The string to store the response in.
* @param responsePattern The regex pattern to match the response against.
* @param timeoutSeconds The timeout in seconds for the read operation.
*/
void readRawSocket(std::string &response, const std::regex &responsePattern, unsigned int timeoutSeconds);
};
#endif //MYLAPS_CLIENT_H