INET Framework for OMNeT++/OMNEST
inet::ChunkQueue Class Reference

This class represents a first in, first out queue of chunks. More...

#include <ChunkQueue.h>

Inheritance diagram for inet::ChunkQueue:

Public Member Functions

Constructors, destructors and duplication related functions
 ChunkQueue (const char *name=nullptr, const Ptr< const Chunk > &content=EmptyChunk::singleton)
 
 ChunkQueue (const ChunkQueue &other)
 
virtual ChunkQueuedup () const override
 
Length querying related functions
b getLength () const
 Returns the total length of data currently available in the queue. More...
 
b getPushedLength () const
 Returns the total length of data pushed into the queue so far. More...
 
b getPoppedLength () const
 Returns the total length of data popped from the queue so far. More...
 
Querying data related functions
const Ptr< const Chunkpeek (b length=b(-1), int flags=0) const
 Returns the designated data from the head of the queue as an immutable chunk in its current representation. More...
 
const Ptr< const ChunkpeekAt (b offset, b length, int flags=0) const
 Returns the designated data at the given offset as an immutable chunk in its current representation. More...
 
template<typename T >
bool has (b length=b(-1)) const
 Returns true if the designated data is available at the head of the queue in the requested representation. More...
 
template<typename T >
const Ptr< const Tpeek (b length=b(-1), int flags=0) const
 Returns the designated data from the head of the queue as an immutable chunk in the requested representation. More...
 
template<typename T >
const Ptr< const TpeekAt (b offset, b length=b(-1), int flags=0) const
 Returns the designated data at the given offset as an immutable chunk in the requested representation. More...
 
const Ptr< const ChunkpeekAll (int flags=0) const
 Returns all data in the queue in the current representation. More...
 
const Ptr< const BitsChunkpeekAllAsBits (int flags=0) const
 Returns all data in the queue in the as a sequence of bits. More...
 
const Ptr< const BytesChunkpeekAllAsBytes (int flags=0) const
 Returns all data in the queue in the as a sequence of bytes. More...
 
Removing data related functions
const Ptr< const Chunkpop (b length=b(-1), int flags=0)
 Pops the designated data and returns it as an immutable chunk in its current representation. More...
 
template<typename T >
const Ptr< const Tpop (b length=b(-1), int flags=0)
 Pops the designated data from the head of the queue and returns it as an immutable chunk in the requested representation. More...
 
void clear ()
 Erases all data from the queue. More...
 
Filling with data related functions
void push (const Ptr< const Chunk > &chunk)
 Inserts the provided chunk at the tail of the queue. More...
 
virtual std::string str () const override
 Returns a human readable string representation. More...
 

Protected Member Functions

const ChunkgetContent () const
 
bool isIteratorConsistent (const Chunk::Iterator &iterator)
 
void remove (b length)
 
void moveIteratorOrRemove (b length)
 

Protected Attributes

b pushedLength = b(0)
 
b poppedLength = b(0)
 
Ptr< const Chunkcontent
 This chunk is always immutable to allow arbitrary peeking. More...
 
Chunk::Iterator iterator
 

Friends

class ChunkQueueDescriptor
 

Detailed Description

This class represents a first in, first out queue of chunks.

It is mainly useful for application and protocol buffers where the incoming chunks are guaranteed to be in order.

Internally, queues stores the data in different kind of chunks. See the Chunk class and its subclasses for details. All chunks are immutable in a queue. Chunks are automatically merged as they are pushed into the queue, and they are also shared among queues when duplicating.

In general, this class supports the following operations:

  • push at the tail and pop at the head
  • query the length
  • serialize to and deserialize from a sequence of bits or bytes
  • copy to a new queue
  • convert to a human readable string

Constructor & Destructor Documentation

◆ ChunkQueue() [1/2]

inet::ChunkQueue::ChunkQueue ( const char *  name = nullptr,
const Ptr< const Chunk > &  content = EmptyChunk::singleton 
)
14  :
15  cNamedObject(name),
17  iterator(Chunk::ForwardIterator(b(0), 0))
18 {
19  constPtrCast<Chunk>(content)->markImmutable();
20 }

◆ ChunkQueue() [2/2]

inet::ChunkQueue::ChunkQueue ( const ChunkQueue other)
22  :
23  cNamedObject(other),
24  content(other.content),
25  iterator(other.iterator)
26 {
27  CHUNK_CHECK_IMPLEMENTATION(content->isImmutable());
28 }

Member Function Documentation

◆ clear()

