📄 core.cpp.svn-base
字号:
/*****************************************************************************Copyright (c) 2001 - 2008, The Board of Trustees of the University of Illinois.All rights reserved.Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditions aremet:* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.* Neither the name of the University of Illinois nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "ASIS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULARPURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER ORCONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, ORPROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDINGNEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THISSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*****************************************************************************//*****************************************************************************written by Yunhong Gu, last updated 11/1/2008*****************************************************************************/#ifndef WIN32 #include <unistd.h> #include <netdb.h> #include <arpa/inet.h> #include <cerrno> #include <cstring> #include <cstdlib>#else #include <winsock2.h> #include <ws2tcpip.h> #include <wspiapi.h>#endif#include <cassert>#include <cmath>#include "queue.h"#include "core.h"#ifdef USING_USTLusing namespace ustl;#elseusing namespace std;#endifauto_ptr<CUDTUnited> CUDT::s_UDTUnited(0);const UDTSOCKET CUDT::INVALID_SOCK = -1;const int CUDT::ERROR = -1;const UDTSOCKET UDT::INVALID_SOCK = CUDT::INVALID_SOCK;const int UDT::ERROR = CUDT::ERROR;const int32_t CSeqNo::m_iSeqNoTH = 0x3FFFFFFF;const int32_t CSeqNo::m_iMaxSeqNo = 0x7FFFFFFF;const int32_t CAckNo::m_iMaxAckSeqNo = 0x7FFFFFFF;const int32_t CMsgNo::m_iMsgNoTH = 0xFFFFFFF;const int32_t CMsgNo::m_iMaxMsgNo = 0x1FFFFFFF;const int CUDT::m_iVersion = 4;const int CUDT::m_iSYNInterval = 10000;const int CUDT::m_iSelfClockInterval = 64;CUDTUnited* CUDT::getUDTUnited(){ if (s_UDTUnited.get() == NULL) { s_UDTUnited.reset(UDT::construct(new (nothrow) CUDTUnited())); } return s_UDTUnited.get();}CUDT::CUDT() throw(){ // Initilize mutex and condition variables initSynch(); // Default UDT configurations m_iMSS = 1500; m_bSynSending = true; m_bSynRecving = true; m_iFlightFlagSize = 25600; m_iSndBufSize = 8192; m_iRcvBufSize = 8192; m_Linger.l_onoff = 1; m_Linger.l_linger = 180; m_iUDPSndBufSize = 65536; m_iUDPRcvBufSize = m_iRcvBufSize * m_iMSS; m_iIPversion = AF_INET; m_bRendezvous = false; m_iSndTimeOut = -1; m_iRcvTimeOut = -1; m_iConnTimeOut = 15 * 1000; // !nash! follow cannot moved to construct() as copy constructor // below has different behavior for allocating m_pCCFactory and // obtaining m_pController m_pCCFactory = new (nothrow) CCCFactory<CUDTCC>; m_pController = NULL;}CUDT::CUDT(const CUDT& ancestor) throw(){ // Initilize mutex and condition variables initSynch(); // Default UDT configurations m_iMSS = ancestor.m_iMSS; m_bSynSending = ancestor.m_bSynSending; m_bSynRecving = ancestor.m_bSynRecving; m_iFlightFlagSize = ancestor.m_iFlightFlagSize; m_iSndBufSize = ancestor.m_iSndBufSize; m_iRcvBufSize = ancestor.m_iRcvBufSize; m_Linger = ancestor.m_Linger; m_iUDPSndBufSize = ancestor.m_iUDPSndBufSize; m_iUDPRcvBufSize = ancestor.m_iUDPRcvBufSize; m_iSockType = ancestor.m_iSockType; m_iIPversion = ancestor.m_iIPversion; m_bRendezvous = ancestor.m_bRendezvous; m_iSndTimeOut = ancestor.m_iSndTimeOut; m_iRcvTimeOut = ancestor.m_iRcvTimeOut; m_iConnTimeOut = ancestor.m_iConnTimeOut; m_pCCFactory = ancestor.m_pCCFactory->clone(); m_pController = ancestor.m_pController;}bool CUDT::construct() throw(){ if (!m_pCCFactory) return false; m_pSndBuffer = NULL; m_pRcvBuffer = NULL; m_pSndLossList = NULL; m_pRcvLossList = NULL; m_pACKWindow = NULL; m_pSndTimeWindow = NULL; m_pRcvTimeWindow = NULL; m_pSndQueue = NULL; m_pRcvQueue = NULL; m_pPeerAddr = NULL; m_pSNode = NULL; m_pRNode = NULL; m_pCC = NULL; // Initial status m_bOpened = false; m_bListening = false; m_bConnected = false; m_bClosing = false; m_bShutdown = false; m_bBroken = false; m_bReuseAddr = true; return true;}CUDT::~CUDT(){ // release mutex/condtion variables destroySynch(); // destroy the data structures delete m_pSndBuffer; delete m_pRcvBuffer; delete m_pSndLossList; delete m_pRcvLossList; delete m_pACKWindow; delete m_pSndTimeWindow; delete m_pRcvTimeWindow; delete m_pCCFactory; delete m_pCC; delete m_pPeerAddr; delete m_pSNode; delete m_pRNode;}CUDTException CUDT::setOpt(UDTOpt optName, const void* optval, const int){ CGuard cg(m_ConnectionLock); CGuard sendguard(m_SendLock); CGuard recvguard(m_RecvLock); switch (optName) { case UDT_MSS: if (m_bOpened) return CUDTException(5, 1, 0); if (*(int*)optval < int(28 + sizeof(CHandShake))) return CUDTException(5, 3, 0); m_iMSS = *(int*)optval; // Packet size cannot be greater than UDP buffer size if (m_iMSS > m_iUDPSndBufSize) m_iMSS = m_iUDPSndBufSize; if (m_iMSS > m_iUDPRcvBufSize) m_iMSS = m_iUDPRcvBufSize; break; case UDT_SNDSYN: m_bSynSending = *(bool *)optval; break; case UDT_RCVSYN: m_bSynRecving = *(bool *)optval; break; case UDT_CC: if (m_bConnected) return CUDTException(5, 1, 0); if (NULL != m_pCCFactory) delete m_pCCFactory; m_pCCFactory = ((CCCVirtualFactory *)optval)->clone(); break; case UDT_FC: if (m_bConnected) return CUDTException(5, 2, 0); if (*(int*)optval < 1) return CUDTException(5, 3); m_iFlightFlagSize = *(int*)optval; break; case UDT_SNDBUF: if (m_bOpened) return CUDTException(5, 1, 0); if (*(int*)optval <= 0) return CUDTException(5, 3, 0); m_iSndBufSize = *(int*)optval / (m_iMSS - 28); break; case UDT_RCVBUF: if (m_bOpened) return CUDTException(5, 1, 0); if (*(int*)optval <= 0) return CUDTException(5, 3, 0); // Mimimum recv buffer size is 32 packets if (*(int*)optval > (m_iMSS - 28) * 32) m_iRcvBufSize = *(int*)optval / (m_iMSS - 28); else m_iRcvBufSize = 32; break; case UDT_LINGER: m_Linger = *(linger*)optval; break; case UDP_SNDBUF: if (m_bOpened) return CUDTException(5, 1, 0); m_iUDPSndBufSize = *(int*)optval; if (m_iUDPSndBufSize < m_iMSS) m_iUDPSndBufSize = m_iMSS; break; case UDP_RCVBUF: if (m_bOpened) return CUDTException(5, 1, 0); m_iUDPRcvBufSize = *(int*)optval; if (m_iUDPRcvBufSize < m_iMSS) m_iUDPRcvBufSize = m_iMSS; break; case UDT_RENDEZVOUS: if (m_bConnected) return CUDTException(5, 1, 0); m_bRendezvous = *(bool *)optval; break; case UDT_SNDTIMEO: m_iSndTimeOut = *(int*)optval; break; case UDT_RCVTIMEO: m_iRcvTimeOut = *(int*)optval; break; case UDT_REUSEADDR: if (m_bOpened) return CUDTException(5, 1, 0); m_bReuseAddr = *(bool*)optval; break; case UDT_CONNTIMEOUT: m_iConnTimeOut = *(int*)optval; if (m_iConnTimeOut < 3000) // !nash! minimum allowed connection timeout is 3000ms m_iConnTimeOut = 3000; break; default: return CUDTException(5, 0, 0); } return CUDTException();}CUDTException CUDT::getOpt(UDTOpt optName, void* optval, int& optlen){ CGuard cg(m_ConnectionLock); switch (optName) { case UDT_MSS: *(int*)optval = m_iMSS; optlen = sizeof(int); break; case UDT_SNDSYN: *(bool*)optval = m_bSynSending; optlen = sizeof(bool); break; case UDT_RCVSYN: *(bool*)optval = m_bSynRecving; optlen = sizeof(bool); break; case UDT_CC: if (!m_bOpened) return CUDTException(5, 5, 0); *(CCC**)optval = m_pCC; optlen = sizeof(CCC*); break; case UDT_FC: *(int*)optval = m_iFlightFlagSize; optlen = sizeof(int); break; case UDT_SNDBUF: *(int*)optval = m_iSndBufSize * (m_iMSS - 28); optlen = sizeof(int); break; case UDT_RCVBUF: *(int*)optval = m_iRcvBufSize * (m_iMSS - 28); optlen = sizeof(int); break; case UDT_LINGER: if (optlen < (int)(sizeof(linger))) return CUDTException(5, 3, 0); *(linger*)optval = m_Linger; optlen = sizeof(linger); break; case UDP_SNDBUF: *(int*)optval = m_iUDPSndBufSize; optlen = sizeof(int); break; case UDP_RCVBUF: *(int*)optval = m_iUDPRcvBufSize; optlen = sizeof(int); break; case UDT_RENDEZVOUS: *(bool *)optval = m_bRendezvous; optlen = sizeof(bool); break; case UDT_SNDTIMEO: *(int*)optval = m_iSndTimeOut; optlen = sizeof(int); break; case UDT_RCVTIMEO: *(int*)optval = m_iRcvTimeOut; optlen = sizeof(int); break; case UDT_REUSEADDR: *(bool *)optval = m_bReuseAddr; optlen = sizeof(bool); break; case UDT_CONNTIMEOUT: *(int*)optval = m_iConnTimeOut; optlen = sizeof(int); break; default: return CUDTException(5, 0, 0); } return CUDTException();}CUDTException CUDT::open(){ CGuard cg(m_ConnectionLock); // Initial sequence number, loss, acknowledgement, etc. m_iPktSize = m_iMSS - 28; m_iPayloadSize = m_iPktSize - CPacket::m_iPktHdrSize; m_iEXPCount = 1; m_iBandwidth = 1; m_iDeliveryRate = 16; m_iAckSeqNo = 0; m_ullLastAckTime = 0; // trace information m_StartTime = CTimer::getTime(); m_llSentTotal = m_llRecvTotal = m_iSndLossTotal = m_iRcvLossTotal = m_iRetransTotal = m_iSentACKTotal = m_iRecvACKTotal = m_iSentNAKTotal = m_iRecvNAKTotal = 0; m_LastSampleTime = CTimer::getTime(); m_llTraceSent = m_llTraceRecv = m_iTraceSndLoss = m_iTraceRcvLoss = m_iTraceRetrans = m_iSentACK = m_iRecvACK = m_iSentNAK = m_iRecvNAK = 0; // structures for queue if (NULL == m_pSNode) { m_pSNode = new (nothrow) CSNode; if (!m_pSNode) return CUDTException(3, 2, 0); } m_pSNode->m_pUDT = this; m_pSNode->m_llTimeStamp = 1; m_pSNode->m_iHeapLoc = -1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -