transacmng.c

来自「mgcp协议源代码和测试程序,还有一个编译器」· C语言 代码 · 共 1,780 行 · 第 1/5 页

C
1,780
字号
/******************************************************************************
  Copyright(C) 2005,2006 Frank ZHANG

  All Rights Reserved.
    
  This program is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published by the Free
  Software Foundation; either version 2 of the License, or (at your option)
  any later version.

  This program is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 
  more details.
    
  You should have received a copy of the GNU General Public License along with
  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  Place, Suite 330, Boston, MA 02111-1307 USA.
 
 ******************************************************************************
*      Authors                   :  Frank ZHANG (openmgcp@gmail.com)
*      Description               :  Transaction Manager module
*
*
*      Date of creation          :  08/20/2005
*
*
*      History                   :
*      2005/08/20 Frank ZHANG    : - Creation
******************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include "typedef.h"
#include "debg.h"
#include "misc.h"
#include "list.h"
#include "abnf.h"
#include "posix.h"
#include "mgcpdef.h"
#include "stackcb.h"
#include "mgcpmsgtran.h"
#include "transacmng.h"

extern LONG SendMsgToEndpointCtrl(MGCP_ENDPOINT_CONTROL*, MGCP_STACK_MSG*);

/*****************************************************************************/

/* Get a new transaction ID */
#define GetNewTransacID(pTransacMng) (++pTransacMng->dwTransactionId)

/******************************************************************************
 * Function          : FindMgcpCmdOutByTranID
 *
 * Description       : Compare the transaction ID of the command with the target
 *                     transaction ID.
 *                     
 * Input parameters  : pTranCmdOut - Pointer to  outgoing command 
 *                     pTransacID - Pointer to the target transaction ID
 *
 * Output parameters : 
 *
 * Return value      : If the transaction ID of the command is equal to the
 *                     target transaction ID, return 0; otherwise return none 0
 *
 * Comments          : 
 *
 * History           :
 *  2005/09/04       : Creation
 *
 * Date              : Sep 04 2005, Frank Zhang
 ******************************************************************************/
int FindMgcpCmdOutByTranID(void *pTranCmdOut, void *pTransacID)
{
  Assert(pTranCmdOut);
  Assert(pTransacID);
  return (*((DWORD*)pTransacID)-((TRANSAC_CMD_OUT*)pTranCmdOut)->dwTransacId);
}
/******************************************************************************
 * Function          : FindMgcpRspAckOutByTranID
 *
 * Description       : Compare the transaction ID of the response with the
 *                     target transaction ID.
 *                     
 * Input parameters  : pRspAckOut - Pointer to  outgoing response 
 *                     pTransacID - Pointer to the target transaction ID
 *
 * Output parameters : 
 *
 * Return value      : If the transaction ID of the response is equal to the
 *                     target transaction ID, return 0; otherwise return none 0
 *
 * Comments          : 
 *
 * History           :
 *  2005/09/04       : Creation
 *
 * Date              : Sep 04 2005, Frank Zhang
 ******************************************************************************/
int FindMgcpRspAckOutByTranID(void *pRspAckOut, void *pTransacID)
{
  Assert(pRspAckOut);
  Assert(pTransacID);
  return (*((DWORD*)pTransacID)-((TRANSAC_RSPACK_OUT*)pRspAckOut)->dwTransacId);
}
/******************************************************************************
 * Function          : FindMgcpCmdInByTranID
 *
 * Description       : Compare the transaction ID of the incoming command with
 *                     the target transaction ID.
 *                     
 * Input parameters  : pCmdIn - Pointer to  incoming command 
 *                     pTransacID - Pointer to the target transaction ID
 *
 * Output parameters : 
 *
 * Return value      : If the transaction ID of the command is equal to the
 *                     target transaction ID, return 0; otherwise return none 0
 *
 * Comments          : 
 *
 * History           :
 *  2005/09/04       : Creation
 *
 * Date              : Sep 04 2005, Frank Zhang
 ******************************************************************************/
