1 /* 2 * Copyright 2006, 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, DoublyLinkedListCLink<struct net_buffer> > SegmentList; 18 19 class BufferQueue { 20 public: 21 BufferQueue(size_t maxBytes); 22 ~BufferQueue(); 23 24 void SetMaxBytes(size_t maxBytes); 25 void SetInitialSequence(tcp_sequence sequence); 26 27 void Add(net_buffer *buffer); 28 void Add(net_buffer *buffer, tcp_sequence sequence); 29 status_t RemoveUntil(tcp_sequence sequence); 30 status_t Get(net_buffer *buffer, tcp_sequence sequence, size_t bytes); 31 status_t Get(size_t bytes, bool remove, net_buffer **_buffer); 32 33 size_t Available() const { return fContiguousBytes; } 34 size_t Available(tcp_sequence sequence) const; 35 36 size_t PushedData() const 37 { 38 // we must check if fPushPointer is not 0 here due to 39 // `tcp_sequence's special handling of > 40 return fPushPointer != 0 && fPushPointer > fFirstSequence ? 41 fPushPointer - fFirstSequence : 0; 42 } 43 44 void SetPushPointer(); 45 46 size_t Used() const { return fNumBytes; } 47 size_t Free() const { return fMaxBytes - fNumBytes; } 48 size_t Size() const { return fMaxBytes; } 49 50 bool IsContiguous() const { return fNumBytes == fContiguousBytes; } 51 52 tcp_sequence FirstSequence() const { return fFirstSequence; } 53 tcp_sequence LastSequence() const { return fLastSequence; } 54 tcp_sequence NextSequence() const { return fFirstSequence + fContiguousBytes; } 55 56 private: 57 SegmentList fList; 58 size_t fMaxBytes; 59 size_t fNumBytes; 60 size_t fContiguousBytes; 61 tcp_sequence fFirstSequence; 62 tcp_sequence fLastSequence; 63 tcp_sequence fPushPointer; 64 }; 65 66 #endif // BUFFER_QUEUE_H 67