xref: /haiku/src/add-ons/kernel/network/protocols/tcp/BufferQueue.h (revision 8c78892580f132d10e624aef96f835df8d94bf19)
1 /*
2  * Copyright 2006-2010, 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 	inline	size_t				Free() const;
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 #if DEBUG_TCP_BUFFER_QUEUE
55 			void				Verify() const;
56 			void				Dump() const;
57 #endif
58 
59 private:
60 			SegmentList			fList;
61 			size_t				fMaxBytes;
62 			size_t				fNumBytes;
63 			size_t				fContiguousBytes;
64 			tcp_sequence		fFirstSequence;
65 			tcp_sequence		fLastSequence;
66 			tcp_sequence		fPushPointer;
67 };
68 
69 
70 size_t
71 BufferQueue::PushedData() const
72 {
73 	// We must check if fPushPointer is not 0 here due to
74 	// tcp_sequence's special handling of >
75 	return fPushPointer != 0 && fPushPointer > fFirstSequence
76 		? (fPushPointer - fFirstSequence).Number() : 0;
77 }
78 
79 
80 size_t
81 BufferQueue::Free() const
82 {
83 	// Max bytes is a soft limit, so it can be lower than the actual amount of
84 	// data in the queue. TCP should never advertize a window outside the max
85 	// buffer size, though.
86 	if (fMaxBytes > fNumBytes)
87 		return fMaxBytes - fNumBytes;
88 
89 	return 0;
90 }
91 
92 #endif	// BUFFER_QUEUE_H
93