int FindMgcpCmdInByTranID(void *pCmdIn, void *pTransacID)
{
  Assert(pCmdIn);
  Assert(pTransacID);
  return (*((DWORD*)pTransacID)-((TRANSAC_CMD_IN*)pCmdIn)->dwTransacId);
}
/******************************************************************************
 * Function          : FindMgcpRspOutByTranID
 *
 * Description       : Compare the transaction ID of the outgoing response with
 *                     the target transaction ID.
 *                     
 * Input parameters  : pRspOut - Pointer to outgoing response 
 *                     pTransacID - Pointer to the target transaction ID
 *
 * Output parameters : 
 *
 * Return value      : If the transaction ID of the response is equal to the
 *                     target transaction ID, return 0; otherwise return none 0
 *
 * Comments          : 
 *
 * History           :
 *  2005/09/04       : Creation
 *
 * Date              : Sep 04 2005, Frank Zhang
 ******************************************************************************/
int FindMgcpRspOutByTranID(void *pRspOut, void *pTransacID)
{
  Assert(pRspOut);
  Assert(pTransacID);
  Assert(((TRANSAC_RSP_OUT*)pRspOut)->pRspOut);
  return (*((DWORD*)pTransacID)-((TRANSAC_RSP_OUT*)pRspOut)->pRspOut->dwTransacId);
}
/******************************************************************************
 * Function          : FindMgcpRspOutWaitAckByTranID
 *
 * Description       : Compare the transaction ID of the outgoing response
 *                     waiting ack with the target transaction ID.
 *                     
 * Input parameters  : pRspOut - Pointer to outgoing response waiting ack 
 *                     pTransacID - Pointer to the target transaction ID
 *
 * Output parameters : 
 *
 * Return value      : If the transaction ID of the response is equal to the
 *                     target transaction ID, return 0; otherwise return none 0
 *
 * Comments          : 
 *
 * History           :
 *  2005/09/04       : Creation
 *
 * Date              : Sep 04 2005, Frank Zhang
 ******************************************************************************/
int FindMgcpRspOutWaitAckByTranID(void *pRspOut, void *pTransacID)
{
  Assert(pRspOut);
  Assert(pTransacID);
  Assert(((TRANSAC_RSP_WAIT_ACK*)pRspOut)->pRspOut);
  
  return (*((DWORD*)pTransacID)-((TRANSAC_RSP_WAIT_ACK*)pRspOut)->pRspOut->dwTransacId);
}
/******************************************************************************
 * Function          : FindMgcpRspACKByTranID
 *
 * Description       : Compare the transaction ID of the outgoing response ack
 *                     with the target transaction ID.
 *                     
 * Input parameters  : pRspAck - Pointer to outgoing response ack 
 *                     pTransacID - Pointer to the target transaction ID
 *
 * Output parameters : 
 *
 * Return value      : If the transaction ID of the response ack is equal to the
 *                     target transaction ID, return 0; otherwise return none 0
 *
 * Comments          : 
 *
 * History           :
 *  2005/09/04       : Creation
 *
 * Date              : Sep 04 2005, Frank Zhang
 ******************************************************************************/
int FindMgcpRspACKByTranID(void *pRspAck, void *pTransacID)
{
  Assert(pRspAck);
  Assert(pTransacID);
  return (*((DWORD*)pTransacID)-((TRANSAC_RSP_ACKED*)pRspAck)->dwTransacId);
}
/******************************************************************************
 * Function          : FindMgcpCmdOutByCmdID
 *
 * Description       : Compare the transaction ID of the outgoing command with
 *                     the target command ID.
 *                     
 * Input parameters  : pCmdOut - Pointer to outgoing command 
 *                     pCommandID - Pointer to the target command ID
 *
 * Output parameters : 
 *
 * Return value      : If the transaction ID of the command is equal to the
 *                     target transaction ID, return 0; otherwise return none 0
 *
 * Comments          : 
 *
 * History           :
 *  2005/09/04       : Creation
 *
 * Date              : Sep 04 2005, Frank Zhang
 ******************************************************************************/
