Skip to content

Commit

Permalink
[core] Refax: improve logging and code readability around specific lo…
Browse files Browse the repository at this point in the history
…gging (#2511).

Fixed non-compiling conditional debug code.
  • Loading branch information
ethouris authored Nov 7, 2022
1 parent f7a024a commit 2fd1363
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 87 deletions.
117 changes: 82 additions & 35 deletions srtcore/packet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,10 @@ extern Logger inlog;
}
using namespace srt_logging;

namespace srt {

// Set up the aliases in the constructure
srt::CPacket::CPacket()
CPacket::CPacket()
: m_extra_pad()
, m_data_owned(false)
, m_iSeqNo((int32_t&)(m_nHeader[SRT_PH_SEQNO]))
Expand All @@ -194,12 +196,12 @@ srt::CPacket::CPacket()
m_PacketVector[PV_DATA].set(NULL, 0);
}

char* srt::CPacket::getData()
char* CPacket::getData()
{
return (char*)m_PacketVector[PV_DATA].dataRef();
}

void srt::CPacket::allocate(size_t alloc_buffer_size)
void CPacket::allocate(size_t alloc_buffer_size)
{
if (m_data_owned)
{
Expand All @@ -213,14 +215,14 @@ void srt::CPacket::allocate(size_t alloc_buffer_size)
m_data_owned = true;
}

void srt::CPacket::deallocate()
void CPacket::deallocate()
{
if (m_data_owned)
delete[](char*) m_PacketVector[PV_DATA].data();
m_PacketVector[PV_DATA].set(NULL, 0);
}

char* srt::CPacket::release()
char* CPacket::release()
{
// When not owned, release returns NULL.
char* buffer = NULL;
Expand All @@ -234,38 +236,86 @@ char* srt::CPacket::release()
return buffer;
}

srt::CPacket::~CPacket()
CPacket::~CPacket()
{
// PV_HEADER is always owned, PV_DATA may use a "borrowed" buffer.
// Delete the internal buffer only if it was declared as owned.
if (m_data_owned)
delete[](char*) m_PacketVector[PV_DATA].data();
}

size_t srt::CPacket::getLength() const
size_t CPacket::getLength() const
{
return m_PacketVector[PV_DATA].size();
}

void srt::CPacket::setLength(size_t len)
void CPacket::setLength(size_t len)
{
m_PacketVector[PV_DATA].setLength(len);
}

void srt::CPacket::setLength(size_t len, size_t cap)
void CPacket::setLength(size_t len, size_t cap)
{
SRT_ASSERT(len <= cap);
setLength(len);
m_zCapacity = cap;
}

void srt::CPacket::pack(UDTMessageType pkttype, const int32_t* lparam, void* rparam, size_t size)
#if ENABLE_HEAVY_LOGGING
// Debug only
static std::string FormatNumbers(UDTMessageType pkttype, const int32_t* lparam, void* rparam, size_t size)
{
// This may be changed over time, so use special interpretation
// only for certain types, and still display all data, no matter
// if it is expected to provide anything or not.
std::ostringstream out;

out << "ARG=";
if (lparam)
out << *lparam;
else
out << "none";

if (size == 0)
{
out << " [no data]";
return out.str();
}
else if (!rparam)
{
out << " [ {" << size << "} ]";
return out.str();
}

bool interp_as_seq = (pkttype == UMSG_LOSSREPORT || pkttype == UMSG_DROPREQ);

out << " [ ";
for (size_t i = 0; i < size; ++i)
{
int32_t val = ((int32_t*)rparam)[i];
if (interp_as_seq)
{
if (val & LOSSDATA_SEQNO_RANGE_FIRST)
out << "<" << (val & (~LOSSDATA_SEQNO_RANGE_FIRST)) << ">";
else
out << val;
}
else
{
out << std::showpos << std::hex << val << "/" << std::dec << val;
}
}

out << "]";
return out.str();
}
#endif

void CPacket::pack(UDTMessageType pkttype, const int32_t* lparam, void* rparam, size_t size)
{
// Set (bit-0 = 1) and (bit-1~15 = type)
setControl(pkttype);
HLOGC(inlog.Debug,
log << "pack: type=" << MessageTypeStr(pkttype) << " ARG=" << (lparam ? Sprint(*lparam) : std::string("NULL"))
<< " [ " << (rparam ? Sprint(*(int32_t*)rparam) : std::string()) << " ]");
HLOGC(inlog.Debug, log << "pack: type=" << MessageTypeStr(pkttype) << FormatNumbers(pkttype, lparam, rparam, size));

// Set additional information and control information field
switch (pkttype)
Expand Down Expand Up @@ -371,7 +421,7 @@ void srt::CPacket::pack(UDTMessageType pkttype, const int32_t* lparam, void* rpa
}
}

void srt::CPacket::toNL()
void CPacket::toNL()
{
// XXX USE HtoNLA!
if (isControl())
Expand All @@ -389,7 +439,7 @@ void srt::CPacket::toNL()
}
}

void srt::CPacket::toHL()
void CPacket::toHL()
{
// convert back into local host order
uint32_t* p = m_nHeader;
Expand All @@ -406,22 +456,22 @@ void srt::CPacket::toHL()
}
}

