-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtest_segments.cpp
More file actions
112 lines (89 loc) · 2.52 KB
/
test_segments.cpp
File metadata and controls
112 lines (89 loc) · 2.52 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include "net/mon/tcp/segments.h"
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
static void payloadfn(const void*, size_t, void*);
static void gapfn(size_t, void*);
int main()
{
struct segment {
uint32_t seqno;
const char* payload;
size_t len;
};
net::mon::tcp::segments segments;
if (segments.create(payloadfn, gapfn)) {
{
segments.next_sequence_number(0);
static constexpr const segment segs[] = {
{25, "Z0123", 5},
{15, "PQRST", 5},
{10, "KLMNO", 5},
{ 5, "FGHIJ", 5},
{ 0, "ABCDE", 5}
};
for (size_t i = 0; i < ARRAY_SIZE(segs); i++) {
if (!segments.add(segs[i].seqno, segs[i].payload, segs[i].len)) {
fprintf(stderr, "Error adding segment %zu.\n", i + 1);
return -1;
}
}
segments.fin();
printf("========================================\n");
}
{
segments.clear();
segments.next_sequence_number(UINT_MAX - 2);
static constexpr const segment segs[] = {
{ 22, "Z0123", 5},
{ 12, "PQRST", 5},
{ 7, "KLMNO", 5},
{ 2, "FGHIJ", 5},
{UINT_MAX - 2, "ABCDE", 5}
};
for (size_t i = 0; i < ARRAY_SIZE(segs); i++) {
if (!segments.add(segs[i].seqno, segs[i].payload, segs[i].len)) {
fprintf(stderr, "Error adding segment %zu.\n", i + 1);
return -1;
}
}
segments.fin();
printf("========================================\n");
}
{
segments.clear();
segments.next_sequence_number(UINT_MAX - 2);
static constexpr const segment segs[] = {
{UINT_MAX - 2, "ABCDE", 5},
{ 2, "FGHIJ", 5},
{ 7, "KLMNO", 5},
{ 12, "PQRST", 5},
{ 22, "Z0123", 5}
};
for (size_t i = 0; i < ARRAY_SIZE(segs); i++) {
if (!segments.add(segs[i].seqno, segs[i].payload, segs[i].len)) {
fprintf(stderr, "Error adding segment %zu.\n", i + 1);
return -1;
}
}
segments.fin();
}
return 0;
} else {
fprintf(stderr, "Error creating segments.\n");
}
return -1;
}
void payloadfn(const void* payload, size_t len, void* user)
{
printf("[Payload] Length: %zu, ", len);
for (size_t i = 0; i < len; i++) {
printf("%c", static_cast<const char*>(payload)[i]);
}
printf("\n");
}
void gapfn(size_t len, void* user)
{
printf("[Gap] Length: %zu\n", len);
}