| Line |
Branch |
Exec |
Source |
| 1 |
|
|
/* |
| 2 |
|
|
** EPITECH PROJECT, 2024 |
| 3 |
|
|
** zappy |
| 4 |
|
|
** File description: |
| 5 |
|
|
** ServerConnect |
| 6 |
|
|
*/ |
| 7 |
|
|
|
| 8 |
|
|
#ifndef INCLUDED_SERVERCONNECT_HPP |
| 9 |
|
|
#define INCLUDED_SERVERCONNECT_HPP |
| 10 |
|
|
|
| 11 |
|
|
#include <string> |
| 12 |
|
|
#include <unistd.h> |
| 13 |
|
|
|
| 14 |
|
|
#include "Select.hpp" |
| 15 |
|
|
|
| 16 |
|
|
class ServerConnect |
| 17 |
|
|
{ |
| 18 |
|
|
private: |
| 19 |
|
|
int fd = -1; |
| 20 |
|
|
Select select = Select(); |
| 21 |
|
|
|
| 22 |
|
|
public: |
| 23 |
|
|
enum ConnectionState { |
| 24 |
|
|
NOTCONNECTED, |
| 25 |
|
|
CONNECTED, |
| 26 |
|
|
SERVERDOWN |
| 27 |
|
|
}; |
| 28 |
|
|
|
| 29 |
|
|
int port = 3000; |
| 30 |
|
|
std::string ip = "127.0.0.1"; |
| 31 |
|
|
ConnectionState connectionState = NOTCONNECTED; |
| 32 |
|
|
|
| 33 |
|
|
/** |
| 34 |
|
|
* @brief read the message sent by the server |
| 35 |
|
|
* @return the message received from the server |
| 36 |
|
|
*/ |
| 37 |
|
|
std::string readFromServer(); |
| 38 |
|
|
|
| 39 |
|
|
/** |
| 40 |
|
|
* @brief send a message to the server |
| 41 |
|
|
* @param message the message to send |
| 42 |
|
|
* @return void |
| 43 |
|
|
*/ |
| 44 |
|
|
void sendToServer(std::string message); |
| 45 |
|
|
|
| 46 |
|
|
/** |
| 47 |
|
|
* @brief connect to the given ip on the given port |
| 48 |
|
|
* @param port the port to use |
| 49 |
|
|
* @param ip the ip adress to connect to |
| 50 |
|
|
* @return void |
| 51 |
|
|
*/ |
| 52 |
|
|
void connectToServer(int port, const char *ip); |
| 53 |
|
|
|
| 54 |
|
|
/** |
| 55 |
|
|
* @brief disconect from the server |
| 56 |
|
|
* @return bool true if the disconection was successful false otherwise |
| 57 |
|
|
*/ |
| 58 |
|
|
bool disconectFromServer(); |
| 59 |
|
|
|
| 60 |
1/2
✓ Branch 2 taken 73 times.
✗ Branch 3 not taken.
|
73 |
ServerConnect() {}; |
| 61 |
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 68 times.
|
70 |
~ServerConnect() { if (this->fd >= 0) close(this->fd); }; |
| 62 |
|
|
}; |
| 63 |
|
|
|
| 64 |
|
|
#endif //SERVERCONNECT_HPP |
| 65 |
|
|
|