Mixes for Privacy and Anonymity in the Internet
Public Member Functions | Static Public Member Functions | Private Attributes | List of all members
CAQueue Class Reference

This is a simple FIFO-Queue. More...

Public Member Functions

 CAQueue (UINT32 expectedElementSize=0)
 Give the size of the amount of data what you will add in one step. More...
 
 ~CAQueue ()
 Deletes this Queue and all stored data. More...
 
SINT32 add (const void *buff, UINT32 size)
 Adds data to the Queue. More...
 
SINT32 close ()
 Closes the Queue (for writing). More...
 
SINT32 get (UINT8 *pbuff, UINT32 *psize)
 Gets up to psize number of bytes from the Queue. More...
 
SINT32 getOrWait (UINT8 *pbuff, UINT32 *psize)
 Gets data from the Queue or waits until some data is available, if the Queue is empty. More...
 
SINT32 getOrWait (UINT8 *pbuff, UINT32 *psize, UINT32 msTimeOut)
 Gets data from the Queue or waits until some data is available, if the Queue is empty or a timeout is reached. More...
 
SINT32 peek (UINT8 *pbuff, UINT32 *psize)
 Peeks data from the Queue. More...
 
SINT32 remove (UINT32 *psize)
 Removes data from the Queue. More...
 
SINT32 clean ()
 Removes any stored data from the Queue. More...
 
UINT32 getSize ()
 Returns the size of stored data in byte. More...
 
UINT32 getSizeLookFree ()
 Returns the size of stored data in byte. More...
 
bool isEmpty ()
 Returns true, if the Queue is empty. More...
 
bool isClosed ()
 Returns true, if the Queue is closed. More...
 

Static Public Member Functions

static SINT32 test ()
 Method to test the Queue. More...
 

Private Attributes

volatile QUEUEm_Queue
 
volatile QUEUEm_lastElem
 
volatile UINT32 m_nQueueSize
 
volatile bool m_bClosed
 
UINT32 m_nExpectedElementSize
 
CAMutexm_pcsQueue
 
CAConditionVariablem_pconvarSize
 

Detailed Description

This is a simple FIFO-Queue.

You can add data and get them back. This class is thread safe. TODO: The handling of getAndWait is not correct because remove could intercept.... Maybe we do not neeed an other Mutex other than then ConVar....

Constructor & Destructor Documentation

◆ CAQueue()

CAQueue::CAQueue ( UINT32  expectedElementSize = 0)

Give the size of the amount of data what you will add in one step.

Used for optimizations. Use expectedElementSize=0, if you have no idea about the typicall amount of data added in one call to add().

References m_bClosed, m_lastElem, m_nExpectedElementSize, m_nQueueSize, m_pconvarSize, m_pcsQueue, and m_Queue.

Referenced by test().

◆ ~CAQueue()

CAQueue::~CAQueue ( )

Deletes this Queue and all stored data.

References clean(), m_pconvarSize, and m_pcsQueue.

Member Function Documentation

◆ add()

SINT32 CAQueue::add ( const void *  buff,
UINT32  size 
)

◆ clean()

SINT32 CAQueue::clean ( )

◆ close()

SINT32 CAQueue::close ( )

Closes the Queue (for writing).

One can still read the remaing bytes out of the queue.

References E_SUCCESS, CAMutex::lock(), m_bClosed, m_pconvarSize, m_pcsQueue, m_Queue, CAConditionVariable::signal(), and CAMutex::unlock().

Referenced by CALastMixA::loop().

◆ get()

SINT32 CAQueue::get ( UINT8 pbuff,
UINT32 psize 
)

Gets up to psize number of bytes from the Queue.

The data is removed from the Queue.

Parameters
pbuffpointer to a buffer, there the data should be stored
psizeon call contains the size of pbuff, on return contains the size of returned data
Return values
E_SUCCESSif succesful
E_CLOSEDif the queue is empty and closed
E_UNKNOWNin case of an error

References E_CLOSED, E_SUCCESS, E_UNKNOWN, _t_queue::index, CAMutex::lock(), m_bClosed, m_nQueueSize, m_pcsQueue, m_Queue, _t_queue::next, _t_queue::pBuff, _t_queue::size, and CAMutex::unlock().

Referenced by CAFirstMix::doUserLogin_internal(), getOrWait(), CAFirstMixA::loop(), CALastMixA::loop(), CAFirstMixB::loop(), CALastMixB::loop(), CAFirstMixA::sendToUsers(), and test().

◆ getOrWait() [1/2]

SINT32 CAQueue::getOrWait ( UINT8 pbuff,
UINT32 psize 
)

Gets data from the Queue or waits until some data is available, if the Queue is empty.

The data is removed from the Queue.

