GCC Code Coverage Report


Directory: ./
File: lib/packetmanager/include/packetmanager.h
Date: 2025-09-29 13:19:55
Exec Total Coverage
Lines: 8 8 100.0%
Functions: 6 6 100.0%
Branches: 0 0 -%

Line Branch Exec Source
1 /*
2 ** EPITECH PROJECT, 2025
3 ** rtype
4 ** File description:
5 ** TODO: add description
6 */
7 #ifndef PACKETMANAGER_H
8 #define PACKETMANAGER_H
9
10 #include <vector>
11 #include <memory>
12 #include "packet.h"
13
14 #define PACKET_HISTORY_SIZE 512
15
16 class PacketManager {
17 public:
18 PacketManager();
19 ~PacketManager(); // Add destructor declaration
20 static packet_t deserializePacket(const uint8_t *data, size_t size, packet_t &packet);
21 static std::unique_ptr<packet_t> deserializePacketSafe(const uint8_t *data, size_t size);
22 static std::vector<uint8_t> serializePacket(const packet_t &packet);
23 void clean();
24
25 /*
26 * Handle incoming raw packet bytes from the socket (including header in data)
27 * Parse the bytes into a packet_t struct and call handlePacket
28 * If the packet is invalid, ignore it
29 * @param data Pointer to the raw packet bytes
30 * @param size Size of the raw packet bytes
31 */
32 void handlePacketBytes(const uint8_t *data, size_t size);
33
34 /*
35 * Safer version of sendPacketBytes that returns a smart pointer to avoid memory leaks
36 * @param data Pointer to the data to be sent (will be copied)
37 * @param size Size of the data to be sent
38 * @param packet_type Type identifier for the packet
39 * @param important If true, packet gets a sequence ID and is tracked for retransmission
40 * @return Smart pointer to serialized packet data for automatic memory management
41 */
42 std::unique_ptr<uint8_t[]> sendPacketBytesSafe(const void *data, size_t data_size, uint8_t packet_type, size_t *output_size, bool important = true);
43
44 void ackMissing();
45
46 std::vector<std::unique_ptr<packet_t> > fetchReceivedPackets();
47 std::vector<std::unique_ptr<packet_t> > fetchPacketsToSend();
48
49 3 [[nodiscard]] uint32_t _get_send_seqid() const { return _send_seqid; }
50 4 [[nodiscard]] uint32_t _get_recv_seqid() const { return _recv_seqid; }
51 [[nodiscard]] uint32_t _get_auth_key() const { return _auth_key; }
52 2 [[nodiscard]] const std::vector<packet_t> *_get_history_sent() const { return &_history_sent; }
53 2 [[nodiscard]] const std::vector<uint32_t> *_get_missed_packets() const { return &_missed_packets; }
54 4 [[nodiscard]] const std::vector<std::unique_ptr<packet_t> > *_get_buffer_send() const {
55 4 return &_buffer_send;
56 }
57
58 5 [[nodiscard]] const std::vector<std::unique_ptr<packet_t> > *_get_buffer_received() const {
59 5 return &_buffer_received;
60 }
61
62 private:
63 uint32_t _send_seqid = 0;
64 uint32_t _recv_seqid = 0;
65 uint32_t _auth_key = 0;
66
67 std::vector<packet_t> _history_sent;
68 std::vector<uint32_t> _missed_packets;
69
70 std::vector<std::unique_ptr<packet_t> > _buffer_received;
71 std::vector<std::unique_ptr<packet_t> > _buffer_send;
72
73 bool _resendPacket(uint32_t seqid);
74
75 void _handlePacket(std::unique_ptr<packet_t> packet);
76 };
77
78 #endif //PACKETMANAGER_H
79