RType
ClientSocket.hpp
Go to the documentation of this file.
1 /*
2 ** EPITECH PROJECT, 2023
3 ** R-type
4 ** File description:
5 ** R-type
6 */
7 
8 #ifndef R_TYPE_SERVER_CLIENTSOCKET_HPP
9 #define R_TYPE_SERVER_CLIENTSOCKET_HPP
10 
11 #include <cstring>
12 #include <mutex>
13 #include <netinet/in.h>
14 #include <string>
15 #include <thread>
16 #include <vector>
17 
18 #include "networking/shared/USocket.hpp"
19 
20 class ClientSocket : public USocket
21 {
22  public:
23  /**
24  * @brief Constructor of ClientSocket
25  */
26  ClientSocket();
27 
28  /**
29  * @brief Destructor of ClientSocket
30  */
31  ~ClientSocket() override;
32 
33  /**
34  * @brief Init the client socket
35  * @param ip
36  * @param port
37  * @return true if the client is init
38  */
39  bool init_client(const std::string &ip, int port);
40 
41  /**
42  * @brief Send a packet to the server
43  * @param packet
44  * @param dest
45  */
46  void send(Packet *packet, struct sockaddr_in dest) override;
47 
48  /**
49  * @brief Send a split packet to the server
50  * @param packet
51  * @param dest
52  */
53  void sendPacket(SplitPacket *packet, struct sockaddr_in dest) override;
54 
55  /**
56  * @brief Receive a packet from the server
57  * @return a tuple with the packet and the size of the packet
58  */
59  std::tuple<std::unique_ptr<Packet>, int> receive() override;
60 
61  /**
62  * @brief Run the client socket
63  */
64  void run() override;
65 
66  /**
67  * @brief Stop the client socket
68  */
70 
71  /**
72  * @brief Stop the client socket
73  */
74  void init_fd_set();
75 
76  /**
77  * @brief isInit, check if the client is init
78  * @return true if the client is init
79  */
80  bool isInit() const;
81 
82  /**
83  * @brief setInit, set the client init
84  * @param init
85  */
86  void setInit(bool init);
87 
88  /**
89  * @brief split and send a packet to the server
90  * @param packet
91  * @param dest
92  */
93  void splitAndSend(Packet *packet, struct sockaddr_in dest) override;
94 
95  /**
96  * @brief receiivePacketAndAddToBuffer receive a packet and add it to the buffer
97  */
99 
100  /**
101  * @brief getPacketFromBuffer get a packet from the buffer
102  * @return a packet get from the buffer
103  */
105 
106 #ifdef _WIN32
107  void read_input();
108 #endif
109 
110  struct sockaddr_in serv_addr;
111  int sockfd;
112 
113  private:
114  std::string lastMessage;
115  fd_set _readfds;
116  bool loop;
117  std::unique_ptr<struct timeval> timeout;
118  bool _isInit;
119  std::vector<std::tuple<std::unique_ptr<SplitPacket>, timeval>> _packetBuffer;
120 #ifdef _WIN32
121  std::string input;
122  std::mutex mtx;
124 #endif
125 };
126 
127 #endif // R_TYPE_SERVER_CLIENTSOCKET_HPP