xref: /haiku/src/add-ons/kernel/network/protocols/tcp/BufferQueue.h (revision 746cac055adc6ac3308c7bc2d29040fb95689cc9)
1 /*
2  * Copyright 2006-2008, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Axel Dörfler, axeld@pinc-software.de
7  */
8 #ifndef BUFFER_QUEUE_H
9 #define BUFFER_QUEUE_H
10 
11 
12 #include "tcp.h"
13 
14 #include <util/DoublyLinkedList.h>
15 
16 
17 typedef DoublyLinkedList<struct net_buffer,
18 	DoublyLinkedListCLink<struct net_buffer> > SegmentList;
19 
20 class BufferQueue {
21 public:
22 							BufferQueue(size_t maxBytes);
23 							~BufferQueue();
24 
25 			void			SetMaxBytes(size_t maxBytes);
26 			void			SetInitialSequence(tcp_sequence sequence);
27 
28 			void			Add(net_buffer* buffer);
29 			void			Add(net_buffer* buffer, tcp_sequence sequence);
30 			status_t		RemoveUntil(tcp_sequence sequence);
31 			status_t		Get(net_buffer* buffer, tcp_sequence sequence,
32 								size_t bytes);
33 			status_t		Get(size_t bytes, bool remove,
34 								net_buffer** _buffer);
35 
36 			size_t			Available() const { return fContiguousBytes; }
37 			size_t			Available(tcp_sequence sequence) const;
38 
39 	inline	size_t			PushedData() const;
40 			void			SetPushPointer();
41 
42 			size_t			Used() const { return fNumBytes; }
43 			size_t			Free() const { return fMaxBytes - fNumBytes; }
44 			size_t			Size() const { return fMaxBytes; }
45 
46 			bool			IsContiguous() const
47 								{ return fNumBytes == fContiguousBytes; }
48 
49 			tcp_sequence	FirstSequence() const { return fFirstSequence; }
50 			tcp_sequence	LastSequence() const { return fLastSequence; }
51 			tcp_sequence	NextSequence() const
52 								{ return fFirstSequence + fContiguousBytes; }
53 
54 private:
55 	SegmentList				fList;
56 	size_t					fMaxBytes;
57 	size_t					fNumBytes;
58 	size_t					fContiguousBytes;
59 	tcp_sequence			fFirstSequence;
60 	tcp_sequence			fLastSequence;
61 	tcp_sequence			fPushPointer;
62 };
63 
64 
65 size_t
66 BufferQueue::PushedData() const
67 {
68 	// we must check if fPushPointer is not 0 here due to
69 	// tcp_sequence's special handling of >
70 	return fPushPointer != 0
71 		&& fPushPointer > fFirstSequence ? fPushPointer - fFirstSequence : 0;
72 }
73 
74 #endif	// BUFFER_QUEUE_H
75