int FindMgcpCmdOutByCmdID(void *pCmdOut, void *pCommandID)
{
  DWORD *pCmdId = (DWORD*)pCommandID;
  MGCP_CMD_OUT *pCmd = NULL;

  Assert(pCommandID);
  Assert(pCmdOut);

  pCmd = ((TRANSAC_CMD_OUT*)pCmdOut)->pCmdOut;
  
  return (*pCmdId - pCmd->dwCmdId);
}
/******************************************************************************
 * Function          : BuildPiggybackedCommands
 *
 * Description       : Fill the Abnf MGCP commands with the piggybacking
 *                     commands
 *                     
 * Input parameters  : pTransacMng - Handle of the TransacMng 
 *                     pMsgList - Pointer to Mgcp outgoing command list of the
 *                                transaction manager
 *                     wNum - Number of the commands need piggybacking
 *                     pCmdID - Command ID table of the piggybacking command
 *
 * Output parameters : pAbnfMsgList - Abnf MGCP messages
 *
 * Return value      : None
 *
 * Comments          : 
 *
 * History           :
 *  2005/09/04       : Creation
 *
 * Date              : Sep 04 2005, Frank Zhang
 ******************************************************************************/
void BuildPiggybackedCommands(H_MGCP_TRANSAC_MANAGER pTransacMng,
                              TMGCPMsgList *pAbnfMsgList,
                              MGCP_MSG_LIST *pMsgList,
                              WORD wNum, DWORD *pCmdID)
{
  int i;
  TRANSAC_CMD_OUT *pTranCmdOut;
  TMGCPMessage *pAbnfMsg;
  
  Assert(pTransacMng);
  Assert(pAbnfMsgList);
  Assert(pMsgList);
  Assert(pCmdID);
  
  /* Find the commands need piggyback with this command */  
  for (i = 0; i < wNum; i++)
  {
    /* Find the command need piggyback in cmdsend list ? */
    if (SListFind(&pMsgList->CmdSentList, pCmdID+i, FindMgcpCmdOutByCmdID) != NULL)
	{
      pTranCmdOut = SListGetCurData(&pMsgList->CmdSentList);
	  Assert(pTranCmdOut);
	}
    else if (SListFind(&pMsgList->CmdSentPiggybackList,
                       &pCmdID[i], FindMgcpCmdOutByCmdID) != NULL)
	{
      pTranCmdOut = SListGetCurData(&pMsgList->CmdSentPiggybackList);
	  Assert(pTranCmdOut);
	}
    else
    {
      /* In some case, the piggyback commands maybe have been removed by
         TransacMng for receiving response or timeout, so if not found, continue
         to find the next */
      continue;
    }

    pAbnfMsgList->MGCPMessage.pNode
      = realloc(pAbnfMsgList->MGCPMessage.pNode,
                ((WORD)pAbnfMsgList->MGCPMessage.iNum+1)*sizeof(TMGCPMessage));
    Assert(pAbnfMsgList->MGCPMessage.pNode);

    pAbnfMsg = ((TMGCPMessage*)pAbnfMsgList->MGCPMessage.pNode)
               + pAbnfMsgList->MGCPMessage.iNum;
    memset(pAbnfMsg, 0, sizeof(TMGCPMessage));
    
    pAbnfMsgList->MGCPMessage.iNum++;
    
    pAbnfMsg->iType = MGCPMessage_MGCPCommand;
    FillAbnfMgcpCommand(&pAbnfMsg->u.MGCPCommand,
                        pTranCmdOut->pCmdOut->eType,
                        pTranCmdOut->pCmdOut->u.pRsipCmd,
                        pTranCmdOut->dwTransacId,
                        pTranCmdOut->pCmdOut->hEndpointHandle->pcEndpointName,
                        pTransacMng->pcDomainName,
                        pTransacMng->pStack->pcMgcpVersion,
                        pTransacMng->pStack->pcProfileName);
  }
}
/******************************************************************************
 * Function          : FillAbnfMgcpCmdOut
 *
 * Description       : Fill the Abnf commands
 *                     
 * Input parameters  : pTransacMng - Handle of the TransacMng 
 *                     pTranMgcpCmdOut - Pointer to Mgcp transaction outgoing
 *                                       command data
 *
 * Output parameters : pAbnfMsgList - Abnf MGCP command data
 *
 * Return value      : None
 *
 * Comments          : 
 *
 * History           :
 *  2005/09/04       : Creation
 *
 * Date              : Sep 04 2005, Frank Zhang
 ******************************************************************************/
void FillAbnfMgcpCmdOut(H_MGCP_TRANSAC_MANAGER pTransacMng,
                        TMGCPMsgList *pAbnfMsgList,
                        TRANSAC_CMD_OUT *pTranMgcpCmdOut)
{

⌨️ 快捷键说明

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