C-chat
packets.h
Go to the documentation of this file.
1 
6 #ifndef C_CHAT_PACKETS_H
7 #define C_CHAT_PACKETS_H
8 
9 #include "constants.h"
10 #include "sockets.h"
11 
16 struct PacketJoin {
17  char type;
18  char username[USERNAME_MAX_LENGTH + 1];
19 };
21 extern const union Packet NewPacketJoin;
22 
27 struct PacketLeave {
28  char type;
29  char username[USERNAME_MAX_LENGTH + 1];
30 };
32 extern const union Packet NewPacketLeave;
33 
41 struct PacketText {
42  char type;
43  char message[MSG_MAX_LENGTH + 1];
44  char username[USERNAME_MAX_LENGTH + 1];
45 };
47 extern const union Packet NewPacketText;
48 
54  char type;
55  char username[USERNAME_MAX_LENGTH + 1];
56 };
58 extern const union Packet NewPacketDefineUsername;
59 
65  char type;
66  char message[MSG_MAX_LENGTH + 1];
67 };
69 extern const union Packet NewPacketServerErrorMessage;
70 
76  char type;
77  char oldUsername[USERNAME_MAX_LENGTH + 1];
78  char newUsername[USERNAME_MAX_LENGTH + 1];
79 };
81 extern const union Packet NewPacketUsernameChanged;
82 
87 struct PacketQuit {
88  char type;
89 };
91 extern const union Packet NewPacketQuit;
92 
98  char type;
99  long long fileSize;
100 };
102 extern const union Packet NewPacketFileUploadRequest;
103 
109  char type;
110  unsigned int fileId;
111 };
113 extern const union Packet NewPacketFileDownloadRequest;
114 
120  char type;
121  char accepted;
122  unsigned int id;
123 };
125 extern const union Packet NewPacketFileUploadValidation;
126 
132  char type;
133  char accepted;
134  unsigned int fileId;
135  long long fileSize;
136 };
138 extern const union Packet NewPacketFileDownloadValidation;
139 
145  char type;
146  unsigned int id;
147  char data[FILE_TRANSFER_CHUNK_SIZE];
148 };
150 extern const union Packet NewPacketFileDataTransfer;
151 
157  char type;
158  unsigned int id;
159 };
161 extern const union Packet NewPacketFileTransferCancel;
162 
168  char type;
169  char message[MSG_MAX_LENGTH +1];
170 };
172 extern const union Packet NewPacketServerSuccess;
173 
179  char type;
180  char roomName[ROOM_NAME_MAX_LENGTH + 1];
181  char roomDesc[ROOM_DESC_MAX_LENGTH + 1];
182 };
184 extern const union Packet NewPacketCreateRoom;
185 
191  char type;
192  char roomName[ROOM_NAME_MAX_LENGTH + 1];
193 };
195 extern const union Packet NewPacketJoinRoom;
196 
202  char type;
203 };
205 extern const union Packet NewPacketLeaveRoom;
206 
212  char type;
213 };
215 extern const union Packet NewPacketListRooms;
216 
221 typedef union Packet {
222  char type;
223  struct PacketJoin asJoinPacket;
224  struct PacketLeave asLeavePacket;
225  struct PacketText asTextPacket;
226  struct PacketDefineUsername asDefineUsernamePacket;
227  struct PacketServerErrorMessage asServerErrorMessagePacket;
228  struct PacketUsernameChanged asUsernameChangedPacket;
229  struct PacketFileUploadRequest asFileUploadRequestPacket;
230  struct PacketFileDownloadRequest asFileDownloadRequestPacket;
231  struct PacketFileUploadValidation asFileUploadValidationPacket;
232  struct PacketFileDownloadValidation asFileDownloadValidationPacket;
233  struct PacketFileDataTransfer asFileDataTransferPacket;
234  struct PacketFileTransferCancel asFileTransferCancelPacket;
235  struct PacketServerSuccess asServerSuccessMessagePacket;
236  struct PacketCreateRoom asCreateRoomPacket;
237  struct PacketJoinRoom asJoinRoomPacket;
238 } Packet;
239 
248 int receiveNextPacket(Socket socket, Packet* packet);
249 
258 int sendPacket(Socket socket, Packet* packet);
259 
260 #endif //C_CHAT_PACKETS_H
PacketFileUploadRequest
This packet is sent to server to ask for file upload.
Definition: packets.h:97
PacketQuit
This packet is sent to server by client to notify him he wants to quit the room.
Definition: packets.h:87
PacketText
This packet is sent to all clients of a room when a client sends a message.
Definition: packets.h:41
NewPacketServerErrorMessage
const union Packet NewPacketServerErrorMessage
Definition: packets.c:7
NewPacketText
const union Packet NewPacketText
Definition: packets.c:5
PacketLeaveRoom
This packet is sent by client to leave its room.
Definition: packets.h:201
Socket
Information of a socket.
Definition: sockets.h:19
PacketJoinRoom
This packet is sent by client to join a room.
Definition: packets.h:190
PacketLeave
This packet is sent to all client of a room when a client leaves the room.
Definition: packets.h:27
NewPacketFileDownloadRequest
const union Packet NewPacketFileDownloadRequest
Definition: packets.c:11
PacketFileDownloadRequest
This packet is sent to server to ask for file download.
Definition: packets.h:108
USERNAME_MAX_LENGTH
#define USERNAME_MAX_LENGTH
Maximum length for a client username.
Definition: constants.h:23
PacketDefineUsername
This packet is sent to server by client to re-define its username.
Definition: packets.h:53
receiveNextPacket
int receiveNextPacket(Socket socket, Packet *packet)
Receives the next packet incoming on the given socket.
Definition: packets.c:71
NewPacketQuit
const union Packet NewPacketQuit
Definition: packets.c:9
NewPacketFileTransferCancel
const union Packet NewPacketFileTransferCancel
Definition: packets.c:15
NewPacketServerSuccess
const union Packet NewPacketServerSuccess
Definition: packets.c:16
NewPacketJoin
const union Packet NewPacketJoin
Definition: packets.c:3
Packet
A generic union type for packets.
Definition: packets.h:221
NewPacketFileDataTransfer
const union Packet NewPacketFileDataTransfer
Definition: packets.c:14
PacketServerSuccess
This packet is sent by server to client on success.
Definition: packets.h:167
PacketFileTransferCancel
This packet is sent between client/server to cancel a file transfer.
Definition: packets.h:156
NewPacketDefineUsername
const union Packet NewPacketDefineUsername
Definition: packets.c:6
FILE_TRANSFER_CHUNK_SIZE
#define FILE_TRANSFER_CHUNK_SIZE
Size of data chunks for file transfer.
Definition: constants.h:29
sendPacket
int sendPacket(Socket socket, Packet *packet)
Sends the given packet on the given socket.
Definition: packets.c:95
NewPacketLeave
const union Packet NewPacketLeave
Definition: packets.c:4
PacketUsernameChanged
This packet is sent to all client of a room when an user of the room changes its username.
Definition: packets.h:75
PacketFileUploadValidation
This packet is sent to client by server to accept/reject a file upload.
Definition: packets.h:119
NewPacketLeaveRoom
const union Packet NewPacketLeaveRoom
Definition: packets.c:19
PacketListRooms
This packet is sent by client to request existing rooms list.
Definition: packets.h:211
ROOM_DESC_MAX_LENGTH
#define ROOM_DESC_MAX_LENGTH
The maximum length for the description of a room.
Definition: constants.h:54
MSG_MAX_LENGTH
#define MSG_MAX_LENGTH
Maximum message size.
Definition: constants.h:17
NewPacketFileUploadRequest
const union Packet NewPacketFileUploadRequest
Definition: packets.c:10
ROOM_NAME_MAX_LENGTH
#define ROOM_NAME_MAX_LENGTH
The maximum length for a name of a room.
Definition: constants.h:48
NewPacketFileUploadValidation
const union Packet NewPacketFileUploadValidation
Definition: packets.c:12
PacketServerErrorMessage
This packet is sent by server to an user when an error occurs.
Definition: packets.h:64
constants.h
Defines constants used on client and server (used for consistency)
sockets.h
API for sockets.
PacketFileDataTransfer
This packet is exchanged between client and server to transfer file content.
Definition: packets.h:144
NewPacketCreateRoom
const union Packet NewPacketCreateRoom
Definition: packets.c:17
NewPacketListRooms
const union Packet NewPacketListRooms
Definition: packets.c:20
PacketFileDownloadValidation
This packet is sent to client by server to accept/reject a file download.
Definition: packets.h:131
PacketJoin
This packet is sent to all client of a room when a client joins the room.
Definition: packets.h:16
NewPacketUsernameChanged
const union Packet NewPacketUsernameChanged
Definition: packets.c:8
PacketCreateRoom
This packet is sent by client to create a room.
Definition: packets.h:178
NewPacketFileDownloadValidation
const union Packet NewPacketFileDownloadValidation
Definition: packets.c:13
NewPacketJoinRoom
const union Packet NewPacketJoinRoom
Definition: packets.c:18