cmpreservation.c

来自「基于h323协议的软phone」· C语言 代码 · 共 1,152 行 · 第 1/3 页

C
1,152
字号

            /* see is this call is preserved */
            if(m_callget(call, preservedCall))
            {
                delCall(app, call);
                return 0;
            }
            else
            {
                /* call was already activated. big error. */
                return RV_ERROR_UNKNOWN;
            }
        }
    }
    return 0;
}


RVAPI RvStatus RVCALLCONV RvH323HaActivateCalls(
    IN HAPP                       hApp,
    IN RvH323HaActivateCallCB     activateCallCB,
    IN RvH323HaActivateChannelCB  activateChanCB)
{
    cmElem * app = (cmElem *) hApp;
    EMAElement emaElem = NULL, nextElem = NULL;
    callElem * call;

    emaElem = emaGetNext(app->hCalls, emaElem);

    /* go over all the calls in the EMA */
    while(emaElem != NULL)
    {
        nextElem = emaGetNext(app->hCalls, emaElem);
        call = (callElem *) emaElem;

        /* see is this call is preserved */
        if(m_callget(call, preservedCall))
        {
            cmTransSession * session = (cmTransSession *)call->hsTransSession;
            RvH323HaHOST *q931=NULL, *h245=NULL, *annexE=NULL;
            catStruct callObj;
            RvBool okCall = RV_TRUE;
            RvBool lockedCall = RV_FALSE;

            /* mark as no longer preserved */
            m_callset(call, preservedCall, RV_FALSE);

            /* get pointers to hosts, if needed */
            if(session)
            {
                if(session->annexEConnection) annexE = (RvH323HaHOST *)&session->annexEConnection;
                else annexE = NULL;
                if(session->Q931Connection)   q931 =   (RvH323HaHOST *)&session->Q931Connection;
                else q931 = NULL;
                if(session->H245Connection)   h245 =   (RvH323HaHOST *)&session->H245Connection;
                else h245 = NULL;
            }

            /* CAT changes */
            if((callObjectCreate(app, (HCALL)call, Incoming, &callObj) < 0) ||
                ((call->hCatCall = catAdd(app->hCat, &callObj, (HCALL)call))==NULL))
            {
                call->hCatCall = NULL;
                okCall = RV_FALSE;
            }

            if( (!okCall) ||
                (activateCallCB(hApp, (HCALL)call, (HAPPCALL)emaGetApplicationHandle(emaElem), call->state, call->stateMode,
                    annexE, q931, h245) < 0)  ||
                ((lockedCall = emaLock(emaElem)) == RV_FALSE) )
            {
                okCall = RV_FALSE;
                session->annexEConnection = session->Q931Connection = session->H245Connection = NULL;
            }
            else
            {
                /* connect the session to the hosts */
                if(annexE)
                {
                    cmTransHost * host = (cmTransHost *) *annexE;

                    if (session != host->firstSession)
                    {
                        session->nextSession = host->firstSession;
                        if (session->nextSession)
                            session->nextSession->prevSession = session;
                        host->firstSession = session;
                    }
                    else
                        printf("cmpreservation(1):insert same seesion as firstSession:%x\n", session);
                }

                if(q931)
                {
                    cmTransHost * host = (cmTransHost *) *q931;

                    if (session != host->firstSession)
                    {
                        session->nextSession = host->firstSession;
                        if (session->nextSession)
                            session->nextSession->prevSession = session;
                        host->firstSession = session;
                    }
                    else
                        printf("cmpreservation(2):insert same seesion as firstSession:%x\n", session);
                }

                if(h245)
                {
                    cmTransHost * host = (cmTransHost *) *h245;

                    host->firstSession = session;
                }
            }

            if(session->hashEntry)
            {
                /* add the new session to the hash table */
                cmTransGlobals * transGlobals = (cmTransGlobals *)app->hTransport;
                void * loc;
                sessionKey key;

                key.CRV  = session->CRV;
                if( !okCall ||
                    ((loc = hashAdd(transGlobals->hHashSessions,(void *)&key,(void *)session, RV_FALSE)) == NULL) )
                {
                    okCall = RV_FALSE;
                    session->hashEntry = NULL;
                }
                else
                    session->hashEntry = loc;
            }

            if(okCall)
            {
                H245Channel * curChan;
                HAPPCHAN      haChan;

                /* start activating the channels */
                curChan = (H245Channel *) cmiGetChannelListForCall((HCALL)call);
                while(curChan != NULL)
                {
                    activateChanCB(hApp, (HCALL)call, (HAPPCALL)emaGetApplicationHandle(emaElem),
                        (HCHAN) curChan, &haChan);
                    emaSetApplicationHandle((EMAElement)curChan, (void*)haChan);

                    curChan = curChan->pNext;
                }
            }

            if(lockedCall)
                emaUnlock(call);

            if(!okCall)
            {
                delCall(app, call);
            }
        }
        emaElem = nextElem;
    }
    return RV_OK;
}


RVAPI RvStatus RVCALLCONV RvH323HaGetAnnexEHost(
    IN  HAPP                hApp,
    IN  HCALL               hsCall,
    IN  cmTransportAddress  *remoteAddress,
    OUT RvH323HaHOST        *hHost)
{
    cmElem * app = (cmElem *) hApp;
    cmTransGlobals *transGlobals = (cmTransGlobals *)app->hTransport;
    callElem * call = (callElem *) hsCall;
    cmTransHost *host = NULL;

    if(hHost == NULL) return RV_ERROR_BADPARAM;

    /* Try first to look for an existing one in the hash */
    host = findHostInHash(transGlobals,  remoteAddress, RV_TRUE, RV_TRUE);

    /* No host, we must create a new one (should we???) */
    if (!host)
    {
        TRANSERR res = createHostByType(transGlobals, call->hsTransSession, (HSTRANSHOST *)&host,
            cmTransQ931Conn, NULL, cmTransUseAnnexE);

        if (res != cmTransOK)
        {
            *hHost = NULL;
            return RV_ERROR_UNKNOWN;
        }
        else
        {
            /* Write the new host to the hash and get its local address */
            emaSetApplicationHandle(host, transGlobals);
            setRemoteAddress(host, remoteAddress);
            annexEGetLocalAddress(transGlobals->annexECntrl, &host->localAddress);
        }
    }

    *hHost = (RvH323HaHOST) host;
    return RV_OK;
}

RVAPI int RVCALLCONV RvH323HaGetMuxHost(
    IN  HAPP                hApp,
    IN  HCALL               hsCall,
    IN  HCALL               hsSameConnectionCall,
    IN  cmTransportAddress  *remoteAddress,
    OUT RvH323HaHOST        *hHost)
{
    cmElem * app = (cmElem *) hApp;
    cmTransGlobals *transGlobals = (cmTransGlobals *)app->hTransport;
    callElem * call = (callElem *) hsCall;
    callElem * sameConnectionCall = (callElem *) hsSameConnectionCall;
    cmTransHost *host = NULL;

    if(hHost == NULL) return RV_ERROR_NULLPTR;

    /* see if a connection is specified */
    if(sameConnectionCall)
    {
        if (emaLock((EMAElement)hsCall))
        {
            if (emaLock((EMAElement)hsSameConnectionCall))
            {
                int value=RV_TRUE;
                cmTransSession * session = (cmTransSession *)sameConnectionCall->hsTransSession;

                host = session->Q931Connection;
                if (host->isMultiplexed &&
                    (host->remoteAddress.ip == remoteAddress->ip) &&
                    (host->remoteAddress.port == remoteAddress->port) )
                {
                    cmTransSetSessionParam(call->hsTransSession,cmTransParam_isMultiplexed,&value);
                }
                else
                    host = NULL;

                emaUnlock((EMAElement)hsSameConnectionCall);
            }
            emaUnlock((EMAElement)hsCall);
        }
    }

    if(!host)
        /* Try to look for an existing one in the hash */
        host = findHostInHash(transGlobals,  remoteAddress, RV_TRUE, RV_FALSE);

    *hHost = (RvH323HaHOST) host;
    return RV_OK;
}