srt::IOVector* srt::CPacket::getPacketVector()
IOVector* CPacket::getPacketVector()
{
return m_PacketVector;
}

srt::UDTMessageType srt::CPacket::getType() const
UDTMessageType CPacket::getType() const
{
return UDTMessageType(SEQNO_MSGTYPE::unwrap(m_nHeader[SRT_PH_SEQNO]));
}

int srt::CPacket::getExtendedType() const
int CPacket::getExtendedType() const
{
return SEQNO_EXTTYPE::unwrap(m_nHeader[SRT_PH_SEQNO]);
}

int32_t srt::CPacket::getAckSeqNo() const
int32_t CPacket::getAckSeqNo() const
{
// read additional information field
// This field is used only in UMSG_ACK and UMSG_ACKACK,
Expand All @@ -430,7 +480,7 @@ int32_t srt::CPacket::getAckSeqNo() const
return m_nHeader[SRT_PH_MSGNO];
}

uint16_t srt::CPacket::getControlFlags() const
uint16_t CPacket::getControlFlags() const
{
// This returns exactly the "extended type" value,
// which is not used at all in case when the standard
Expand All @@ -439,17 +489,17 @@ uint16_t srt::CPacket::getControlFlags() const
return SEQNO_EXTTYPE::unwrap(m_nHeader[SRT_PH_SEQNO]);
}

srt::PacketBoundary srt::CPacket::getMsgBoundary() const
PacketBoundary CPacket::getMsgBoundary() const
{
return PacketBoundary(MSGNO_PACKET_BOUNDARY::unwrap(m_nHeader[SRT_PH_MSGNO]));
}

bool srt::CPacket::getMsgOrderFlag() const
bool CPacket::getMsgOrderFlag() const
{
return 0 != MSGNO_PACKET_INORDER::unwrap(m_nHeader[SRT_PH_MSGNO]);
}

int32_t srt::CPacket::getMsgSeq(bool has_rexmit) const
int32_t CPacket::getMsgSeq(bool has_rexmit) const
{
if (has_rexmit)
{
Expand All @@ -461,38 +511,38 @@ int32_t srt::CPacket::getMsgSeq(bool has_rexmit) const
}
}

bool srt::CPacket::getRexmitFlag() const
bool CPacket::getRexmitFlag() const
{
return 0 != MSGNO_REXMIT::unwrap(m_nHeader[SRT_PH_MSGNO]);
}

void srt::CPacket::setRexmitFlag(bool bRexmit)
void CPacket::setRexmitFlag(bool bRexmit)
{
const int32_t clr_msgno = m_nHeader[SRT_PH_MSGNO] & ~MSGNO_REXMIT::mask;
m_nHeader[SRT_PH_MSGNO] = clr_msgno | MSGNO_REXMIT::wrap(bRexmit? 1 : 0);
}

srt::EncryptionKeySpec srt::CPacket::getMsgCryptoFlags() const
EncryptionKeySpec CPacket::getMsgCryptoFlags() const
{
return EncryptionKeySpec(MSGNO_ENCKEYSPEC::unwrap(m_nHeader[SRT_PH_MSGNO]));
}

// This is required as the encryption/decryption happens in place.
// This is required to clear off the flags after decryption or set
// crypto flags after encrypting a packet.
void srt::CPacket::setMsgCryptoFlags(EncryptionKeySpec spec)
void CPacket::setMsgCryptoFlags(EncryptionKeySpec spec)
{
int32_t clr_msgno = m_nHeader[SRT_PH_MSGNO] & ~MSGNO_ENCKEYSPEC::mask;
m_nHeader[SRT_PH_MSGNO] = clr_msgno | EncryptionKeyBits(spec);
}

uint32_t srt::CPacket::getMsgTimeStamp() const
uint32_t CPacket::getMsgTimeStamp() const
{
// SRT_DEBUG_TSBPD_WRAP may enable smaller timestamp for faster wraparoud handling tests
return (uint32_t)m_nHeader[SRT_PH_TIMESTAMP] & TIMESTAMP_MASK;
}

srt::CPacket* srt::CPacket::clone() const
CPacket* CPacket::clone() const
{
CPacket* pkt = new CPacket;
memcpy((pkt->m_nHeader), m_nHeader, HDR_SIZE);
Expand All @@ -503,9 +553,6 @@ srt::CPacket* srt::CPacket::clone() const
return pkt;
}

namespace srt
{

// Useful for debugging
std::string PacketMessageFlagStr(uint32_t msgno_field)
{
Expand Down Expand Up @@ -534,10 +581,8 @@ inline void SprintSpecialWord(std::ostream& os, int32_t val)
os << val;
}

} // namespace srt

#if ENABLE_LOGGING
std::string srt::CPacket::Info()
std::string CPacket::Info()
{
std::ostringstream os;
os << "TARGET=@" << m_iID << " ";
Expand Down Expand Up @@ -592,3 +637,5 @@ std::string srt::CPacket::Info()
return os.str();
}
#endif

} // end namespace srt
Loading

0 comments on commit 2fd1363

Please sign in to comment.