Skip to content

Commit

Permalink
[core] Added assertions and error checks to CRcvBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
maxsharabayko authored and rndi committed Aug 27, 2019
1 parent 92dc53c commit 347c433
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
21 changes: 17 additions & 4 deletions srtcore/buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -676,9 +676,9 @@ void CSndBuffer::increase()
//const int CRcvBuffer::TSBPD_DRIFT_PRT_SAMPLES = 200; // ACK-ACK packets
#endif

CRcvBuffer::CRcvBuffer(CUnitQueue* queue, int bufsize):
CRcvBuffer::CRcvBuffer(CUnitQueue* queue, int bufsize_pkts):
m_pUnit(NULL),
m_iSize(bufsize),
m_iSize(bufsize_pkts),
m_pUnitQueue(queue),
m_iStartPos(0),
m_iLastAckPos(0),
Expand Down Expand Up @@ -760,7 +760,11 @@ void CRcvBuffer::countBytes(int pkts, int bytes, bool acked)

int CRcvBuffer::addData(CUnit* unit, int offset)
{
int pos = (m_iLastAckPos + offset) % m_iSize;
SRT_ASSERT(unit != NULL);
if (offset >= getAvailBufSize())
return -1;

const int pos = (m_iLastAckPos + offset) % m_iSize;
if (offset >= m_iMaxPos)
m_iMaxPos = offset + 1;

Expand All @@ -784,11 +788,17 @@ int CRcvBuffer::readBuffer(char* data, int len)
char* begin = data;
#endif

uint64_t now = (m_bTsbPdMode ? CTimer::getTime() : uint64_t());
const uint64_t now = (m_bTsbPdMode ? CTimer::getTime() : uint64_t());

HLOGC(dlog.Debug, log << CONID() << "readBuffer: start=" << p << " lastack=" << lastack);
while ((p != lastack) && (rs > 0))
{
if (m_pUnit[p] == NULL)
{
LOGC(dlog.Error, log << CONID() << " IPE readBuffer on null packet pointer");
return -1;
}

if (m_bTsbPdMode)
{
HLOGC(dlog.Debug, log << CONID() << "readBuffer: chk if time2play: NOW=" << now << " PKT TS=" << getPktTsbPdTime(m_pUnit[p]->m_Packet.getMsgTimeStamp()));
Expand Down Expand Up @@ -871,6 +881,9 @@ int CRcvBuffer::readBufferToFile(fstream& ofs, int len)

void CRcvBuffer::ackData(int len)
{
SRT_ASSERT(len < m_iSize);
SRT_ASSERT(len > 0);

{
int pkts = 0;
int bytes = 0;
Expand Down
11 changes: 9 additions & 2 deletions srtcore/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,10 @@ class CRcvBuffer
std::string CONID() const { return ""; }


CRcvBuffer(CUnitQueue* queue, int bufsize = 65536);
/// Construct the buffer.
/// @param [in] queue CUnitQueue that actually holds the units (packets)
/// @param [in] bufsize_pkts in units (packets)
CRcvBuffer(CUnitQueue* queue, int bufsize_pkts = 65536);
~CRcvBuffer();

/// Write data into the buffer.
Expand Down Expand Up @@ -270,7 +273,11 @@ class CRcvBuffer
void ackData(int len);

/// Query how many buffer space left for data receiving.
/// Actually only acknowledged packets, that are still in the buffer,
/// are considered to take buffer space.
///
/// @return size of available buffer space (including user buffer) for data receiving.
/// Not counting unacknowledged packets.

int getAvailBufSize() const;

Expand Down Expand Up @@ -419,7 +426,7 @@ class CRcvBuffer

private:
CUnit** m_pUnit; // pointer to the protocol buffer
int m_iSize; // size of the protocol buffer
int m_iSize; // size of the protocol buffer in units
CUnitQueue* m_pUnitQueue; // the shared unit queue

int m_iStartPos; // the head position for I/O (inclusive)
Expand Down
2 changes: 1 addition & 1 deletion srtcore/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5223,7 +5223,7 @@ int CUDT::receiveBuffer(char* data, int len)
throw CUDTException(MJ_CONNECTION, MN_CONNLOST, 0);
}

int res = m_pRcvBuffer->readBuffer(data, len);
const int res = m_pRcvBuffer->readBuffer(data, len);

/* Kick TsbPd thread to schedule next wakeup (if running) */
if (m_bTsbPd)
Expand Down

0 comments on commit 347c433

Please sign in to comment.