📄 core.cpp.svn-base
字号:
CUDTException CUDT::send(const char* data, const int len, int& size){ size = 0; if (UDT_DGRAM == m_iSockType) return CUDTException(5, 10, 0); CGuard sendguard(m_SendLock); // throw an exception if not connected if (m_bBroken || m_bClosing) return CUDTException(2, 1, 0); else if (!m_bConnected) return CUDTException(2, 2, 0); if (len <= 0) return CUDTException(); if (m_iSndBufSize <= m_pSndBuffer->getCurrBufSize()) { if (!m_bSynSending) return CUDTException(6, 1, 0); else { // wait here during a blocking sending #ifndef WIN32 pthread_mutex_lock(&m_SendBlockLock); if (m_iSndTimeOut < 0) { while (!m_bBroken && m_bConnected && !m_bClosing && (m_iSndBufSize <= m_pSndBuffer->getCurrBufSize())) pthread_cond_wait(&m_SendBlockCond, &m_SendBlockLock); } else { uint64_t exptime = CTimer::getTime() + m_iSndTimeOut * 1000ULL; timespec locktime; locktime.tv_sec = exptime / 1000000; locktime.tv_nsec = (exptime % 1000000) * 1000; pthread_cond_timedwait(&m_SendBlockCond, &m_SendBlockLock, &locktime); } pthread_mutex_unlock(&m_SendBlockLock); #else if (m_iSndTimeOut < 0) { while (!m_bBroken && m_bConnected && !m_bClosing && (m_iSndBufSize <= m_pSndBuffer->getCurrBufSize())) WaitForSingleObject(m_SendBlockCond, INFINITE); } else WaitForSingleObject(m_SendBlockCond, DWORD(m_iSndTimeOut)); #endif // check the connection status if (m_bBroken || m_bClosing) return CUDTException(2, 1, 0); else if (!m_bConnected) return CUDTException(2, 2, 0); } } if (m_iSndBufSize <= m_pSndBuffer->getCurrBufSize()) return CUDTException(); size = (m_iSndBufSize - m_pSndBuffer->getCurrBufSize()) * m_iPayloadSize; if (size > len) size = len; // insert the user buffer into the sening list m_pSndBuffer->addBuffer(data, size); // insert this socket to snd list if it is not on the list yet m_pSndQueue->m_pSndUList->update(this, false); return CUDTException();}CUDTException CUDT::recv(char* data, const int len, int& outSize){ outSize = 0; if (UDT_DGRAM == m_iSockType) return CUDTException(5, 10, 0); CGuard recvguard(m_RecvLock); // throw an exception if not connected if (!m_bConnected) return CUDTException(2, 2, 0); else if ((m_bBroken || m_bClosing) && (0 == m_pRcvBuffer->getRcvDataSize())) return CUDTException(2, 1, 0); if (len <= 0) return CUDTException(); if (0 == m_pRcvBuffer->getRcvDataSize()) { if (!m_bSynRecving) return CUDTException(6, 2, 0); else { #ifndef WIN32 pthread_mutex_lock(&m_RecvDataLock); if (m_iRcvTimeOut < 0) { while (!m_bBroken && m_bConnected && !m_bClosing && (0 == m_pRcvBuffer->getRcvDataSize())) pthread_cond_wait(&m_RecvDataCond, &m_RecvDataLock); } else { uint64_t exptime = CTimer::getTime() + m_iRcvTimeOut * 1000ULL; timespec locktime; locktime.tv_sec = exptime / 1000000; locktime.tv_nsec = (exptime % 1000000) * 1000; pthread_cond_timedwait(&m_RecvDataCond, &m_RecvDataLock, &locktime); } pthread_mutex_unlock(&m_RecvDataLock); #else if (m_iRcvTimeOut < 0) { while (!m_bBroken && m_bConnected && !m_bClosing && (0 == m_pRcvBuffer->getRcvDataSize())) WaitForSingleObject(m_RecvDataCond, INFINITE); } else WaitForSingleObject(m_RecvDataCond, DWORD(m_iRcvTimeOut)); #endif } } // throw an exception if not connected if (!m_bConnected) return CUDTException(2, 2, 0); else if ((m_bBroken || m_bClosing) && (0 == m_pRcvBuffer->getRcvDataSize())) return CUDTException(2, 1, 0); outSize = m_pRcvBuffer->readBuffer(data, len); return CUDTException();}CUDTException CUDT::sendmsg(const char* data, const int len, const int msttl, const bool inorder, int& outSize){ outSize = 0; if (UDT_STREAM == m_iSockType) return CUDTException(5, 9, 0); CGuard sendguard(m_SendLock); // throw an exception if not connected if (m_bBroken || m_bClosing) return CUDTException(2, 1, 0); else if (!m_bConnected) return CUDTException(2, 2, 0); if (len <= 0) return CUDTException(); if (len > m_iSndBufSize * m_iPayloadSize) return CUDTException(5, 12, 0); if ((m_iSndBufSize - m_pSndBuffer->getCurrBufSize()) * m_iPayloadSize < len) { if (!m_bSynSending) return CUDTException(6, 1, 0); else { // wait here during a blocking sending #ifndef WIN32 pthread_mutex_lock(&m_SendBlockLock); if (m_iSndTimeOut < 0) { while (!m_bBroken && m_bConnected && !m_bClosing && ((m_iSndBufSize - m_pSndBuffer->getCurrBufSize()) * m_iPayloadSize < len)) pthread_cond_wait(&m_SendBlockCond, &m_SendBlockLock); } else { uint64_t exptime = CTimer::getTime() + m_iSndTimeOut * 1000ULL; timespec locktime; locktime.tv_sec = exptime / 1000000; locktime.tv_nsec = (exptime % 1000000) * 1000; pthread_cond_timedwait(&m_SendBlockCond, &m_SendBlockLock, &locktime); } pthread_mutex_unlock(&m_SendBlockLock); #else if (m_iSndTimeOut < 0) { while (!m_bBroken && m_bConnected && !m_bClosing && ((m_iSndBufSize - m_pSndBuffer->getCurrBufSize()) * m_iPayloadSize < len)) WaitForSingleObject(m_SendBlockCond, INFINITE); } else WaitForSingleObject(m_SendBlockCond, DWORD(m_iSndTimeOut)); #endif // check the connection status if (m_bBroken || m_bClosing) return CUDTException(2, 1, 0); else if (!m_bConnected) return CUDTException(2, 2, 0); } } if ((m_iSndBufSize - m_pSndBuffer->getCurrBufSize()) * m_iPayloadSize < len) return CUDTException(); // insert the user buffer into the sening list m_pSndBuffer->addBuffer(data, len, msttl, inorder); // insert this socket to the snd list if it is not on the list yet m_pSndQueue->m_pSndUList->update(this, false); outSize = len; return CUDTException();}CUDTException CUDT::recvmsg(char* data, const int len, int& outSize){ outSize = 0; if (UDT_STREAM == m_iSockType) return CUDTException(5, 9, 0); CGuard recvguard(m_RecvLock); // throw an exception if not connected if (!m_bConnected) return CUDTException(2, 2, 0); if (len <= 0) return CUDTException(); if (m_bBroken || m_bClosing) { int res = m_pRcvBuffer->readMsg(data, len); if (0 == res) return CUDTException(2, 1, 0); else { outSize = res; return CUDTException(); } } if (!m_bSynRecving) { int res = m_pRcvBuffer->readMsg(data, len); if (0 == res) return CUDTException(6, 2, 0); else { outSize = res; return CUDTException(); } } int res = 0; bool timeout = false; do { #ifndef WIN32 pthread_mutex_lock(&m_RecvDataLock); if (m_iRcvTimeOut < 0) { while (!m_bBroken && m_bConnected && !m_bClosing && (0 == (res = m_pRcvBuffer->readMsg(data, len)))) pthread_cond_wait(&m_RecvDataCond, &m_RecvDataLock); } else { uint64_t exptime = CTimer::getTime() + m_iRcvTimeOut * 1000ULL; timespec locktime; locktime.tv_sec = exptime / 1000000; locktime.tv_nsec = (exptime % 1000000) * 1000; if (pthread_cond_timedwait(&m_RecvDataCond, &m_RecvDataLock, &locktime) == ETIMEDOUT) timeout = true; res = m_pRcvBuffer->readMsg(data, len); } pthread_mutex_unlock(&m_RecvDataLock); #else if (m_iRcvTimeOut < 0) { while (!m_bBroken && m_bConnected && !m_bClosing && (0 == (res = m_pRcvBuffer->readMsg(data, len)))) WaitForSingleObject(m_RecvDataCond, INFINITE); } else { if (WaitForSingleObject(m_RecvDataCond, DWORD(m_iRcvTimeOut)) == WAIT_TIMEOUT) timeout = true; res = m_pRcvBuffer->readMsg(data, len); } #endif if (m_bBroken || m_bClosing) return CUDTException(2, 1, 0); else if (!m_bConnected) return CUDTException(2, 2, 0); } while ((0 == res) && !timeout); outSize = res; return CUDTException();}CUDTException CUDT::sendfile(ifstream& ifs, const int64_t offset, const int64_t size, const int block, int64_t& outSize){ outSize = 0; if (UDT_DGRAM == m_iSockType) return CUDTException(5, 10, 0); CGuard sendguard(m_SendLock); if (m_bBroken || m_bClosing) return CUDTException(2, 1, 0); else if (!m_bConnected) return CUDTException(2, 2, 0); if (size <= 0) return CUDTException(); int64_t tosend = size; int unitsize; // positioning... //try //{ ifs.seekg(offset); int64_t seekedPos = ifs.tellg(); assert(seekedPos == offset); if (seekedPos != offset) { return CUDTException(4, 1, 0); } //} //catch (...) //{ // return CUDTException(4, 1); //} // sending block by block while (tosend > 0) { unitsize = int((tosend >= block) ? block : tosend); #ifndef WIN32 pthread_mutex_lock(&m_SendBlockLock); while (!m_bBroken && m_bConnected && !m_bClosing && (m_iSndBufSize <= m_pSndBuffer->getCurrBufSize())) pthread_cond_wait(&m_SendBlockCond, &m_SendBlockLock); pthread_mutex_unlock(&m_SendBlockLock); #else while (!m_bBroken && m_bConnected && !m_bClosing && (m_iSndBufSize <= m_pSndBuffer->getCurrBufSize())) WaitForSingleObject(m_SendBlockCond, INFINITE); #endif if (m_bBroken || m_bClosing) return CUDTException(2, 1, 0); else if (!m_bConnected) return CUDTException(2, 2, 0); m_pSndBuffer->addBufferFromFile(ifs, unitsize); // insert this socket to snd list if it is not on the list yet m_pSndQueue->m_pSndUList->update(this, false); tosend -= unitsize; } outSize = size; return CUDTException();}CUDTException CUDT::recvfile(ofstream& ofs, const int64_t offset, const int64_t size, const int block, int64_t& outSize){ outSize = 0; if (UDT_DGRAM == m_iSockType) return CUDTException(5, 10, 0); CGuard recvguard(m_RecvLock); if (!m_bConnected) return CUDTException(2, 2, 0); else if ((m_bBroken || m_bClosing) && (0 == m_pRcvBuffer->getRcvDataSize())) return CUDTException(2, 1, 0); if (size <= 0) return CUDTException(); int64_t torecv = size; int unitsize = block; int recvsize; // positioning... //try //{ ofs.seekp(offset); int64_t seekedPos = ofs.tellp(); assert(seekedPos == offset); if (seekedPos != offset) { return CUDTException(4, 1, 0); } //} //catch (...) //{ // return CUDTException(4, 3); //} // receiving... "recvfile" is always blocking while (torecv > 0) { #ifndef WIN32 pthread_mutex_lock(&m_RecvDataLock); while (!m_bBroken && m_bConnected && !m_bClosing && (0 == m_pRcvBuffer->getRcvDataSize())) pthread_cond_wait(&m_RecvDataCond, &m_RecvDataLock); pthread_mutex_unlock(&m_RecvDataLock); #else while (!m_bBroken && m_bConnected && !m_bClosing && (0 == m_pRcvBuffer->getRcvDataSize())) WaitForSingleObject(m_RecvDataCond, INFINITE); #endif if (!m_bConnected) return CUDTException(2, 2, 0); else if ((m_bBroken || m_bClosing) && (0 == m_pRcvBuffer->getRcvDataSize())) return CUDTException(2, 1, 0); unitsize = int((torecv >= block) ? block : torecv); recvsize = m_pRcvBuffer->readBufferToFile(ofs, unitsize); torecv -= recvsize; } outSize = size - torecv; return CUDTException(); }CUDTException CUDT::sample(CPerfMon* perf, bool clear){ if (!m_bConnected) return CUDTException(2, 2, 0); if (m_bBroken || m_bClosing) return CUDTException(2, 1, 0); uint64_t currtime = CTimer::getTime(); perf->msTimeStamp = (currtime - m_StartTime) / 1000; m_llSentTotal += m_llTraceSent; m_llRecvTotal += m_llTraceRecv; m_iSndLossTotal += m_iTraceSndLoss; m_iRcvLossTotal += m_iTraceRcvLoss; m_iRetransTotal += m_iTraceRetrans; m_iSentACKTotal += m_iSentACK; m_iRecvACKTotal += m_iRecvACK; m_iSentNAKTotal += m_iSentNAK; m_iRecvNAKTotal += m_iRecvNAK; perf->pktSentTotal = m_llSentTotal; perf->pktRecvTotal = m_llRecvTotal; perf->pktSndLossTotal = m_iSndLossTotal; perf->pktRcvLossTotal = m_iRcvLossTotal;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -