⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 annexe.c

📁 基于h323协议的软phone
💻 C
字号:
/***********************************************************************
        Copyright (c) 2002 RADVISION Ltd.
************************************************************************
NOTICE:
This document contains information that is confidential and proprietary
to RADVISION Ltd.. No part of this document may be reproduced in any
form whatsoever without written prior approval by RADVISION Ltd..
RADVISION Ltd. reserve the right to revise this publication and make
changes without obligation to notify any person of such revisions or
changes.
***********************************************************************/


#include "rvinternal.h"
#include "hash.h"
#include "annexe.h"
#include "iannexe.h"
#include "cmutils.h"

#ifdef __cplusplus
extern "C" {
#endif

void free_resources(tAnnexE* pAnnexE);

void annexEEvent(
    IN RvSelectEngine*  selectEngine,
    IN RvSelectFd*      fd,
    IN RvSelectEvents   selectEvent,
    IN RvBool           error);


/* package init & done functions
 ****************************************************************************
 ***************************************************************************/

annexEStatus annexEInit(
    OUT HANNEXE*                hAnnexE,
    IN  HAPPANNEXE              hAppAnnexE,
    IN  int                     nMaxNodes,
    IN  int                     nMaxSupportedMessageSize,
    IN  int                     nMaxStoredPackets,
    IN  RvH323TimerPoolHandle   timersPool,
    IN  RvSelectEngine*         selEngine,
    IN  RvPortRange*            portRange)
{

    tAnnexE*    pAnnexE;
    int         i;

    if (hAnnexE == NULL)
        return annexEStatusBadParameter;

    *hAnnexE = NULL;

    /* allocate annex E context */
    if(RvMemoryAlloc(NULL, (void**)&pAnnexE, sizeof(tAnnexE)) != RV_OK)
    {
        return annexEStatusMemoryProblem;
    }
    memset(pAnnexE, 0, sizeof(tAnnexE));

    /* initialize tAnnexE */
    pAnnexE->State = NOT_INITIALIZED;

    RvLogSourceConstruct(RvLogGet(), &pAnnexE->log, RV_LOG_LIBCODE_H323, "AnnexE", "H323 AnnexE module");

    RvLogInfo(&pAnnexE->log,
        (&pAnnexE->log, "annexEInit(MaxSes:%i, MaxMsgSize:%i, MaxPDU's:%i) Begin",
            nMaxNodes, nMaxSupportedMessageSize, nMaxStoredPackets));

    InitializeListHead(&pAnnexE->FreeNodesList);
    InitializeListHead(&pAnnexE->FreePDUList);
    InitializeListHead(&pAnnexE->WaitingForTransmissionList);
    InitializeListHead(&pAnnexE->ResBlockedNodeList);

    pAnnexE->t_R1   = ANNEXE_TR1;
    pAnnexE->t_R2   = ANNEXE_TR1;
    pAnnexE->n_R1   = ANNEXE_NR1;
    pAnnexE->t_IMA1 = ANNEXE_TIMA1;
    pAnnexE->n_IMA1 = ANNEXE_NIMA1;
    pAnnexE->t_DT   = ANNEXE_TR1 / 4;

    pAnnexE->ActiveNodesHash = hashConstruct(nMaxNodes + 1,
                                             nMaxNodes,
                                             hashstr,
                                             hashDefaultCompare,
                                             sizeof_tNodeKey,
                                             sizeof(tNode*),
                                             "Annex E nodes header");

    if (pAnnexE->ActiveNodesHash == NULL)
    {
        RvLogError(&pAnnexE->log, (&pAnnexE->log, "annexEInit() - MemoryProblem (ActiveNodes hash)!"));
        RvLogSourceDestruct(RvLogGet(), &pAnnexE->log);
        RvMemoryFree(pAnnexE);
        return annexEStatusMemoryProblem;
    }

    pAnnexE->hAppAnnexE = hAppAnnexE;

    /* initialize tNode's */
    for (i = 0; i < nMaxNodes; i++)
    {
        tNode*      pNode;

        /* allocate tNode */
        if (RvMemoryAlloc(NULL, (void**)&pNode, sizeof(tNode)) != RV_OK)
        {
            RvLogError(&pAnnexE->log, (&pAnnexE->log, "annexEInit() - MemoryProblem (nodes)!"));
            free_resources(pAnnexE);
            return annexEStatusMemoryProblem;
        }

        /* initialize tNode */
        memset(pNode, 0, sizeof(tNode));

        pNode->pAnnexE = pAnnexE;

        pNode->hResendAndIMATimer = NULL;

        /* append tNode to FreeNodesList */
        InsertTailList(&pAnnexE->FreeNodesList, &pNode->lNodeLink);
    }

    /* initialize PDU's */
    for (i = 0; i < nMaxStoredPackets; i++) {
        tPDU*       pPDU;
        int         nPDUmemsize = sizeof(tPDU) - sizeof_IAnnexEHeader + nMaxSupportedMessageSize;

        /* allocate tPDU;*/
        if(RvMemoryAlloc(NULL, (void**)&pPDU, nPDUmemsize) != RV_OK)
        {
            RvLogError(&pAnnexE->log, (&pAnnexE->log, "annexEInit() - MemoryProblem (send PDU's)!"));
            free_resources(pAnnexE);
            return annexEStatusMemoryProblem;
        }

        /* initialize tPDU;*/
        memset(pPDU, 0, nPDUmemsize);
        pPDU->nMaxSize = nMaxSupportedMessageSize;

        /* append tPDU to FreePDUList   */
        InsertTailList(&pAnnexE->FreePDUList, &pPDU->lPDULink);
    }

    /* initialize pRecvPDU! */
    if(RvMemoryAlloc(NULL, (void**)&pAnnexE->pRecvPDU,
        sizeof(tPDU)-sizeof_IAnnexEHeader+nMaxSupportedMessageSize/*MAX_UDP_FRAME*/) != RV_OK)
    {
        RvLogError(&pAnnexE->log, (&pAnnexE->log, "annexEInit() - MemoryProblem (recv PDU)!"));
        free_resources(pAnnexE);
        return annexEStatusMemoryProblem;
    }
    pAnnexE->pRecvPDU->nMaxSize = nMaxSupportedMessageSize /*MAX_UDP_FRAME*/;

    /* initialize Timers    */
    pAnnexE->hTimers = timersPool;

    if (pAnnexE->hTimers == NULL)
    {
        RvLogError(&pAnnexE->log, (&pAnnexE->log, "annexEInit() - MemoryProblem!"));
        free_resources(pAnnexE);
        return annexEStatusMemoryProblem;
    }

    pAnnexE->pSelEngine = selEngine;
    pAnnexE->pPortRange = portRange;

    pAnnexE->State = INITIALIZED;

    *hAnnexE = AsHANNEXE(pAnnexE);

    RvLogInfo(&pAnnexE->log, (&pAnnexE->log, "annexEInit() End! - successful!"));

    return annexEStatusNormal;
}

void
free_resources(tAnnexE* pAnnexE)
{
    /* free pRecvPDU    */
    if (pAnnexE->pRecvPDU)
        RvMemoryFree(pAnnexE->pRecvPDU);

    /* free PDUList */
    while (!IsListEmpty(&pAnnexE->FreePDUList))
    {
        tPDU*       pPDU;
        RV_PLIST_ENTRY plink;

        plink   = RemoveHeadList(&pAnnexE->FreePDUList);
        pPDU    = RV_GET_STRUCT(tPDU, lPDULink, plink);

        RvMemoryFree(pPDU);
    }

    /* free Nodes   */
    while (!IsListEmpty(&pAnnexE->FreeNodesList))
    {
        tNode*  pNode;
        RV_PLIST_ENTRY plink;

        plink   = RemoveHeadList(&pAnnexE->FreeNodesList);
        pNode   = RV_GET_STRUCT(tNode, lNodeLink, plink);

        RvMemoryFree(pNode);
    }

    if (pAnnexE->ActiveNodesHash != NULL)
    {
        hashDestruct(pAnnexE->ActiveNodesHash);
    }

    RvLogSourceDestruct(RvLogGet(), &pAnnexE->log);

    /* free AnnexE  */
    RvMemoryFree(pAnnexE);
}

annexEStatus annexEEnd(IN  HANNEXE                 hAnnexE)
{

    tAnnexE*    pAnnexE = AsAnnexE(hAnnexE);

    if (pAnnexE->State == RUNNING)
        annexEStop(hAnnexE);

    pAnnexE->State = NOT_INITIALIZED;

    RvLogInfo(&pAnnexE->log, (&pAnnexE->log, "annexEEnd() - successful!"));

    free_resources(pAnnexE);

    return annexEStatusNormal;
}

/* event handler & protocol parameter functions
****************************************************************************
***************************************************************************/

annexEStatus annexESetProtocolParams(
    IN  HANNEXE                 hAnnexE,
    IN  int                     t_R1,
    IN  int                     t_R2,
    IN  int                     n_R1,
    IN  int                     t_IMA1,
    IN  int                     n_IMA1,
    IN  int                     t_DT
) {

    tAnnexE*    pAnnexE = AsAnnexE(hAnnexE);

    if (pAnnexE->State != INITIALIZED) {
        RvLogError(&pAnnexE->log, (&pAnnexE->log, "annexESetProtocolParams() - annexEStatusMustBeStopped!"));
        return annexEStatusMustBeStopped;
    }

    pAnnexE->t_R1   = (t_R1 > 0) ? t_R1 : ANNEXE_TR1;
    pAnnexE->t_R2   = (t_R2 > 0) ? t_R2 : pAnnexE->t_R1;
    pAnnexE->n_R1   = (n_R1 > 0) ? n_R1 : ANNEXE_NR1;
    pAnnexE->t_IMA1 = (t_IMA1 > 0) ? t_IMA1 : ANNEXE_TIMA1;
    pAnnexE->n_IMA1 = (n_IMA1 > 0) ? n_IMA1 : ANNEXE_NIMA1;
    pAnnexE->t_DT   = (t_DT > 0) ? t_DT : (pAnnexE->t_R1 / 4);

    RvLogDebug(&pAnnexE->log, (&pAnnexE->log, "annexESetProtocolParams(tR1=%i, tR2=%i, nR1=%i, tIMA1=%i, nIMA1=%i, tDT=%i) - successful!",
            pAnnexE->t_R1, pAnnexE->t_R2, pAnnexE->n_R1, pAnnexE->t_IMA1, pAnnexE->n_IMA1, pAnnexE->t_DT));

    return annexEStatusNormal;
}

annexEStatus annexESetLocalAddress(
    IN  HANNEXE                 hAnnexE,
    IN  cmTransportAddress*     pLocalAddress
) {

    tAnnexE*    pAnnexE = AsAnnexE(hAnnexE);

    if (pAnnexE->State != INITIALIZED)
    {
        RvLogError(&pAnnexE->log, (&pAnnexE->log, "annexESetLocalAddress() - annexEStatusMustBeStopped!"));
        return annexEStatusMustBeStopped;
    }

    if (pLocalAddress == NULL)
    {
        RvLogError(&pAnnexE->log, (&pAnnexE->log, "annexESetLocalAddress() - annexEStatusBadParameter(pLocalAddress == NULL)!"));
        return annexEStatusBadParameter;
    }

    if ((pLocalAddress->distribution != cmDistributionUnicast) ||
        (pLocalAddress->type != cmTransportTypeIP))
    {
        RvLogError(&pAnnexE->log, (&pAnnexE->log, "annexESetLocalAddress() - annexEStatusBadParameter(distribution or type error)!"));
        return annexEStatusBadParameter;
    }

    if (RvH323CmToCoreAddress(pLocalAddress, &pAnnexE->localAddr) != RV_OK)
    {
        RvLogError(&pAnnexE->log, (&pAnnexE->log, "annexESetLocalAddress() - annexEStatusBadParameter(bad address)!"));
        return annexEStatusBadParameter;
    }

#if (RV_LOGMASK_COMPILEMASK & RV_LOGLEVEL_INFO)
    {
        RvChar addrBuf[RV_ADDRESS_MAXSTRINGSIZE];
        addrBuf[0] = 0;
        RvAddressGetString(&pAnnexE->localAddr, addrBuf, sizeof(addrBuf));
        RvLogInfo(&pAnnexE->log, (&pAnnexE->log,
            "annexESetLocalAddress(%s) - successful!", addrBuf));
    }
#endif

    return annexEStatusNormal;
}

annexEStatus annexEGetLocalAddress(
    IN  HANNEXE                 hAnnexE,
    IN  cmTransportAddress*     pLocalAddress
) {
    tAnnexE* pAnnexE = AsAnnexE(hAnnexE);

    if (pLocalAddress == NULL) {
        RvLogError(&pAnnexE->log, (&pAnnexE->log, "annexEGetLocalAddress() - annexEStatusBadParameter(pLocalAddress == NULL)!"));
        return annexEStatusBadParameter;
    }

    RvH323CoreToCmAddress(&pAnnexE->localAddr, pLocalAddress);

    return annexEStatusNormal;
}

annexEStatus annexESetEventHandler(
    IN  HANNEXE             hAnnexE,
    IN  annexEEvents*       pAnnexEEventHandler
) {

    tAnnexE*    pAnnexE = AsAnnexE(hAnnexE);

    if (pAnnexE->State != INITIALIZED) {
        RvLogError(&pAnnexE->log, (&pAnnexE->log, "annexESetEventHandler() - annexEStatusMustBeStopped!"));
        return annexEStatusMustBeStopped;
    }

    if (pAnnexEEventHandler == NULL) {
        RvLogError(&pAnnexE->log, (&pAnnexE->log, "annexESetEventHandler() - annexEStatusBadParameter(pAnnexEEventHandler == NULL)!"));
        return annexEStatusBadParameter;
    }

    pAnnexE->events = *pAnnexEEventHandler;

    return annexEStatusNormal;
}

RvH323Connection* annexEGetConnection(
    IN  HANNEXE                 hAnnexE
                                      )
{
    tAnnexE*    pAnnexE = AsAnnexE(hAnnexE);

    return &pAnnexE->connection;
}

/* start & stop functions
****************************************************************************
***************************************************************************/

annexEStatus annexEStart(IN  HANNEXE                 hAnnexE)
{
    RvStatus res;
    tAnnexE*    pAnnexE = AsAnnexE(hAnnexE);

    if (pAnnexE->State != INITIALIZED)
    {
        RvLogError(&pAnnexE->log, (&pAnnexE->log, "annexEStart() - annexEStatusMustBeStopped!"));
        return annexEStatusMustBeStopped;
    }

    res = RvSocketConstruct(&pAnnexE->connection.socket, RV_ADDRESS_TYPE_IPV4, RvSocketProtocolUdp)
    RvAddressDestruct(&localAddress);
    if (res != RV_OK)
    {
        RvLogError(&pAnnexE->log, (&pAnnexE->log, "annexEStart() - Error opening socket!"));
        return annexEStatusSocketProblem;
    }

    /* We're always working with non-blocking sockets */
    res = RvSocketSetBlocking(&pAnnexE->connection.socket, RV_FALSE);

    pAnnexE->connection.type = RvH323ConnectionAnnexE;
    pAnnexE->State = RUNNING;
    RvSocketBind(&pAnnexE->connection.socket, &pAnnexE->localAddr, pAnnexE->pPortRange);
    pAnnexE->fReadyToSend = RV_TRUE;

    RvFdConstruct(&pAnnexE->connection.fd, &pAnnexE->connection.socket);
    RvSelectAdd(pAnnexE->pSelEngine, &pAnnexE->connection.fd, RvSelectRead, annexEEvent);

#if (RV_LOGMASK_COMPILEMASK & RV_LOGLEVEL_INFO)
    {
        RvChar addrBuf[RV_ADDRESS_MAXSTRINGSIZE];
        addrBuf[0] = 0;

        RvSocketGetLocalAddress(&pAnnexE->connection.socket, &pAnnexE->localAddr);
        RvAddressGetString(&pAnnexE->localAddr, addrBuf, sizeof(addrBuf));

        RvLogInfo(&pAnnexE->log, (&pAnnexE->log,
            "annexEStart() - successfuly bind on %s", addrBuf));
    }
#endif

    return annexEStatusNormal;
}

annexEStatus annexEStop(IN  HANNEXE                 hAnnexE)
{
    tAnnexE*    pAnnexE = AsAnnexE(hAnnexE);

    if (pAnnexE->State != RUNNING)
    {
        RvLogError(&pAnnexE->log, (&pAnnexE->log, "annexEStop() - annexEStatusMustBeStarted!"));
        return annexEStatusMustBeStarted;
    }

    RvSelectRemove(pAnnexE->pSelEngine, &pAnnexE->connection.fd);
    RvFdDestruct(&pAnnexE->connection.fd);
    RvSocketDestruct(&pAnnexE->connection.socket, RV_FALSE, pAnnexE->pPortRange);

    del_all_nodes(pAnnexE);

    /* clear WaitingForTransmissionList! */
    {
        RV_PLIST_ENTRY plink;
        tPDU*       pPDU;
        while (!IsListEmpty(&pAnnexE->WaitingForTransmissionList))
        {
            plink = RemoveHeadList(&pAnnexE->WaitingForTransmissionList);
            pPDU = RV_GET_STRUCT(tPDU, lPDULink, plink);
            pPDU->fPDUIsWaitingForSend = RV_FALSE;
            pPDU->nSize = 0;
            InsertTailList(&pAnnexE->FreePDUList, &pPDU->lPDULink);
        }
    }

    pAnnexE->State = INITIALIZED;

    RvLogInfo(&pAnnexE->log, (&pAnnexE->log, "annexEStop() - successful!"));

    return annexEStatusNormal;
}

#ifdef __cplusplus
}
#endif /* __cplusplus */

⌨️ 快捷键说明

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