void inet::ChunkQueue::clear ( )

◆ dup()

virtual ChunkQueue* inet::ChunkQueue::dup ( ) const
inlineoverridevirtual
66 { return new ChunkQueue(*this); }

◆ getContent()

const Chunk* inet::ChunkQueue::getContent ( ) const
inlineprotected
49 { return content.get(); } // only for class descriptor

◆ getLength()

◆ getPoppedLength()

b inet::ChunkQueue::getPoppedLength ( ) const
inline

Returns the total length of data popped from the queue so far.

84 { return poppedLength; }

◆ getPushedLength()

b inet::ChunkQueue::getPushedLength ( ) const
inline

Returns the total length of data pushed into the queue so far.

79 { return pushedLength; }

◆ has()

template<typename T >
bool inet::ChunkQueue::has ( b  length = b(-1)) const
inline

Returns true if the designated data is available at the head of the queue in the requested representation.

If the length is unspecified, then the length of the result is chosen according to the internal representation.

109  {
110  return content->has<T>(iterator, length == b(-1) ? Chunk::unspecifiedLength : length);
111  }

◆ isIteratorConsistent()

bool inet::ChunkQueue::isIteratorConsistent ( const Chunk::Iterator iterator)
inlineprotected
51  {
52  Chunk::Iterator copy(iterator);
53  content->seekIterator(copy, iterator.getPosition());
54  return iterator.getPosition() == copy.getPosition() && (iterator.getIndex() == -1 || iterator.getIndex() == copy.getIndex());
55  }

Referenced by moveIteratorOrRemove(), push(), and remove().

◆ moveIteratorOrRemove()

void inet::ChunkQueue::moveIteratorOrRemove ( b  length)
protected
48 {
49  poppedLength += length;
50  content->moveIterator(iterator, length);
51  if (iterator.getPosition() > content->getChunkLength() / 2)
54 }

Referenced by pop().

◆ peek() [1/2]

const Ptr< const Chunk > inet::ChunkQueue::peek ( b  length = b(-1),
int  flags = 0 
) const

Returns the designated data from the head of the queue as an immutable chunk in its current representation.

If the length is unspecified, then the length of the result is chosen according to the internal representation.

57 {
58  CHUNK_CHECK_USAGE(b(-1) <= length && length <= getLength(), "length is invalid");
59  return content->peek(iterator, length == b(-1) ? Chunk::unspecifiedLength : length, flags);
60 }

Referenced by inet::tcp::TcpLwipSendQueue::getBytesForTcpLayer(), and pop().

◆ peek() [2/2]

template<typename T >
const Ptr<const T> inet::ChunkQueue::peek ( b  length = b(-1),
int  flags = 0 
) const
inline

Returns the designated data from the head of the queue as an immutable chunk in the requested representation.

If the length is unspecified, then the length of the result is chosen according to the internal representation.

119  {
120  return content->peek<T>(iterator, length == b(-1) ? Chunk::unspecifiedLength : length, flags);
121  }

◆ peekAll()

const Ptr<const Chunk> inet::ChunkQueue::peekAll ( int  flags = 0) const
inline

Returns all data in the queue in the current representation.

The length of the returned chunk is the same as the value returned by getLength().

139  {
140  return peekAt(b(0), getLength(), flags);
141  }

◆ peekAllAsBits()

const Ptr<const BitsChunk> inet::ChunkQueue::peekAllAsBits ( int  flags = 0) const
inline

Returns all data in the queue in the as a sequence of bits.

The length of the returned chunk is the same as the value returned by getLength().

147  {
148  return peekAt<BitsChunk>(b(0), getLength(), flags);
149  }

◆ peekAllAsBytes()

const Ptr<const BytesChunk> inet::ChunkQueue::peekAllAsBytes ( int  flags = 0) const
inline

Returns all data in the queue in the as a sequence of bytes.

The length of the returned chunk is the same as the value returned by getLength().

155  {
156  return peekAt<BytesChunk>(b(0), getLength(), flags);
157  }

◆ peekAt() [1/2]

const Ptr< const Chunk > inet::ChunkQueue::peekAt ( b  offset,
b  length,
int  flags = 0 
) const

Returns the designated data at the given offset as an immutable chunk in its current representation.

If the length is unspecified, then the length of the result is chosen according to the internal representation.

