1 /*
2 * Copyright 2009, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
4 */
5
6 #define DEBUG_TCP_BUFFER_QUEUE 1
7
8 #include "BufferQueue.h"
9
10 #include <stdio.h>
11 #include <string.h>
12
13
14 extern "C" status_t _add_builtin_module(module_info *info);
15
16 extern struct net_buffer_module_info gNetBufferModule;
17 // from net_buffer.cpp
18
19 struct net_socket_module_info gNetSocketModule;
20 struct net_buffer_module_info* gBufferModule;
21
22 BufferQueue gQueue(32768);
23
24
25 net_buffer*
create_filled_buffer(size_t bytes)26 create_filled_buffer(size_t bytes)
27 {
28 const static uint8 data[4096] = {0};
29
30 net_buffer* buffer = gBufferModule->create(256);
31 if (buffer == NULL) {
32 printf("creating a buffer failed!\n");
33 return NULL;
34 }
35
36 status_t status = gBufferModule->append(buffer, data, bytes);
37 if (status != B_OK) {
38 printf("appending %lu bytes to buffer %p failed: %s\n", bytes, buffer,
39 strerror(status));
40 gBufferModule->free(buffer);
41 return NULL;
42 }
43
44 return buffer;
45 }
46
47
48 void
add(size_t bytes,uint32 at)49 add(size_t bytes, uint32 at)
50 {
51 gQueue.Add(create_filled_buffer(bytes), at);
52 }
53
54
55 void
eat(size_t bytes)56 eat(size_t bytes)
57 {
58 net_buffer* buffer = NULL;
59 status_t status = gQueue.Get(bytes, true, &buffer);
60 if (status == B_OK) {
61 ASSERT(buffer->size == bytes);
62 gBufferModule->free(buffer);
63 } else
64 printf("getting %lu bytes failed: %s\n", bytes, strerror(status));
65 }
66
67
68 void
dump(const char * text="")69 dump(const char* text = "")
70 {
71 printf("%s (available %lu at %lu)\n", text, gQueue.Available(),
72 (uint32)gQueue.FirstSequence());
73 gQueue.Dump();
74 }
75
76
77 int
main()78 main()
79 {
80 _add_builtin_module((module_info*)&gNetBufferModule);
81 get_module(NET_BUFFER_MODULE_NAME, (module_info**)&gBufferModule);
82 gQueue.SetInitialSequence(100);
83
84 add(100, 100);
85 add(100, 300);
86 add(100, 250);
87 add(100, 175);
88 ASSERT(gQueue.Available() == 300);
89 dump("add 4");
90
91 eat(99);
92 dump("ate 99");
93
94 eat(1);
95 eat(1);
96 eat(149);
97 eat(50);
98
99 add(10, 100);
100 add(0, 400);
101 add(1, 399);
102 dump("add nothing");
103
104 add(1, 1000);
105 dump("add far away");
106
107 add(2, 399);
108 dump("add 1");
109
110 add(100, 500);
111 add(10, 480);
112 add(19, 401);
113 add(10, 460);
114 add(10, 420);
115 add(30, 430);
116 add(35, 465);
117 dump("added with holes");
118
119 add(50, 425);
120 dump("added no new data");
121
122 eat(19);
123 eat(1);
124 eat(40);
125 eat(50);
126 dump("ate some");
127
128 add(1, 999);
129 dump("add 1");
130 add(2, 999);
131 add(2, 999);
132 dump("add 2");
133 add(3, 999);
134 dump("add 3");
135
136 add(60, 540);
137 dump("added at the end of previous data");
138
139 add(998, 1002);
140 add(500, 1000);
141 dump("added data covered by next");
142
143 put_module(NET_BUFFER_MODULE_NAME);
144 return 0;
145 }
146