transacmng.c
来自「mgcp协议源代码和测试程序,还有一个编译器」· C语言 代码 · 共 1,780 行 · 第 1/5 页
C
1,780 行
TMGCPMessage *pAbnfMgcpMsg = NULL;
Assert(pTransacMng);
Assert(pAbnfMsgList);
Assert(pTranMgcpCmdOut);
/* First check if need piggyback other commands */
if (pTranMgcpCmdOut->pCmdOut->wPiggyCmdNum > 0)
{
BuildPiggybackedCommands(pTransacMng,
pAbnfMsgList,
&pTransacMng->MsgList,
pTranMgcpCmdOut->pCmdOut->wPiggyCmdNum,
pTranMgcpCmdOut->pCmdOut->PiggybackCmdIDTable);
}
/* Fill the current outgoing mgcp command */
pAbnfMsgList->MGCPMessage.pNode = realloc(pAbnfMsgList->MGCPMessage.pNode,
((WORD)pAbnfMsgList->MGCPMessage.iNum+1)*sizeof(TMGCPMessage));
Assert(pAbnfMsgList->MGCPMessage.pNode);
pAbnfMgcpMsg = ((TMGCPMessage*)pAbnfMsgList->MGCPMessage.pNode)
+ pAbnfMsgList->MGCPMessage.iNum;
memset(pAbnfMgcpMsg, 0, sizeof(TMGCPMessage));
pAbnfMsgList->MGCPMessage.iNum++;
/* Command type */
pAbnfMgcpMsg->iType = MGCPMessage_MGCPCommand;
/* Fill the command data */
FillAbnfMgcpCommand(&pAbnfMgcpMsg->u.MGCPCommand,
pTranMgcpCmdOut->pCmdOut->eType,
pTranMgcpCmdOut->pCmdOut->u.pRsipCmd,
pTranMgcpCmdOut->dwTransacId,
pTranMgcpCmdOut->pCmdOut->hEndpointHandle->pcEndpointName,
pTransacMng->pcDomainName,
pTransacMng->pStack->pcMgcpVersion,
pTransacMng->pStack->pcProfileName);
}
/******************************************************************************
* Function : FillAbnfMgcpRspOut
*
* Description : Fill the Abnf mgcp response
*
* Input parameters : pTransacMng - Handle of the TransacMng
* pMgcpRspOut - Pointer to Mgcp outgoing response data
* bNeedAck - Flag of whether this response need ack
*
* Output parameters : pAbnfMsgList
*
* Return value : None
*
* Comments :
*
* History :
* 2005/09/04 : Creation
*
* Date : Sep 04 2005, Frank Zhang
******************************************************************************/
void FillAbnfMgcpRspOut(H_MGCP_TRANSAC_MANAGER pTransacMng,
TMGCPMsgList *pAbnfMsgList,
MGCP_RSP_OUT *pMgcpRspOut,
BOOL bNeedAck)
{
TMGCPMessage *pAbnfMgcpMsg = NULL;
Assert(pTransacMng);
Assert(pAbnfMsgList);
Assert(pMgcpRspOut);
/* First check if need piggyback other commands */
if (pMgcpRspOut->wPiggyCmdNum > 0)
{
BuildPiggybackedCommands(pTransacMng, pAbnfMsgList,
&pTransacMng->MsgList, pMgcpRspOut->wPiggyCmdNum,
pMgcpRspOut->PiggybackCmdIDTable);
}
/* Fill the current outgoing mgcp command */
pAbnfMsgList->MGCPMessage.pNode = realloc(pAbnfMsgList->MGCPMessage.pNode,
((WORD)pAbnfMsgList->MGCPMessage.iNum+1)*sizeof(TMGCPMessage));
Assert(pAbnfMsgList->MGCPMessage.pNode);
pAbnfMgcpMsg = ((TMGCPMessage*)pAbnfMsgList->MGCPMessage.pNode)
+ pAbnfMsgList->MGCPMessage.iNum;
memset(pAbnfMgcpMsg, 0, sizeof(TMGCPMessage));
pAbnfMsgList->MGCPMessage.iNum++;
/* Command type */
pAbnfMgcpMsg->iType = MGCPMessage_MGCPResponse;
/* Fill the command data */
FillAbnfMgcpResponse(&pAbnfMgcpMsg->u.MGCPResponse, pMgcpRspOut->wRspCode,
pMgcpRspOut->pcRspString, pMgcpRspOut->dwTransacId,
pMgcpRspOut->eType, pMgcpRspOut->u.pEpcfRsp, bNeedAck);
}
/******************************************************************************
* Function : SendMgcpCmdOut
*
* Description : Encode the mgcp commands and sent out to the socket
*
* Input parameters : pTransacMng - Handle of the TransacMng
* pTranMgcpCmdOut - Pointer to Mgcp transaction outgoing
* command data
*
* Output parameters :
*
* Return value : None
*
* Comments :
*
* History :
* 2005/09/03 : Creation
*
* Date : Sep 03 2005, Frank Zhang
******************************************************************************/
void SendMgcpCmdOut(H_MGCP_TRANSAC_MANAGER pTransacMng,
TRANSAC_CMD_OUT *pTranMgcpCmdOut)
{
TMGCPMsgList AbnfMsgList;
TCtx ctx;
char EnBuffer[ABNF_ENCODE_BUF_LEN];
Assert(pTransacMng);
Assert(pTranMgcpCmdOut);
Assert(pTranMgcpCmdOut->pCmdOut);
memset(&AbnfMsgList, 0, sizeof(TMGCPMsgList));
CtxInitial(&ctx, EnBuffer, ABNF_ENCODE_BUF_LEN);
memset(EnBuffer, 0, sizeof(EnBuffer));
FillAbnfMgcpCmdOut(pTransacMng, &AbnfMsgList, pTranMgcpCmdOut);
if (EN_MGCPMsgList(&ctx, &AbnfMsgList) == OK)
{
struct sockaddr_in DesAddress;
socklen_t SocketLen = sizeof(struct sockaddr_in);
WORD wIndex = pTranMgcpCmdOut->pCmdOut->NotifiedEntity.wCurAddrIndex;
DesAddress.sin_family = AF_INET;
DesAddress.sin_port = htons(pTranMgcpCmdOut->pCmdOut->NotifiedEntity.wPort);
DesAddress.sin_addr.s_addr
= htonl(pTranMgcpCmdOut->pCmdOut->NotifiedEntity.pIPAddrList[wIndex]);
/* If Log message is on, print the transmit abnf message */
DEBUG({
int i = 0;
char str_cur_time[24] = {0};
if (Get_Time_Ext(str_cur_time, 24) == TRUE)
{
LogMSG(LOG_ABNF_MSG, "\r\n>TX ------------ %s:\r\n", str_cur_time);
}
else
{
LogMSG(LOG_ABNF_MSG, "\r\n>TX----------------------------------------:\r\n");
}
LogMSG(LOG_ABNF_MSG, " IP == %s : port == %d\r\n",
inet_ntoa(DesAddress.sin_addr),
pTranMgcpCmdOut->pCmdOut->NotifiedEntity.wPort);
for (i = 0; i < ctx.pCur-ctx.pHead; i++)
{
LogMSG(LOG_ABNF_MSG, "%c", ctx.pHead[i]);
}
});
if (sendto(pTransacMng->iSocket_fd, ctx.pHead, ctx.pCur-ctx.pHead, 0,
(struct sockaddr*)&DesAddress, SocketLen) < 0)
{
DEBUG(LogMSG(LOG_ERROR, "\n@@TransacMng: Fail to send socket data! IP: %d, Port: %d",
pTranMgcpCmdOut->pCmdOut->NotifiedEntity.pIPAddrList[wIndex],
pTranMgcpCmdOut->pCmdOut->NotifiedEntity.wPort););
}
}
else
{
DEBUG(LogMSG(LOG_ERROR, "\nFail to encode MGCP outgoing command!"););
}
Free_MGCPMsgList(&AbnfMsgList);
}
/******************************************************************************
* Function : SendMgcpRspAckOut
*
* Description : Fill the Abnf mgcp Response Ack and sent to the socket
*
* Input parameters : pTransacMng - Handle of the TransacMng
* dwTranID - Transaction ID of the ACK response
*
* Output parameters :
*
* Return value : None
*
* Comments :
*
* History :
* 2005/09/03 : Creation
*
* Date : Sep 03 2005, Frank Zhang
******************************************************************************/
void SendMgcpRspAckOut(H_MGCP_TRANSAC_MANAGER pTransacMng, DWORD dwTranID)
{
MGCP_RSP_OUT *pMgcpRspOut = (MGCP_RSP_OUT*)calloc(1, sizeof(MGCP_RSP_OUT));
Assert(pMgcpRspOut);
Assert(pTransacMng);
// memset(pMgcpRspOut, 0, sizeof(MGCP_RSP_OUT));
/* Send response ack out */
pMgcpRspOut->wRspCode = 0;
pMgcpRspOut->dwTransacId = dwTranID;
SendMgcpRspOut(pTransacMng, pMgcpRspOut, FALSE);
ClearMgcpRspOut(pMgcpRspOut);
free(pMgcpRspOut);
}
/******************************************************************************
* Function : SendMgcpRspOut
*
* Description : Fill the Abnf mgcp Response and sent to the socket, if
* the response need ack, add the ACK parameter into the
* Abnf response data.
*
* Input parameters : pTransacMng - Handle of the TransacMng
* pMgcpRspOut - Pointer to Mgcp outgoing response data
* bNeedAck - Flag if the response need ack
*
* Output parameters :
*
* Return value : None
*
* Comments :
*
* History :
* 2005/09/03 : Creation
*
* Date : Sep 03 2005, Frank Zhang
******************************************************************************/
void SendMgcpRspOut(H_MGCP_TRANSAC_MANAGER pTransacMng,
MGCP_RSP_OUT *pMgcpRspOut , BOOL bNeedAck)
{
TMGCPMsgList AbnfMsgList;
TCtx ctx;
char EnBuffer[ABNF_ENCODE_BUF_LEN];
memset(&AbnfMsgList, 0, sizeof(TMGCPMsgList));
CtxInitial(&ctx, EnBuffer, ABNF_ENCODE_BUF_LEN);
memset(EnBuffer, 0, sizeof(EnBuffer));
Assert(pTransacMng);
FillAbnfMgcpRspOut(pTransacMng, &AbnfMsgList, pMgcpRspOut, bNeedAck);
if (EN_MGCPMsgList(&ctx, &AbnfMsgList) == OK)
{
struct sockaddr_in DesAddress;
socklen_t SocketLen = sizeof(struct sockaddr);
DesAddress.sin_family = AF_INET;
DesAddress.sin_port = htons(pMgcpRspOut->wDesPort);
DesAddress.sin_addr.s_addr = htonl(pMgcpRspOut->dwDesIpAddr);
/* If Log message is on, print the transmit abnf message */
DEBUG({
int i = 0;
char str_cur_time[24] = {0};
if (Get_Time_Ext(str_cur_time, 24) == TRUE)
{
LogMSG(LOG_ABNF_MSG, "\r\n>TX ------------ %s:\r\n", str_cur_time);
}
else
{
LogMSG(LOG_ABNF_MSG, "\r\n>TX----------------------------------------:\r\n");
}
LogMSG(LOG_ABNF_MSG, " IP == %s : port == %d\r\n",
inet_ntoa(DesAddress.sin_addr),
pMgcpRspOut->wDesPort);
for (i = 0; i < ctx.pCur-ctx.pHead; i++)
{
LogMSG(LOG_ABNF_MSG, "%c", ctx.pHead[i]);
}
});
if (sendto(pTransacMng->iSocket_fd, ctx.pHead, ctx.pCur-ctx.pHead, 0,
(struct sockaddr*)&DesAddress, SocketLen) <0)
{
DEBUG(LogMSG(LOG_ERROR, "\nTransacMng: Fail to send socket data! IP: %d, Port: %d",
pMgcpRspOut->dwDesIpAddr,
pMgcpRspOut->wDesPort););
}
}
else
{
DEBUG(LogMSG(LOG_ERROR, "\nFail to encode MGCP outgoing response!"););
}
Free_MGCPMsgList(&AbnfMsgList);
}
/******************************************************************************
* Function : RemovePiggyBackingCmdID
*
* Description : Find the commands/responses piggybacking the target
* command id, if found, remove the command id from the
* piggybacking command ID table, if the tabke is empty,
* move this command/response from piggybacking list to none
* piggybacking list
*
* Input parameters : dwCommandID - Command ID to be find and removed
* pMsgList - pointer to Mgcp outgoing message list
*
* Output parameters :
*
* Return value : None
*
* Comments :
*
* History :
* 2005/09/03 : Creation
*
* Date : Sep 03 2005, Frank Zhang
******************************************************************************/
void RemovePiggyBackingCmdID(DWORD dwCommandID, MGCP_MSG_LIST *pMsgList)
{
WORD i;
TRANSAC_CMD_OUT *pCmdPiggy;
TRANSAC_RSP_OUT *pRspPiggy;
TRANSAC_RSP_WAIT_ACK *pRspPiggyWaitAck;
Assert(pMsgList);
SListReset(&pMsgList->CmdSentPiggybackList);
while((pCmdPiggy = SListGetCurData(&pMsgList->CmdSentPiggybackList))!= NULL)
{
Assert(pCmdPiggy->pCmdOut->wPiggyCmdNum > 0);
for (i = 0; i < pCmdPiggy->pCmdOut->wPiggyCmdNum; i++)
{
if (pCmdPiggy->pCmdOut->PiggybackCmdIDTable[i] == dwCommandID)
{
WORD j;
/* Advance the following command IDs after the found Command ID */
for (j = i+1; j < pCmdPiggy->pCmdOut->wPiggyCmdNum; j++)
{
pCmdPiggy->pCmdOut->PiggybackCmdIDTable[i] =
pCmdPiggy->pCmdOut->PiggybackCmdIDTable[j];
}
pCmdPiggy->pCmdOut->wPiggyCmdNum--;
break;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?