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

📄 voip_adapter_ready.c

📁 telcobridges voip develop
💻 C
📖 第 1 页 / 共 2 页
字号:
/*--------------------------------------------------------------------------------------------------------------------------------
 |
 |	Project:    	VOIP sample
 |
 |	Filename:   	voip_adapter_ready.c
 |
 |	Copyright:  	TelcoBridges 2002-2004, All Rights Reserved
 |
 |	Description:	This file contains functions used to configure adapter when it is ready.
 |
 |	Notes:      	Tabs = 4
 |
 *-------------------------------------------------------------------------------------------------------------------------------
 |
 |	Revision:   	$Revision: 1.21 $
 |
 *------------------------------------------------------------------------------------------------------------------------------*/


/*--------------------------------------------------------------------------------------------------------------------------------
 |  Includes
 *------------------------------------------------------------------------------------------------------------------------------*/

#include "voip_includes.h"


/*--------------------------------------------------------------------------------------------------------------------------------
 |  Forward declarations
 *------------------------------------------------------------------------------------------------------------------------------*/


/*--------------------------------------------------------------------------------------------------------------------------------
 |  Defines
 *------------------------------------------------------------------------------------------------------------------------------*/


/*--------------------------------------------------------------------------------------------------------------------------------
 |  Types
 *------------------------------------------------------------------------------------------------------------------------------*/


/*--------------------------------------------------------------------------------------------------------------------------------
 |  Versioning
 *------------------------------------------------------------------------------------------------------------------------------*/

#ifdef WIN32
/*@unused@*/ static char g_szFileVersion [] = "$Revision: 1.21 $";
#endif


/*--------------------------------------------------------------------------------------------------------------------------------
 |  Global variables
 *------------------------------------------------------------------------------------------------------------------------------*/


/*--------------------------------------------------------------------------------------------------------------------------------
 |  Macros
 *------------------------------------------------------------------------------------------------------------------------------*/


/*--------------------------------------------------------------------------------------------------------------------------------
 |  Function Prototypes
 *------------------------------------------------------------------------------------------------------------------------------*/


/*--------------------------------------------------------------------------------------------------------------------------------
 |  Implementation
 *------------------------------------------------------------------------------------------------------------------------------*/

/*-------------------------------------------------------------------------------------------------------------------------------
 |
 |  VoipAdapterIsConnectMessage	:	Tell if message is for connect message handler.
 |
 |	in_hMsg						:	Message to test.
 |
 |  Note						:	~
 |
 |  Return						:	TBX_RESULT_OK if the function succeeded
 |									Other error code if the function could not complete properly.
 |
 *------------------------------------------------------------------------------------------------------------------------------*/
TBX_BOOL VoipAdapterIsConnectMessage(
	IN		TBX_MSG_HANDLE	in_hMsg)
{
	switch( TBX_MSG_ID_GET( in_hMsg ) )
	{
		case VOIP_MSG_ID_OP_ALLOCATE_CONNECTION:
		case TB640_MSG_ID_STREAM_RES_ALLOC:
		case TB640_MSG_ID_VP_GROUP_ALLOC:
		case TB640_MSG_ID_CONN_OP_CREATE:
			break;

		default:
			return TBX_FALSE;
	}

	return TBX_TRUE;
}

/*-------------------------------------------------------------------------------------------------------------------------------
 |
 |  VoipAdapterIsDisconnectMessage	:	Tell if message is for disconnect message handler.
 |
 |	in_hMsg							:	Message to test.
 |
 |  Note							:	~
 |
 |  Return							:	TBX_RESULT_OK if the function succeeded
 |										Other error code if the function could not complete properly.
 |
 *------------------------------------------------------------------------------------------------------------------------------*/
TBX_BOOL VoipAdapterIsDisconnectMessage(
	IN		TBX_MSG_HANDLE	in_hMsg)
{
	switch( TBX_MSG_ID_GET( in_hMsg ) )
	{
		case VOIP_MSG_ID_OP_CLEAR_CONNECTION:
		case TB640_MSG_ID_STREAM_RES_FREE:
		case TB640_MSG_ID_VP_GROUP_FREE:
		case TB640_MSG_ID_CONN_OP_DESTROY:
			break;

		default:
			return TBX_FALSE;
	}

	return TBX_TRUE;
}

