Пример #1
0
int CwxAppFramework::noticeTcpListen(CWX_UINT32 uiSvrId,
                                  char const* szAddr,
                                  CWX_UINT16 unPort,
                                  bool bRawData,
                                  CWX_UINT16 unMode,
                                  CWX_NET_SOCKET_ATTR_FUNC fn,
                                  void* fnArg,
                                  CWX_INT32  iFamily)
{
    if (uiSvrId < SVR_TYPE_USER_START)
    {
        CWX_ERROR(("svr-id must not less than SVR_TYPE_USER_START"));
        return -1;
    }

    CWX_UINT32 uiListenId = 0;
    CwxINetAddr inetAddr;
    CwxAppTcpAcceptor* acceptor=NULL;
    uiListenId = m_pListenMgr->getNextListenId();
    acceptor = new CwxAppTcpAcceptor(this,
        reactor(),
        szAddr,
        unPort,
        uiSvrId,
        uiListenId,
        bRawData,
        unMode,
        fn,
        fnArg);
    int ret = 0;
    if (strcmp(szAddr, "*") == 0){
        if (AF_UNSPEC == iFamily) iFamily = AF_INET;
        ret = inetAddr.set(unPort, iFamily);
    } else {
        ret = inetAddr.set(unPort, szAddr, iFamily);
    }
    if (0 != ret) {
      CWX_ERROR(("Address is invalid, tcp listen for ip=%s, port=%u, errno=%d",
        szAddr,
        unPort,
        errno));
      return -1;
    }
    //register the acceptor
    if (acceptor->accept(inetAddr) != 0)
    {
        CWX_ERROR(("Can't open tcp listen for ip=%s, port=%u, errno=%d",
            szAddr,
            unPort,
            errno));
        acceptor->close();
        return -1;
    }
    if (0 != acceptor->open())
    {
        CWX_ERROR(("Failure to register tcp listen for ip=%s, port=%u", szAddr, unPort));
        acceptor->close();
        return -1;
    }
    //create and init the notice object.
    CwxAppNotice* notice = new CwxAppNotice();
    notice->m_unNoticeType = CwxAppNotice::TCP_LISTEN;
    notice->m_noticeArg = acceptor;
    if (0 != m_pReactor->notice(notice))
    {
        acceptor->close();
        delete notice;
        CWX_ERROR(("Failure to notice TCP listen by PIPE"));
        return -1;
    }
    return (int)uiListenId;
}
Пример #2
0
int UnistorHandler4Trans::rebuildConn(UnistorApp* app){
    if (!m_strMasterHost.length()){
        CWX_INFO(("UnistorHandler4Trans: Master is empty, refuse to connect trans connection."));
    }
    if (!m_bRebuildConn) return 0;
    CWX_UINT32 ttNow = time(NULL);
    if (m_ttLastRebuildConn + 2 > ttNow){
        return 0;
    }
    m_ttLastRebuildConn = ttNow;

    m_bCanTrans = false;
    ///关闭已有的所有连接
    UnistorHandler4Trans* handle = NULL;
    map<CWX_UINT32, UnistorHandler4Trans*>::iterator iter = m_handlers->begin();
    while(iter != m_handlers->end()){
        handle = iter->second;
        m_handlers->erase(iter);
        handle->close();
        iter = m_handlers->begin();
    }
    m_uiAuthConnNum = 0;
    CWX_INFO(("UnistorHandler4Trans:Rebuild trans connection to %s:%u, user:passwd=%s:%s",
        m_strMasterHost.c_str(),
        app->getConfig().getRecv().getPort(),
        app->getConfig().getRecv().getUser().length()?app->getConfig().getRecv().getUser().c_str():"",
        app->getConfig().getRecv().getUser().length()?app->getConfig().getRecv().getUser().c_str():""));
    ///重建所有连接
    CwxINetAddr addr;
    if (0 != addr.set(app->getConfig().getRecv().getPort(), m_strMasterHost.c_str())){
        CWX_ERROR(("Failure to init addr, addr:%s, port:%u, err=%d", m_strMasterHost.c_str(), app->getConfig().getRecv().getPort(), errno));
        return -1;
    }
    CWX_UINT32 i=0;
    int* fds = new int[app->getConfig().getCommon().m_uiTranConnNum];
    for (i=0; i<app->getConfig().getCommon().m_uiTranConnNum; i++){
        fds[i] = -1;
    }
    CwxTimeValue timeout(UNISTOR_CONN_TIMEOUT_SECOND);
    CwxTimeouter timeouter(&timeout);
    if (0 != UnistorConnector::connect(addr,
        app->getConfig().getCommon().m_uiTranConnNum,
        fds,
        &timeouter,
        true,
        UnistorApp::setConnSockAttr,
        app->getRecvSockAttr()))
    {
        CWX_ERROR(("Failure to connect to addr:%s, port:%u, err=%d",m_strMasterHost.c_str(), app->getConfig().getRecv().getPort(), errno)); 
        return -1;
    }
    ///将连接注册到channel
    CwxAppChannel* channel = app->getTransChannel();
    for (i=0; i<app->getConfig().getCommon().m_uiTranConnNum; i++){
        if (channel->isRegIoHandle(fds[i])){
            CWX_ERROR(("Handler[%] is register", fds[i]));
            break;
        }
        UnistorHandler4Trans* pHandler = new UnistorHandler4Trans(app,
            app->reactor()->getNextConnId(),
            channel);
        pHandler->setHandle(fds[i]);
        if (0 != pHandler->open()){
            pHandler->setHandle(-1);
            delete pHandler;
            break;
        }
        pHandler->m_tss = (UnistorTss*)CwxTss::instance(); ///<对象对应的tss对象

    }
    if (i < app->getConfig().getCommon().m_uiTranConnNum){
        for (CWX_UINT32 j=i; j<app->getConfig().getCommon().m_uiTranConnNum; j++){
            ::close(fds[j]);
        }
        return -1;
    }
    m_bRebuildConn = false;
    return 0;
}