void RvH323HaAcceptHandler(HTPKT tpkt, RvSelectEvents event, int length, int error, void* context)
{
    RV_UNUSED_ARG(length);
    RV_UNUSED_ARG(error);
    RV_UNUSED_ARG(context);

    if ((event == RvSelectAccept) || (event == RvSelectConnect))
    {
        cmTransHost * host = (cmTransHost *)emaGetApplicationHandle((EMAElement)tpkt);
        cmTransGlobals *transGlobals = (cmTransGlobals *)emaGetUserData((EMAElement)host);
        RvH323Connection* connection = tpktGetConnection(tpkt);
        hostKey key;

        if (event == RvSelectAccept)
        {
            RvSocketAccept(&connection->socket, &connection->socket, NULL);
        }

        host->state = hostConnected;

        /* set the new local address of the host */
        tpktGetAddresses(tpkt, &host->localAddress, &host->remoteAddress);

        if(host->type == RvH323ConnectionQ931)
        {
            /* add to hash */
            key.ip   = host->remoteAddress.ip;
            key.port = (RvUint32)host->remoteAddress.port;
            key.type = (host->annexEUsageMode != cmTransNoAnnexE);

            host->hashEntry = hashAdd(transGlobals->hHashHosts,(void *)&key, (void *)&host, RV_FALSE);

            /* register a new callback */
            tpktRecvNotify(tpkt, transQ931Handler, host);
        }
        if(host->type == RvH323ConnectionH245)
        {
            /* register a new callback */
            tpktRecvNotify(tpkt, transH245Handler, host);
        }
    }
}


RVAPI int RVCALLCONV RvH323HaGetTPKTHost(
    IN    HAPP                hApp,
    IN    RvBool              bQ931Host,
    IN    cmTransportAddress  *remoteAddress,
    INOUT cmTransportAddress  *localAddress,
    OUT   RvH323HaHOST        *hHost)
{
    cmElem* app = (cmElem*) hApp;
    cmTransGlobals * transGlobals = (cmTransGlobals *)app->hTransport;
    HTPKT hTpkt;
    TRANSERR res;
    cmTransHost* host;
    RvH323ConnectionType connType;

    if (hHost == NULL) return RV_ERROR_NULLPTR;
    if ((remoteAddress != NULL) && (localAddress == NULL)) return RV_ERROR_NULLPTR;

    if (bQ931Host)
    {
        connType = RvH323ConnectionQ931;
    }
    else
    {
        connType = RvH323ConnectionH245;
    }

    /* create host */
    res = cmTransCreateHost((HAPPTRANS)transGlobals, NULL, (HSTRANSHOST*) &host);
    if (res != cmTransOK)
    {
        return RV_ERROR_UNKNOWN;
    }

    /* initialize */
    memset(host, 0, sizeof(cmTransHost));
    host->annexEUsageMode = cmTransNoAnnexE;
    host->closeOnNoSessions = RV_TRUE;
    host->hostIsApproved = RV_TRUE;
    host->remoteCloseOnNoSessions = RV_TRUE;
    host->reported = RV_TRUE;
    host->type = cmTransQ931Conn;

    /* see if we got a remote address to connect to */
    if(remoteAddress != NULL)
    {
        /* connect to the remote address */
        host->state = hostConnecting;
        host->incoming = RV_FALSE;

        /* open a connection and stuff */
        hTpkt = tpktOpen(transGlobals->tpktCntrl, localAddress, connType,
            tpktClient, RvH323HaAcceptHandler, (void *)host);
        if(hTpkt == NULL)
        {
            emaDelete(host);
            return RV_ERROR_UNKNOWN;
        }

        tpktConnect(hTpkt, remoteAddress);
    }
    else
    {
        /* just listen */
        host->state = hostListenning;
        host->incoming = RV_TRUE;

        /* open a connection and stuff */
        hTpkt = tpktOpen(transGlobals->tpktCntrl, localAddress, connType,
            tpktServer, RvH323HaAcceptHandler, (void *)host);
        if(hTpkt == NULL)
        {
            emaDelete(host);
            return RV_ERROR_UNKNOWN;
        }
    }

    host->hTpkt = hTpkt;
    *hHost = (RvH323HaHOST) host;
    return RV_OK;
}


#ifdef __cplusplus
}
#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?