/*-------------------------------------------------------------------------------------------------------------------------------
 |
 |  VoipAdapterHandleConnectMessage	:	Handles messages to create connections.
 |
 |	in_pAdapterContext				:	Adapter we are configuring
 |	in_hMsg							:	Message to handle
 |	
 |
 |  Note							:	~
 |
 |  Return							:	TBX_RESULT_OK if the function succeeded
 |										Other error code if the function could not complete properly.
 |
 *------------------------------------------------------------------------------------------------------------------------------*/
TBX_RESULT	VoipAdapterHandleConnectMessage(
	IN		PVOIP_ADAPTER_CONTEXT	in_pAdapterContext,
	IN		TBX_MSG_HANDLE			in_hMsg)
{
	TBX_RESULT					Result;
	PVOIP_CONNECTION_CONTEXT	pCurrentConnectionContext;
	PVOIP_CONNECTION_CONTEXT	pTargetConnectionContext;
	TBX_UINT32					un32ConnectionId;
	TBX_UINT32					un32Index;
	TBX_BOOL					fContinue;

	/*---------------------------------------------------------------------------------------------------------------------------
	 |  Code section
	 *--------------------------------------------------------------------------------------------------------------------------*/
	CODE
	{
		/* Initialize local variables */
		Result						= TBX_RESULT_OK;
		pCurrentConnectionContext	= NULL;
		pTargetConnectionContext	= NULL;
		fContinue					= TBX_FALSE;
		un32ConnectionId			= 0;

		switch( TBX_MSG_ID_GET( in_hMsg ) )
		{
			case VOIP_MSG_ID_OP_ALLOCATE_CONNECTION:
			{
				PVOIP_EVT_OP_ALLOCATE_CONNECTION	pEvt = TBX_MSG_PAYLOAD_POINTER( in_hMsg );

				for( un32Index = 0; un32Index < pEvt->un32NbConnections; un32Index++ )
				{
					/* Increment stress-test stats */
					if( in_pAdapterContext->StressTest.fActive == TBX_TRUE )
					{
						in_pAdapterContext->StressTest.Stats.un64NbConnectionsAttempt++;
					}

					/* Add new connection to target configuration */
					Result = VoipAdapterAddConnectionToTarget( in_pAdapterContext, &pEvt->Params, &pTargetConnectionContext );
					if( TBX_RESULT_FAILURE( Result ) == TBX_TRUE )
					{
						TBX_EXIT_ERROR( Result, 0, "Failed to create new connection context" );
					}

					/* Retrieve connection identifier */
					un32ConnectionId = pTargetConnectionContext->un32ConnectionId;

					/* Reset responses counter */
					pTargetConnectionContext->un32NbResponsesExpected = 0;

					if( pTargetConnectionContext->Params.Type1.SecondRes.Type != VOIP_RESOURCE_TYPE_STREAM_SOCKET )
					{
						/*
						 *	Allocate the connection context
						 */
						Result = VoipSendAdapterConnectionAlloc(
							in_pAdapterContext,
							pTargetConnectionContext);
						if( TBX_RESULT_FAILURE( Result ) )
						{
							TBX_EXIT_ERROR (Result, 0, "Failed to allocate connection");
						}
					}
					else
					{
						/*
						 * Allocate current stream resource
						 */
						{
							PVOIP_STREAM_RES	pCurrentStreamRes;
							PVOIP_STREAM_RES	pTargetStreamRes;

							Result = TBXHashFind( in_pAdapterContext->pTargetConfig->hStreamResHash, un32ConnectionId, (PTBX_VOID*)&pTargetStreamRes );
							if( TBX_RESULT_FAILURE( Result ) == TBX_TRUE )
							{
								TBX_EXIT_ERROR (Result, 0, "Failed to found target stream resource!");
							}
							if( pTargetStreamRes->Common.fAllocated == TBX_TRUE )
							{
								/* Find the corresponding current stream resource context */
								Result = TBXHashFind
								(
									in_pAdapterContext->CurrentConfig.hStreamResHash,
									(TBX_HASH_KEY)un32ConnectionId,
									(PTBX_VOID*)&pCurrentStreamRes
								);

								if( TBX_RESULT_FAILURE( Result ) == TBX_TRUE )
								{
									/*
									 *	Allocate the current stream resource
									 */
									Result = VoipSendAdapterStreamResAlloc(
										in_pAdapterContext,
										pTargetStreamRes);
									if( TBX_RESULT_FAILURE( Result ) )
									{
										TBX_EXIT_ERROR (Result, 0, "Failed to allocate current stream resource");
									}

									pTargetConnectionContext->un32NbResponsesExpected++;
								}
							}
						}

						/*
						 * Allocate current voice processing resource
						 */
						{
							PVOIP_VP_GROUP	pCurrentVpGroup;
							PVOIP_VP_GROUP	pTargetVpGroup;

							Result = TBXHashFind( in_pAdapterContext->pTargetConfig->hVpGroupHash, un32ConnectionId, (PTBX_VOID*)&pTargetVpGroup );
							if( TBX_RESULT_FAILURE( Result ) == TBX_TRUE )
							{
								TBX_EXIT_ERROR (Result, 0, "Failed to found target voice processing group!");
							}
							if( pTargetVpGroup->Common.fAllocated == TBX_TRUE )
							{
								/* Find the corresponding current voice processing group context */
								Result = TBXHashFind
								(
									in_pAdapterContext->CurrentConfig.hVpGroupHash,
									(TBX_HASH_KEY)pTargetVpGroup->Common.un32ConnectionId,
									(PTBX_VOID*)&pCurrentVpGroup
								);

								if( TBX_RESULT_FAILURE( Result ) == TBX_TRUE )
								{
									/*
									 *	Allocate the current voice processing group
									 */
									Result = VoipSendAdapterVpGroupAlloc(
										in_pAdapterContext,
										pTargetVpGroup);
									if( TBX_RESULT_FAILURE( Result ) )
									{
										TBX_EXIT_ERROR (Result, 0, "Failed to allocate current voice processing group");
									}

									pTargetConnectionContext->un32NbResponsesExpected++;
								}
							}
						}
					}

					/* Update connections parameters for next connection */
					if( pEvt->un32NbConnections > 1 )
					{
						if( pEvt->Params.Type == VOIP_CONNECTION_TYPE_0 )
						{
							pEvt->Params.Type0.un16RxIPPort += pEvt->un16IPPortIncrement;
						}
						else
						{
							/* Update first resource params for next connection */
							if( pEvt->Params.Type1.FirstRes.Type == VOIP_RESOURCE_TYPE_TRUNK )
							{
								PVOIP_TRUNK_CONFIG	pTrunkConfig = &in_pAdapterContext->CurrentConfig.aTrunk[pEvt->Params.Type1.FirstRes.un32TrunkNb];

								pEvt->Params.Type1.FirstRes.un32TimeSlot++;
								if( pEvt->Params.Type1.FirstRes.un32TimeSlot > pTrunkConfig->un32MaxTimeSlot )
								{
									pEvt->Params.Type1.FirstRes.un32TrunkNb++;
									pEvt->Params.Type1.FirstRes.un32TimeSlot = 1;
								}
							}
							else if( pEvt->Params.Type1.FirstRes.Type == VOIP_RESOURCE_TYPE_MBL_PORT )
							{
								pEvt->Params.Type1.FirstRes.un32MblTimeSlot++;
								if( pEvt->Params.Type1.FirstRes.un32MblTimeSlot > (VOIP_MAX_TIMESLOT_PER_MBL_STREAM - 1) )
								{
									pEvt->Params.Type1.FirstRes.un32MblStreamNb++;
									pEvt->Params.Type1.FirstRes.un32MblTimeSlot = 0;
									if( pEvt->Params.Type1.FirstRes.un32MblStreamNb > (VOIP_MAX_STREAM_PER_MBL_PORT - 1) )
									{
										pEvt->Params.Type1.FirstRes.un32MblPortNb++;
										pEvt->Params.Type1.FirstRes.un32MblStreamNb = 0;
										pEvt->Params.Type1.FirstRes.un32MblTimeSlot = 0;
									}
								}
							}

							/* Update second resource params for next connection */
							if( pEvt->Params.Type1.SecondRes.Type == VOIP_RESOURCE_TYPE_TRUNK )
							{
								PVOIP_TRUNK_CONFIG	pTrunkConfig = &in_pAdapterContext->CurrentConfig.aTrunk[pEvt->Params.Type1.SecondRes.un32TrunkNb];

								pEvt->Params.Type1.SecondRes.un32TimeSlot++;
								if( pEvt->Params.Type1.SecondRes.un32TimeSlot > pTrunkConfig->un32MaxTimeSlot )
								{
									pEvt->Params.Type1.SecondRes.un32TrunkNb++;
									pEvt->Params.Type1.SecondRes.un32TimeSlot = 1;
								}
							}
							else if( pEvt->Params.Type1.SecondRes.Type == VOIP_RESOURCE_TYPE_MBL_PORT )
							{
								pEvt->Params.Type1.SecondRes.un32MblTimeSlot++;
								if( pEvt->Params.Type1.SecondRes.un32MblTimeSlot > (VOIP_MAX_TIMESLOT_PER_MBL_STREAM - 1) )
								{
									pEvt->Params.Type1.SecondRes.un32MblStreamNb++;
									pEvt->Params.Type1.SecondRes.un32MblTimeSlot = 0;
									if( pEvt->Params.Type1.SecondRes.un32MblStreamNb > (VOIP_MAX_STREAM_PER_MBL_PORT - 1) )
									{
										pEvt->Params.Type1.SecondRes.un32MblPortNb++;
										pEvt->Params.Type1.SecondRes.un32MblStreamNb = 0;
										pEvt->Params.Type1.SecondRes.un32MblTimeSlot = 0;
									}
								}
							}
							else
							{
								pEvt->Params.Type1.SecondRes.un16RxIPPort += pEvt->un16IPPortIncrement;
								pEvt->Params.Type1.SecondRes.un16TxIPPort = pEvt->Params.Type1.SecondRes.un16RxIPPort;
							}
						}
					}
				}

			} break;

			/*
			 * Response from our "stream alloc" messages
			 */
			case TB640_MSG_ID_STREAM_RES_ALLOC:
			{
				/* Retrieve connection identifier */
				un32ConnectionId = (TBX_UINT32)TBX_MSG_USER_CONTEXT1_GET( in_hMsg );

				/* Handle this response */
				Result = VoipHandleAdapterStreamResAllocResponse( in_pAdapterContext, in_hMsg );
				if( TBX_RESULT_FAILURE( Result ) == TBX_TRUE )
				{
					TBX_EXIT_ERROR( Result, 0, "Failed to allocate stream resource!" );
				}

				/* Retrieve target connection context */
				Result = TBXHashFind( in_pAdapterContext->pTargetConfig->hConnectionHash, (TBX_HASH_KEY)un32ConnectionId, (PTBX_VOID*)&pTargetConnectionContext );
				if( TBX_RESULT_FAILURE( Result ) == TBX_TRUE )
				{
					TBX_EXIT_ERROR(TBX_RESULT_NOT_FOUND, 0, "Failed to found target connection context!" );
				}

				/* Update target connection context */
				pTargetConnectionContext->un32NbResponsesExpected--;
				if( pTargetConnectionContext->un32NbResponsesExpected == 0 )
				{
					/*
					 *	Allocate the connection context
					 */
					Result = VoipSendAdapterConnectionAlloc(
						in_pAdapterContext,
						pTargetConnectionContext);
					if( TBX_RESULT_FAILURE( Result ) )
					{
						TBX_EXIT_ERROR (Result, 0, "Failed to allocate connection");
					}
				}

			} break;

			/*
			 * Response from our "voice processing alloc" messages
			 */
			case TB640_MSG_ID_VP_GROUP_ALLOC:
			{
				/* Retrieve connection identifier */
				un32ConnectionId = (TBX_UINT32)TBX_MSG_USER_CONTEXT1_GET( in_hMsg );

				/* Handle this response */
				Result = VoipHandleAdapterVpGroupAllocResponse( in_pAdapterContext, in_hMsg );
				if( TBX_RESULT_FAILURE( Result ) == TBX_TRUE )
				{
					TBX_EXIT_ERROR( Result, 0, "Failed to allocate voice processing group!" );
				}

				/* Retrieve target connection context */
				Result = TBXHashFind( in_pAdapterContext->pTargetConfig->hConnectionHash, (TBX_HASH_KEY)un32ConnectionId, (PTBX_VOID*)&pTargetConnectionContext );
				if( TBX_RESULT_FAILURE( Result ) == TBX_TRUE )
				{
					TBX_EXIT_ERROR(TBX_RESULT_NOT_FOUND, 0, "Failed to found target connection context!" );
				}

⌨️ 快捷键说明

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