Exemple #1
0
int CServerLinker::Link(void)
{
    closeSock();

    int iRtn = Q_RTN_OK;
    CNETAddr objAddr;
    int iKeepAlive = 1;

    iRtn = objAddr.setAddr(m_strDesIp.c_str(), m_usDesPort);
    if (Q_RTN_OK != iRtn)
    {
        return iRtn;
    }

    m_Sock = socket(AF_INET, SOCK_STREAM, 0);
    if (Q_INVALID_SOCK == m_Sock)
    {
        iRtn = Q_SockError();
        Q_Printf("create socket error. error code %d, message %s", 
            iRtn, Q_SockError2Str(iRtn));

        return iRtn;
    }

    iRtn = connect(m_Sock, objAddr.getAddr(), objAddr.getAddrSize());
    if (Q_RTN_OK != iRtn)
    {
        iRtn = Q_SockError();
        Q_Printf("connect %s on port %d error. error code %d, message %s", 
            objAddr.getIp().c_str(), objAddr.getPort(), iRtn, Q_SockError2Str(iRtn));

        return iRtn;
    }

    (void)setsockopt(m_Sock, SOL_SOCKET, SO_KEEPALIVE, (char *)&iKeepAlive, sizeof(iKeepAlive));

    return Q_RTN_OK;
}
Exemple #2
0
void CNetListener::onAdjure(CAdjure *pAdjure)
{
    CNETAddr objAddr;
    CNetInfo *pNetInfo(((CAddListenAdjure *)pAdjure)->getListenInfo());    
    if (H_RTN_OK != objAddr.setAddr(pNetInfo->getHost(), pNetInfo->getPort(), pNetInfo->isIpV6()))
    {
        H_SafeDelete(pNetInfo);
        H_LOG(LOGLV_ERROR, "parse host %s port %d error.", pNetInfo->getHost(), pNetInfo->getPort());
        return;
    }

    struct evconnlistener *pListener(evconnlistener_new_bind((struct event_base *)getLoop(), acceptCB, pNetInfo,
        LEV_OPT_CLOSE_ON_FREE | LEV_OPT_CLOSE_ON_EXEC, -1, objAddr.getAddr(), (int)objAddr.getAddrSize()));
    if (NULL == pListener)
    {
        H_SafeDelete(pNetInfo);
        H_LOG(LOGLV_ERROR, "%s", "evconnlistener_new_bind error.");
        return;
    }    

    H_LOG(LOGLV_SYS, "listen at host %s port %d", pNetInfo->getHost(), pNetInfo->getPort());
    m_vcListener.push_back(pListener);
    m_vcNetInfo.push_back(pNetInfo);
}