void CPeerLink::HandleClose() { if (m_nPeerState == PS_CONNECTING) { m_nPeerState = PS_CONNECTFAILED; OnConnectFailed(); } if (m_nPeerState == PS_ESTABLISHED) { m_nPeerState = PS_CLOSED; OnConnectClosed(); } if (m_nConnTimeoutID != 0) { GetReactor()->RemoveTimer(m_nConnTimeoutID); m_nConnTimeoutID = 0; } m_bCanRead = false; m_bCanWrite = false; SetReactor(NULL); CWinSocket::Close(); }
int boss::UdpHandlerBase::Open(u_int16_t uiPort, const string &sAddr) { struct sockaddr_in stAddr; memset(&stAddr, 0, sizeof(stAddr)); stAddr.sin_family = AF_INET; stAddr.sin_port = htons((unsigned short)uiPort); if("" == sAddr) { stAddr.sin_addr.s_addr = htonl(INADDR_ANY); } else { stAddr.sin_addr.s_addr = inet_addr(sAddr.c_str()); } int iSocketFD; if((iSocketFD = socket(PF_INET, SOCK_DGRAM, 0)) < 0) { BOSS_ERROR("socket Error: %s", strerror(errno)); return -1; } u_int32_t uiRecvBufSize = 40 * 100000; uint32_t dwLen = sizeof(uiRecvBufSize); if (setsockopt(iSocketFD, SOL_SOCKET, SO_RCVBUF, (int *)&uiRecvBufSize, dwLen) == -1) { BOSS_ERROR("setsockopt Error: %s", strerror(errno)); close(iSocketFD); return -1; } int iFlags; iFlags = fcntl(iSocketFD, F_GETFL, 0); iFlags |= O_NONBLOCK; iFlags |= O_NDELAY; fcntl(iSocketFD, F_SETFL, iFlags); if(bind(iSocketFD, (struct sockaddr*)&stAddr, sizeof(stAddr)) < 0) { BOSS_ERROR("bind Error: %s", strerror(errno)); close(iSocketFD); return -1; } SetHandler(iSocketFD); GetReactor()->RegisterHandler(this, MASK_INPUT); return 0; }
void CPeerLink::Connect( const char *IpAddr, int nPort ) { CWinSocket::CreateTCPSocket(); CWinSocket::SetReactor(m_pPeerManager->GetTorrentTask()->GetSocketReactor()); m_strIPAddr = IpAddr; m_nPort = nPort; CWinSocket::SetHandleMask(WRITE_MASK); CWinSocket::Connect(IpAddr, nPort); m_nPeerState = PS_CONNECTING; m_nConnTimeoutID = GetReactor()->AddTimer(this, 10000, true); }