-
Notifications
You must be signed in to change notification settings - Fork 1
/
packet.h
48 lines (37 loc) · 933 Bytes
/
packet.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#ifndef RELIABLE_OVER_UDP_PACKET_H
#define RELIABLE_OVER_UDP_PACKET_H
#include <cstdint>
#include <type_traits>
#include <memory>
#define MAX_PACKET_SIZE (10240)
#define ROUND_UP(a, b) (((uint32_t)(a) + ((uint32_t)(b) - 1)) / (uint32_t)(b) * (uint32_t)(b))
#pragma pack(push, 1)
enum class PacketType : uint16_t {
DATA,
ACK,
SYN,
SYN_ACK,
FIN,
FIN_ACK,
};
struct Packet {
// header
PacketType type;
uint16_t checksum;
uint32_t num;
uint32_t len;
// data (if type is DATA)
uint8_t data[0];
};
static_assert(std::is_trivial_v<Packet>);
namespace PacketHelper {
bool isValidPacket(const std::unique_ptr<Packet> &packet);
std::unique_ptr<Packet> makePacket(
PacketType type,
uint32_t num = 0, /* seq or ack */
const void *data = nullptr,
uint32_t len = 0
);
}
#pragma pack(pop)
#endif //RELIABLE_OVER_UDP_PACKET_H