pjsip学习 ------ 三

本文详细介绍了pjsip中定时器的使用方法,包括创建timer、设置回调函数、启动定时器以及持续轮询的实现。pjsip的定时器基于ACE库的ACE_Timer_Heap,为适应库风格进行了少量修改。通过创建pj_timer_heap_t实例,设置pj_timer_entry和回调函数,然后启动定时器并进行轮询,可以实现定时任务的执行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


这次我们来说一下pjsip中的定时器timer.

pjsip中的定时器来自于ACE库。( The timer scheduling implementation here is based on ACE library's 

  ACE_Timer_Heap, with only little modification to suit our library's style ) http://www.cs.wustl.edu/~schmidt/ACE.html


       如何使用

1.  创建timer  

pj_timer_heap_t  *timer ;

pj_timer_heap_create(pool , 1 , &timer ) ;

       2.  设置entry 及回调函数。

             pj_timer_entry   *entry ;

            static  void timer_callback(pj_timer_heap_t *ht , pj_timer_entry *en)
{

PJ_LOG(3 , ("timer" , "timercallback")) ;
}

      entry = (pj_timer_entry*)pj_pool_calloc(pool , 1 , sizeof(*entry)) ;

             entry->cb = &timer_callback ;

        3.启动定时器。

pj_time_val  delay ;

delay.sec = 1 ;
delay.msec = 0 ;
pj_timer_heap_schedule(timer , &entry, &delay) ;

       4.  当然你需要不停的去poll这个timer(和ioqueue一样).

另起一个线程

while(!quit_flag)
{

pj_timer_heap_poll(timer , NULL ) ;

PJ_LOG(3,("test" , "printf")) ;
}

#include "pjlib.h"



pj_timer_heap_t  *timer ;

int  quit_flag = 0 ;

static  void timer_callback(pj_timer_heap_t *ht , pj_timer_entry *en)
{
	pj_time_val  delay ;
	PJ_LOG(3 , ("timer" , "timercallback")) ;
	delay.sec = 1 ;
    delay.msec = 0 ;
    pj_timer_heap_schedule(ht , en , &delay) ;

}



  
static  int  main_thread_fun(void *arg)
{

	while(!quit_flag)
	{

		pj_time_val  timeout = {0 , 500 };

		pj_timer_heap_poll(timer , NULL ) ;

		PJ_LOG(3,("test" , "printf")) ;
	}


	return 0 ;
}
int main(){

	pj_status_t  status ;
	pj_caching_pool  cp ;
	pj_pool_t       *pool ;
	pj_timer_entry   *entry ;	
	pj_time_val     delay ;
	pj_thread_t   * thread ;
	status = pj_init();
	pj_caching_pool_init(&cp , NULL , 0 ) ;

	pool = pj_pool_create(&cp.factory , NULL , 512 , 512 , NULL ) ;	

	entry = (pj_timer_entry*)pj_pool_calloc(pool , 1 , sizeof(*entry)) ;

	status = pj_timer_heap_create(pool , 1 , &timer ) ;
	
	entry[0].cb = &timer_callback ;		
	
	delay.sec = 1 ;
	delay.msec = 0 ;

	pj_timer_heap_schedule(timer , &entry[0] , &delay) ;
	
	pj_thread_create(pool , NULL , main_thread_fun , NULL , 0 , 0 , &thread) ;

	while(!quit_flag)
	{

		pj_thread_sleep(1000) ;

	}

	
	pj_shutdown();

	return 0 ;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值