RType
USocket.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_USOCKET_HPP
9 #define R_TYPE_SERVER_USOCKET_HPP
10 
11 #include <cstdint>
12 #include <memory>
13 #include <string>
14 #include <sys/time.h>
15 
16 #ifdef _WIN32
17 #include <winsock2.h>
18 #include <ws2tcpip.h>
19 
20 #define STDIN_FILENO 0
21 #elif defined(__unix__) || defined(__linux__)
22 #include <SFML/Window/Event.hpp>
23 #include <arpa/inet.h>
24 #include <netinet/in.h>
25 #include <sys/socket.h>
26 #include <unistd.h>
27 
28 #endif
29 
30 /**
31  * @brief Enum of all the code of the packet
32  */
33 typedef enum code {
43 } CODE;
44 
45 /**
46  * @brief Enum of all the type of the component
47  */
48 typedef enum {
56 } ComponentTypeSocket;
57 
58 
59 typedef struct key {
60  int key;
61  int type;
62 } Key;
63 
64 
65 typedef struct mouse {
66  int x;
67  int y;
68  int type;
69 } Mouse;
70 
71 typedef struct event {
72  int eventType;
73  int key;
74  int id;
75 } Event;
76 
77 typedef struct packet {
78  CODE code;
79  int data_size;
80  void *data;
81 } Packet;
82 
83 typedef union attribute {
84  char data;
86 } attribute_t;
87 
88 typedef struct drawable {
89  int id;
90 
91  float x;
92  float y;
94  float sizeVertical;
95  int rectLeft;
96  int rectTop;
97  int rectWidth;
101 } DrawablePacket;
102 
103 typedef struct newComponent {
104  int id;
105  float x;
106  float y;
109  int rectLeft;
110  int rectTop;
115  ComponentTypeSocket type;
116 } NewComponent;
117 
118 // obsolete
119 typedef enum type {
132 } Type;
133 
134 // obsolete
135 typedef struct element {
136  int id;
137  float x;
138  float y;
139  int width;
140  int height;
141  Type type;
142 } Element;
143 
144 typedef struct split_packet {
145  CODE code;
148  char data[1024];
149 } SplitPacket;
150 
151 class USocket
152 {
153  public:
154 
155  /**
156  * @brief Destructor of USocket
157  */
158  virtual ~USocket() = default;
159 
160  /**
161  * @brief Send a packet to a client
162  * @param packet
163  * @param dest
164  */
165  virtual void send(Packet *packet, struct sockaddr_in dest) = 0;
166 
167  /**
168  * @brief Receive a packet from a client
169  * @return a tuple with the packet and the size of the packet
170  */
171  virtual std::tuple<std::unique_ptr<Packet>, int> receive() = 0;
172 
173  /**
174  * @brief Send a split packet to a client
175  * @param packet
176  * @param dest
177  */
178  virtual void splitAndSend(Packet *packet, struct sockaddr_in dest) = 0;
179 
180  /**
181  * @brief Run the server socket
182  */
183  virtual void run() = 0;
184 
185  /**
186  * @brief Send a split packet to a client
187  * @param packet
188  * @param dest
189  */
190  virtual void sendPacket(SplitPacket *packet, struct sockaddr_in dest) = 0;
191 };
192 
193 #endif // R_TYPE_SERVER_USOCKET_HPP