63 {
64  CHUNK_CHECK_USAGE(b(0) <= offset && offset <= getLength(), "offset is out of range");
65  CHUNK_CHECK_USAGE(b(-1) <= length && offset + length <= getLength(), "length is invalid");
66  return content->peek(Chunk::Iterator(true, iterator.getPosition() + offset, -1), length == b(-1) ? Chunk::unspecifiedLength : length, flags);
67 }

Referenced by inet::tcp::TcpSendQueue::createSegmentWithBytes().

◆ peekAt() [2/2]

template<typename T >
const Ptr<const T> inet::ChunkQueue::peekAt ( b  offset,
b  length = b(-1),
int  flags = 0 
) const
inline

Returns the designated data at the given offset as an immutable chunk in the requested representation.

If the length is unspecified, then the length of the result is chosen according to the internal representation.

129  {
130  CHUNK_CHECK_USAGE(b(0) <= offset && offset <= getLength(), "offset is out of range");
131  CHUNK_CHECK_USAGE(b(-1) <= length && offset + length <= getLength(), "length is invalid");
132  return content->peek<T>(Chunk::Iterator(true, iterator.getPosition() + offset, -1), length == b(-1) ? Chunk::unspecifiedLength : length, flags);
133  }

◆ pop() [1/2]

const Ptr< const Chunk > inet::ChunkQueue::pop ( b  length = b(-1),
int  flags = 0 
)

Pops the designated data and returns it as an immutable chunk in its current representation.

If the length is unspecified, then the length of the result is chosen according to the internal representation.

70 {
71  CHUNK_CHECK_USAGE(b(-1) <= length && length <= getLength(), "length is invalid");
72  const auto& chunk = peek(length == b(-1) ? Chunk::unspecifiedLength : length, flags);
73  if (chunk != nullptr)
74  moveIteratorOrRemove(chunk->getChunkLength());
75  return chunk;
76 }

Referenced by inet::tcp::TcpLwipSendQueue::dequeueTcpLayerMsg(), inet::tcp::TcpSendQueue::discardUpTo(), inet::tcp::TcpLwipReceiveQueue::extractBytesUpTo(), inet::TcpGenericServerApp::handleMessage(), inet::bgp::BgpRouter::socketDataArrived(), and inet::Ldp::socketDataArrived().

◆ pop() [2/2]

template<typename T >
const Ptr<const T> inet::ChunkQueue::pop ( b  length = b(-1),
int  flags = 0 
)
inline

Pops the designated data from the head of the queue and returns it as an immutable chunk in the requested representation.

If the length is unspecified, then the length of the result is chosen according to the internal representation.

176  {
177  const auto& chunk = peek<T>(length == b(-1) ? Chunk::unspecifiedLength : length, flags);
178  if (chunk != nullptr)
179  moveIteratorOrRemove(chunk->getChunkLength());
180  return chunk;
181  }

◆ push()

void inet::ChunkQueue::push ( const Ptr< const Chunk > &  chunk)

Inserts the provided chunk at the tail of the queue.

86 {
87  CHUNK_CHECK_USAGE(chunk != nullptr, "chunk is nullptr");
88  CHUNK_CHECK_USAGE(chunk->getChunkLength() > b(0), "chunk is empty");
89  constPtrCast<Chunk>(chunk)->markImmutable();
90  pushedLength += chunk->getChunkLength();
92  content = chunk;
93  else {
94  if (content->canInsertAtBack(chunk)) {
95  const auto& newContent = makeExclusivelyOwnedMutableChunk(content);
96  newContent->insertAtBack(chunk);
97  newContent->markImmutable();
98  content = newContent->simplify();
99  }
100  else {
101  auto sequenceChunk = makeShared<SequenceChunk>();
102  sequenceChunk->insertAtBack(content);
103  sequenceChunk->insertAtBack(chunk);
104  sequenceChunk->markImmutable();
105  content = sequenceChunk;
106  content->seekIterator(iterator, iterator.getPosition());
107  }
108  }
110 }

Referenced by inet::tcp::TcpSendQueue::enqueueAppData(), inet::tcp::TcpLwipSendQueue::enqueueAppData(), inet::tcp::TcpLwipReceiveQueue::enqueueTcpLayerData(), inet::TcpGenericServerApp::handleMessage(), and inet::TcpSocket::ReceiveQueueBasedCallback::socketDataArrived().

◆ remove()

void inet::ChunkQueue::remove ( b  length)
protected
31 {
32  CHUNK_CHECK_IMPLEMENTATION(b(0) <= length && length <= iterator.getPosition());
33  if (content->getChunkLength() == length)
35  else if (content->canRemoveAtFront(length)) {
36  const auto& newContent = makeExclusivelyOwnedMutableChunk(content);
37  newContent->removeAtFront(length);
38  newContent->markImmutable();
39  content = newContent;
40  }
41  else
42  content = content->peek(length, content->getChunkLength() - length);
43  content->seekIterator(iterator, iterator.getPosition() - length);
45 }

Referenced by moveIteratorOrRemove().

◆ str()

std::string inet::ChunkQueue::str ( ) const
overridevirtual

Returns a human readable string representation.

113 {
114  std::stringstream stream;
115  if (iterator.getPosition() == b(0))
116  stream << content;
117  else
118  stream << content->peek(iterator);
119  return stream.str();
120 }

Referenced by inet::operator<<(), and inet::tcp::TcpSendQueue::str().

Friends And Related Function Documentation

◆ ChunkQueueDescriptor

friend class ChunkQueueDescriptor
friend

Member Data Documentation

◆ content

Ptr<const Chunk> inet::ChunkQueue::content
protected

This chunk is always immutable to allow arbitrary peeking.

Nevertheless it's reused if possible to allow efficient merging with newly added chunks.

Referenced by ChunkQueue(), clear(), moveIteratorOrRemove(), peek(), peekAt(), push(), remove(), and str().

◆ iterator

Chunk::Iterator inet::ChunkQueue::iterator
protected

◆ poppedLength

b inet::ChunkQueue::poppedLength = b(0)
protected

Referenced by clear(), and moveIteratorOrRemove().

◆ pushedLength

b inet::ChunkQueue::pushedLength = b(0)
protected

Referenced by push().


The documentation for this class was generated from the following files:
inet::units::units::T
compose< Wb, pow< m, -2 > > T
Definition: Units.h:951
inet::ChunkQueue::iterator
Chunk::Iterator iterator
Definition: ChunkQueue.h:46
CHUNK_CHECK_IMPLEMENTATION
#define CHUNK_CHECK_IMPLEMENTATION(condition)
Definition: Chunk.h:29
inet::makeExclusivelyOwnedMutableChunk
const Ptr< T > makeExclusivelyOwnedMutableChunk(const Ptr< const T > &chunk)
Definition: Chunk.h:860
inet::ChunkQueue::remove
void remove(b length)
Definition: ChunkQueue.cc:30
inet::ChunkQueue::moveIteratorOrRemove
void moveIteratorOrRemove(b length)
Definition: ChunkQueue.cc:47
inet::ChunkQueue::ChunkQueue
ChunkQueue(const char *name=nullptr, const Ptr< const Chunk > &content=EmptyChunk::singleton)
Definition: ChunkQueue.cc:14
inet::ChunkQueue::poppedLength
b poppedLength
Definition: ChunkQueue.h:40
inet::EmptyChunk::singleton
static const Ptr< EmptyChunk > singleton
Definition: EmptyChunk.h:23
CHUNK_CHECK_USAGE
#define CHUNK_CHECK_USAGE(condition, format,...)
Definition: Chunk.h:42
inet::Chunk::Iterator::getPosition
b getPosition() const
Definition: Chunk.h:306
inet::units::values::b
value< int64_t, units::b > b
Definition: Units.h:1241
inet::ChunkQueue::isIteratorConsistent
bool isIteratorConsistent(const Chunk::Iterator &iterator)
Definition: ChunkQueue.h:51
inet::ChunkQueue::getLength
b getLength() const
Returns the total length of data currently available in the queue.
Definition: ChunkQueue.h:74
inet::ChunkQueue::content
Ptr< const Chunk > content
This chunk is always immutable to allow arbitrary peeking.
Definition: ChunkQueue.h:45
inet::Chunk::Iterator::getIndex
int getIndex() const
Definition: Chunk.h:309
inet::Chunk::unspecifiedLength
static const b unspecifiedLength
Definition: Chunk.h:287
inet::ChunkQueue::peek
const Ptr< const Chunk > peek(b length=b(-1), int flags=0) const
Returns the designated data from the head of the queue as an immutable chunk in its current represent...
Definition: ChunkQueue.cc:56
copy
MIT License free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to copy
Definition: LICENSE.txt:8
inet::ChunkQueue::peekAt
const Ptr< const Chunk > peekAt(b offset, b length, int flags=0) const
Returns the designated data at the given offset as an immutable chunk in its current representation.
Definition: ChunkQueue.cc:62
inet::ChunkQueue::pushedLength
b pushedLength
Definition: ChunkQueue.h:39