socket.cpp
来自「可用该程序将avi的电影文件转化为TS流」· C++ 代码 · 共 1,306 行 · 第 1/3 页
CPP
1,306 行
strLocalName = pszDst; else if(errno != ENOSPC) throw E_Socket("Unable to get name of the intf: " + GetErrorMsg(SYSTEM_NETWORK)); } }#endif else { ASSERT(false); } return strLocalName;}//------------------------------------------------------------------------------////------------------------------------------------------------------------------C_String C_Socket::GetLocalPort() const{ C_String strLocalPort("Unknown"); if(m_iDomain == AF_INET) { struct sockaddr_in saIntf; socklen_t iNameLen = sizeof(saIntf); int iRc = getsockname(m_hSocket, (struct sockaddr*)&saIntf, &iNameLen); ASSERT(iNameLen == sizeof(saIntf)); if(iRc) { throw E_Socket("Unable to get port of the intf: " + GetErrorMsg(SYSTEM_NETWORK)); } else strLocalPort = ntohs(saIntf.sin_port); }#ifdef HAVE_IPV6 else if(m_iDomain == AF_INET6) { struct sockaddr_in6 saIntf; socklen_t iNameLen = sizeof(saIntf); int iRc = getsockname(m_hSocket, (struct sockaddr*)&saIntf, &iNameLen); ASSERT(iNameLen == sizeof(saIntf)); if(iRc) { throw E_Socket("Unable to get port of the intf: " + GetErrorMsg(SYSTEM_NETWORK)); } else strLocalPort = ntohs(saIntf.sin6_port); }#endif else { ASSERT(false); } return strLocalPort;}//------------------------------------------------------------------------------////------------------------------------------------------------------------------C_String C_Socket::GetPeerName() const{ C_String strPeerName("Unknown"); if(m_iDomain == AF_INET) { struct sockaddr_in saPeer; socklen_t iNameLen = sizeof(saPeer); int iRc = getpeername(m_hSocket, (struct sockaddr*)&saPeer, &iNameLen); ASSERT(iNameLen == sizeof(saPeer)); if(iRc) { throw E_Socket("Unable to get name of the peer: " + GetErrorMsg(SYSTEM_NETWORK)); } else strPeerName = inet_ntoa(saPeer.sin_addr); }#ifdef HAVE_IPV6 else if(m_iDomain == AF_INET6) { struct sockaddr_in6 saPeer; socklen_t iNameLen = sizeof(saPeer); int iRc = getpeername(m_hSocket, (struct sockaddr*)&saPeer, &iNameLen); ASSERT(iNameLen == sizeof(saPeer)); if(iRc) { throw E_Socket("Unable to get name of the peer: " + GetErrorMsg(SYSTEM_NETWORK)); } else { char pszDst[40]; // 40 characters is enough for an IPv6 address if(inet_ntop(AF_INET6, &saPeer.sin6_addr, pszDst, 40) == pszDst) strPeerName = pszDst; else if(errno != ENOSPC) throw E_Socket("Unable to get name of the intf: " + GetErrorMsg(SYSTEM_NETWORK)); } }#endif else { ASSERT(false); } return strPeerName;}//------------------------------------------------------------------------------////------------------------------------------------------------------------------C_String C_Socket::GetPeerPort() const{ C_String strPeerPort("Unknown"); if(m_iDomain == AF_INET) { struct sockaddr_in saPeer; socklen_t iNameLen = sizeof(saPeer); int iRc = getpeername(m_hSocket, (struct sockaddr*)&saPeer, &iNameLen); ASSERT(iNameLen == sizeof(saPeer)); if(iRc) { throw E_Socket("Unable to get port of the peer: " + GetErrorMsg(SYSTEM_NETWORK)); } else strPeerPort = ntohs(saPeer.sin_port); }#ifdef HAVE_IPV6 if(m_iDomain == AF_INET6) { struct sockaddr_in6 saPeer; socklen_t iNameLen = sizeof(saPeer); int iRc = getpeername(m_hSocket, (struct sockaddr*)&saPeer, &iNameLen); ASSERT(iNameLen == sizeof(saPeer)); if(iRc) { throw E_Socket("Unable to get port of the peer: " + GetErrorMsg(SYSTEM_NETWORK)); } else strPeerPort = ntohs(saPeer.sin6_port); }#endif else { ASSERT(false); } return strPeerPort;}//------------------------------------------------------------------------------////------------------------------------------------------------------------------C_String C_Socket::GetName() const{ C_String strName; try { strName += GetPeerName(); } catch(E_Exception e) { strName = "Unavailable"; } return strName;}//------------------------------------------------------------------------------////------------------------------------------------------------------------------C_String C_Socket::GetInfo() const{ C_String strInfo; try { strInfo += GetPeerName() + "/" + GetPeerPort(); strInfo += "connected to " + GetLocalPort(); } catch(E_Exception e) { strInfo = "Unavailable"; } return strInfo;}//******************************************************************************// //******************************************************************************// //******************************************************************************//------------------------------------------------------------------------------// //------------------------------------------------------------------------------C_SocketPool::C_SocketPool() : m_vRecvSockets(1), m_vSendSockets(1), m_vExceptSockets(1), m_vWaitingSockets(5, false){}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------C_SocketPool::~C_SocketPool(){ // Remove the remaining Sockets from the cWaitingSockets list otherwise they // would be deleted twice //for(unsigned int i = m_vWaitingSockets.Size(); i > 0; i--) // m_vWaitingSockets.Remove(i - 1);}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------void C_SocketPool::AddRecv(C_Socket* pSocket){ m_vRecvSockets.Add(pSocket);}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------C_Socket* C_SocketPool::RemoveRecv(const C_Socket& cSocket){ C_Socket* pSocket = NULL; int iPos = m_vRecvSockets.Find(cSocket); if(iPos != GEN_ERR) pSocket = m_vRecvSockets.Remove(iPos); return pSocket;}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------void C_SocketPool::AddSend(C_Socket* pSocket){ m_vSendSockets.Add(pSocket);}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------C_Socket* C_SocketPool::RemoveSend(const C_Socket& cSocket){ C_Socket* pSocket = NULL; int iPos = m_vSendSockets.Find(cSocket); if(iPos != GEN_ERR) pSocket = m_vSendSockets.Remove(iPos); return pSocket;}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------void C_SocketPool::AddExcept(C_Socket* pSocket){ m_vExceptSockets.Add(pSocket);}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------C_Socket* C_SocketPool::RemoveExcept(const C_Socket& cSocket){ C_Socket* pSocket = NULL; int iPos = m_vExceptSockets.Find(cSocket); if(iPos != GEN_ERR) pSocket = m_vExceptSockets.Remove(iPos); return pSocket;}//------------------------------------------------------------------------------////------------------------------------------------------------------------------C_Socket* C_SocketPool::Monitor(){ return InternalMonitor(NULL);}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------C_Socket* C_SocketPool::Monitor(u32 iTimeOut){ // Init the TimeOut struct timeval sTv; sTv.tv_sec = iTimeOut; sTv.tv_usec = 0; return InternalMonitor(&sTv);}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------C_Socket* C_SocketPool::InternalMonitor(struct timeval* pTimeOut){ // To comment if(m_vWaitingSockets.Size() > 0) { C_Socket* pSocket = m_vWaitingSockets.Remove(m_vWaitingSockets.Size() - 1); return pSocket; } // Select return with error EINTR when a signal is caught: detect it // and retry a select when this occurs int iRc = NO_ERR; do { // Put our pools of sockets in the sets of socket to monitor // Also detect which is the higher handle among all C_Socket::sock_handle m_hHigherSocket = 0; fd_set sRecvSet; FD_ZERO(&sRecvSet); for(unsigned int i = 0; i < m_vRecvSockets.Size(); i++) { if(m_hHigherSocket < m_vRecvSockets[i].m_hSocket) m_hHigherSocket = m_vRecvSockets[i].m_hSocket; FD_SET(m_vRecvSockets[i].m_hSocket, &sRecvSet); } fd_set sSendSet; FD_ZERO(&sSendSet); for(unsigned int j = 0; j < m_vSendSockets.Size(); j++) { if(m_hHigherSocket < m_vSendSockets[j].m_hSocket) m_hHigherSocket = m_vSendSockets[j].m_hSocket; FD_SET(m_vSendSockets[j].m_hSocket, &sSendSet); } fd_set sExceptSet; FD_ZERO(&sExceptSet); for(unsigned int k = 0; k < m_vExceptSockets.Size(); k++) { if(m_hHigherSocket < m_vExceptSockets[k].m_hSocket) m_hHigherSocket = m_vExceptSockets[k].m_hSocket; FD_SET(m_vExceptSockets[k].m_hSocket, &sExceptSet); } // Monitor the sockets if(!pTimeOut) iRc = select(m_hHigherSocket+1, &sRecvSet, &sSendSet, &sExceptSet, NULL); else { struct timeval sTv; memcpy(&sTv, pTimeOut, sizeof(sTv)); iRc = select(m_hHigherSocket+1, &sRecvSet, &sSendSet, &sExceptSet, &sTv); } if(iRc > 0) { // An interesting event occured on a least one socket for(unsigned int i = 0; i < m_vRecvSockets.Size(); i++) { if(FD_ISSET(m_vRecvSockets[i].m_hSocket, &sRecvSet)) { C_Socket* pSocket = &m_vRecvSockets[i]; m_vWaitingSockets.Add(pSocket); } } for(unsigned int j = 0; j < m_vSendSockets.Size(); j++) { if(FD_ISSET(m_vSendSockets[j].m_hSocket, &sSendSet)) { C_Socket* pSocket = &m_vSendSockets[j]; m_vWaitingSockets.Add(pSocket); } } for(unsigned int k = 0; k < m_vExceptSockets.Size(); k++) { if(FD_ISSET(m_vExceptSockets[k].m_hSocket, &sExceptSet)) { C_Socket* pSocket = &m_vExceptSockets[k]; m_vWaitingSockets.Add(pSocket); } } } } while(iRc == -1 && GetErrorCode(SYSTEM_NETWORK) == ERR_INTR); C_Socket* pSocket = NULL; if(iRc < 0) { throw E_Socket("Unable to receive data: " + GetErrorMsg(SYSTEM_NETWORK)); } else if(iRc > 0) { ASSERT(m_vWaitingSockets.Size() > 0); pSocket = m_vWaitingSockets.Remove(m_vWaitingSockets.Size() - 1); } return pSocket;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?