Parameters
pbuffpointer to a buffer, there the data should be stored
psizeon call contains the size of pbuff, on return contains the size of returned data
Return values
E_SUCCESSif succesful
E_CLOSEDif the queue is empty and closed
E_UNKNOWNin case of an error

References get(), CAMutex::lock(), m_bClosed, m_pconvarSize, m_Queue, CAMutex::unlock(), and CAConditionVariable::wait().

Referenced by consumer().

◆ getOrWait() [2/2]

SINT32 CAQueue::getOrWait ( UINT8 pbuff,
UINT32 psize,
UINT32  msTimeout 
)

Gets data from the Queue or waits until some data is available, if the Queue is empty or a timeout is reached.

The data is removed from the Queue.

Parameters
pbuffpointer to a buffer, there the data should be stored
psizeon call contains the size of pbuff, on return contains the size of returned data
msTimeouttimeout in milli seconds
Return values
E_SUCCESSif succesful
E_TIMEDOUTif timeout was reached
E_UNKNOWNin case of an error

References E_TIMEDOUT, get(), CAMutex::lock(), m_pconvarSize, m_Queue, CAMutex::unlock(), and CAConditionVariable::wait().

◆ getSize()

UINT32 CAQueue::getSize ( )

◆ getSizeLookFree()

UINT32 CAQueue::getSizeLookFree ( )

Returns the size of stored data in byte.

This is the look free version which might return a slighty wrong result due to concurrent changes in the queue.

Returns
size of Queue

References m_nQueueSize.

Referenced by CALastMixA::loop().

◆ isClosed()

bool CAQueue::isClosed ( )

Returns true, if the Queue is closed.

Return values
true,ifQueue is closed
false,otherwise

References m_bClosed.

Referenced by CALastMixA::loop().

◆ isEmpty()

bool CAQueue::isEmpty ( )

Returns true, if the Queue is empty.

Return values
true,ifQueue is empty
false,ifQueue contains data

References m_Queue.

Referenced by CALastMixA::loop(), CAChain::sendUpstreamData(), and test().

◆ peek()

SINT32 CAQueue::peek ( UINT8 pbuff,
UINT32 psize 
)

Peeks data from the Queue.

The data is NOT removed from the Queue.

Parameters
pbuffpointer to a buffer, where the data should be stored
psizeon call contains the size of pbuff, on return contains the size of returned data
Return values
E_SUCCESSif succesful
E_CLOSEDif the queue is already empty AND closed
E_UNKNOWNin case of an error

References E_CLOSED, E_SUCCESS, E_UNKNOWN, _t_queue::index, CAMutex::lock(), m_bClosed, m_pcsQueue, m_Queue, _t_queue::next, _t_queue::pBuff, _t_queue::size, and CAMutex::unlock().

Referenced by MemFormatTarget::dumpMem(), CALastMixA::loop(), and CAChain::sendUpstreamDataInternal().

◆ remove()

SINT32 CAQueue::remove ( UINT32 psize)

Removes data from the Queue.

Parameters
psizeon call contains the size of data to remove, on return contains the size of removed data
Return values
E_SUCCESSif succesful
E_UNKNOWNin case of an error

References E_SUCCESS, E_UNKNOWN, _t_queue::index, CAMutex::lock(), m_nQueueSize, m_pcsQueue, m_Queue, _t_queue::next, _t_queue::pBuff, _t_queue::size, and CAMutex::unlock().

Referenced by CALastMixA::loop(), and CAChain::sendUpstreamDataInternal().

◆ test()

SINT32 CAQueue::test ( )
static

Method to test the Queue.

Return values
E_SUCCESS,ifQueue implementation seams to be ok

References add(), __queue_test::buff, CAQueue(), consumer(), E_SUCCESS, E_UNKNOWN, get(), getRandom(), getSize(), isEmpty(), CAThread::join(), __queue_test::len, __queue_test::pQueue, producer(), CAThread::setMainLoop(), CAThread::start(), and TEST_SIZE.

Member Data Documentation

◆ m_bClosed

volatile bool CAQueue::m_bClosed
private

◆ m_lastElem

volatile QUEUE* CAQueue::m_lastElem
private

Referenced by add(), CAQueue(), and clean().

◆ m_nExpectedElementSize

UINT32 CAQueue::m_nExpectedElementSize
private

Referenced by CAQueue().

◆ m_nQueueSize

volatile UINT32 CAQueue::m_nQueueSize
private

◆ m_pconvarSize

CAConditionVariable* CAQueue::m_pconvarSize
private

Referenced by add(), CAQueue(), close(), getOrWait(), and ~CAQueue().

◆ m_pcsQueue

CAMutex* CAQueue::m_pcsQueue
private

◆ m_Queue

volatile QUEUE* CAQueue::m_Queue
private