31
|
1 |
#ifndef NETWORK_PACKET_HH
|
|
2 |
#define NETWORK_PACKET_HH
|
|
3 |
|
|
4 |
#include "NetworkConfig.hh"
|
36
|
5 |
#include "Vector.hh"
|
|
6 |
#include "Error.hh"
|
|
7 |
|
|
8 |
class NetworkPacketError : public Error {
|
|
9 |
public:
|
|
10 |
NetworkPacketError (const std::string &message) : Error(message) { }
|
|
11 |
};
|
31
|
12 |
|
|
13 |
class NetworkPacket {
|
|
14 |
private:
|
|
15 |
char buf[NETWORK_PACKET_SIZE];
|
33
|
16 |
size_t buf_size, data_size, offset;
|
|
17 |
|
|
18 |
void check_write_size (size_t item_size);
|
|
19 |
void check_read_size (size_t item_size);
|
31
|
20 |
|
|
21 |
template <typename T> T read_val (void);
|
|
22 |
template <typename T> void write_val (const T &val);
|
|
23 |
|
|
24 |
public:
|
|
25 |
NetworkPacket (void);
|
|
26 |
|
|
27 |
char* get_buf (void) { return buf; }
|
33
|
28 |
const char* get_buf (void) const { return buf; }
|
|
29 |
size_t get_data_size (void) const { return data_size; }
|
|
30 |
size_t get_buf_size (void) const { return buf_size; }
|
32
|
31 |
|
33
|
32 |
void set_data_size (size_t size) { offset = 0; data_size = size; }
|
40
|
33 |
|
|
34 |
// raw
|
|
35 |
void write (const void *ptr, size_t len);
|
|
36 |
void read (void *ptr, size_t len);
|
31
|
37 |
|
|
38 |
// type-reads, handle network-endianlness
|
36
|
39 |
uint32_t read_uint32 (void);
|
|
40 |
uint16_t read_uint16 (void);
|
|
41 |
uint8_t read_uint8 (void);
|
31
|
42 |
|
36
|
43 |
int32_t read_int32 (void);
|
|
44 |
int16_t read_int16 (void);
|
|
45 |
int8_t read_int8 (void);
|
|
46 |
|
|
47 |
float read_float32 (void);
|
|
48 |
|
31
|
49 |
|
|
50 |
void write_uint32 (uint32_t val);
|
|
51 |
void write_uint16 (uint16_t val);
|
|
52 |
void write_uint8 (uint8_t val);
|
|
53 |
|
|
54 |
void write_int32 (int32_t val);
|
|
55 |
void write_int16 (int16_t val);
|
|
56 |
void write_int8 (int8_t val);
|
36
|
57 |
|
|
58 |
void write_float32 (float val);
|
31
|
59 |
|
40
|
60 |
// complex
|
|
61 |
Vector read_vector (void);
|
36
|
62 |
void write_vector (const Vector &vec);
|
33
|
63 |
void write_packet (const NetworkPacket &pkt);
|
31
|
64 |
};
|
|
65 |
|
|
66 |
#endif /* NETWORK_PACKET_HH */
|