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

📄 timer.c

📁 用来作为linux中SIP SERVER,完成VOIP网络电话中服务器的功能
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * $Id: timer.c,v 1.58.2.1 2005/06/01 13:44:01 janakj Exp $ * * * Copyright (C) 2001-2003 FhG Fokus * * This file is part of ser, a free SIP server. * * ser 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 * * For a license to use the ser software under conditions * other than those described here, or to purchase support for this * software, please contact iptel.org by e-mail at the following addresses: *    info@iptel.org * * ser 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 *//*   timer.c is where we implement TM timers. It has been designed  for high performance using some techniques of which timer users  need to be aware.	One technique is "fixed-timer-length". We maintain separate 	timer lists, all of them include elements of the same time	to fire. That allows *appending* new events to the list as	opposed to inserting them by time, which is costly due to	searching time spent in a mutex. The performance benefit is	noticeable. The limitation is you need a new timer list for	each new timer length.	Another technique is the timer process slices off expired elements	from the list in a mutex, but executes the timer after the mutex	is left. That saves time greatly as whichever process wants to	add/remove a timer, it does not have to wait until the current	list is processed. However, be aware the timers may hit in a delayed	manner; you have no guarantee in your process that after resetting a timer, 	it will no more hit. It might have been removed by timer process,    and is waiting to be executed.  The following example shows it:			PROCESS1				TIMER PROCESS	0.								timer hits, it is removed from queue and									about to be executed	1.	process1 decides to		reset the timer 	2.								timer is executed now	3.	if the process1 naively		thinks the timer could not 		have been executed after 		resetting the timer, it is		WRONG -- it was (step 2.)	So be careful when writing the timer handlers. Currently defined timers 	don't hurt if they hit delayed, I hope at least. Retransmission timer 	may results in a useless retransmission -- not too bad. FR timer not too	bad either as timer processing uses a REPLY mutex making it safe to other	processing affecting transaction state. Wait timer not bad either -- processes	putting a transaction on wait don't do anything with it anymore.		Example when it does not hurt:			P1						TIMER	0.								RETR timer removed from list and									scheduled for execution	1. 200/BYE received->	   reset RETR, put_on_wait	2.								RETR timer executed -- too late but it does									not hurt	3.								WAIT handler executed	The rule of thumb is don't touch data you put under a timer. Create data,    put them under a timer, and let them live until they are safely destroyed from    wait/delete timer.  The only safe place to manipulate the data is     from timer process in which delayed timers cannot hit (all timers are    processed sequentially).	A "bad example" -- rewriting content of retransmission buffer	in an unprotected way is bad because a delayed retransmission timer might 	hit. Thats why our reply retransmission procedure is enclosed in 	a REPLY_LOCK.*//* * History: * -------- *  2003-06-27  timers are not unlinked if timerlist is 0 (andrei) *  2004-02-13  t->is_invite, t->local, t->noisy_ctimer replaced; *              timer_link.payload removed (bogdan) */#include "defs.h"#include "config.h"#include "h_table.h"#include "timer.h"#include "../../dprint.h"#include "lock.h"#include "t_stats.h"#include "../../hash_func.h"#include "../../dprint.h"#include "../../config.h"#include "../../parser/parser_f.h"#include "../../ut.h"#include "t_funcs.h"#include "t_reply.h"#include "t_cancel.h"static struct timer_table *timertable=0;static struct timer detached_timer; /* just to have a value to compare with*/#define DETACHED_LIST (&detached_timer)#define is_in_timer_list2(_tl) ( (_tl)->timer_list &&  \									((_tl)->timer_list!=DETACHED_LIST) )int noisy_ctimer=0;int timer_group[NR_OF_TIMER_LISTS] = {	TG_FR, TG_FR,	TG_WT,	TG_DEL,	TG_RT, TG_RT, TG_RT, TG_RT};/* default values of timeouts for all the timer list   (see timer.h for enumeration of timer lists)*/unsigned int timer_id2timeout[NR_OF_TIMER_LISTS] = {	FR_TIME_OUT, 		/* FR_TIMER_LIST */	INV_FR_TIME_OUT, 	/* FR_INV_TIMER_LIST */	WT_TIME_OUT, 		/* WT_TIMER_LIST */	DEL_TIME_OUT,		/* DELETE_LIST */	RETR_T1, 			/* RT_T1_TO_1 */	RETR_T1 << 1, 		/* RT_T1_TO_2 */	RETR_T1 << 2, 		/* RT_T1_TO_3 */	RETR_T2 			/* RT_T2 */						/* NR_OF_TIMER_LISTS */};/******************** handlers ***************************/static void unlink_timers( struct cell *t );static void delete_cell( struct cell *p_cell, int unlock ){#ifdef EXTRA_DEBUG	int i;#endif	/* there may still be FR/RETR timers, which have been reset	   (i.e., time_out==TIMER_DELETED) but are stilled linked to	   timer lists and must be removed from there before the	   structures are released	*/	unlink_timers( p_cell );#ifdef EXTRA_DEBUG	if (is_in_timer_list2(& p_cell->wait_tl )) {		LOG( L_ERR, "ERROR: transaction %p scheduled for deletion and"			" still on WAIT, timeout=%d\n", p_cell, p_cell->wait_tl.time_out);		abort();	}	if (is_in_timer_list2(& p_cell->uas.response.retr_timer )) {		LOG( L_ERR, "ERROR: transaction %p scheduled for deletion and"			" still on RETR (rep), timeout=%d\n",			p_cell, p_cell->uas.response.retr_timer.time_out);		abort();	}	if (is_in_timer_list2(& p_cell->uas.response.fr_timer )) {		LOG( L_ERR, "ERROR: transaction %p scheduled for deletion and"			" still on FR (rep), timeout=%d\n", p_cell,			p_cell->uas.response.fr_timer.time_out);		abort();	}	for (i=0; i<p_cell->nr_of_outgoings; i++) {		if (is_in_timer_list2(& p_cell->uac[i].request.retr_timer)) {			LOG( L_ERR, "ERROR: transaction %p scheduled for deletion and"				" still on RETR (req %d), timeout %d\n", p_cell, i,				p_cell->uac[i].request.retr_timer.time_out);			abort();		}		if (is_in_timer_list2(& p_cell->uac[i].request.fr_timer)) {			LOG( L_ERR, "ERROR: transaction %p scheduled for deletion and"				" still on FR (req %d), timeout %d\n", p_cell, i,				p_cell->uac[i].request.fr_timer.time_out);			abort();		}		if (is_in_timer_list2(& p_cell->uac[i].local_cancel.retr_timer)) {			LOG( L_ERR, "ERROR: transaction %p scheduled for deletion and"				" still on RETR/cancel (req %d), timeout %d\n", p_cell, i,				p_cell->uac[i].request.retr_timer.time_out);			abort();		}		if (is_in_timer_list2(& p_cell->uac[i].local_cancel.fr_timer)) {			LOG( L_ERR, "ERROR: transaction %p scheduled for deletion and"				" still on FR/cancel (req %d), timeout %d\n", p_cell, i,				p_cell->uac[i].request.fr_timer.time_out);			abort();		}	}	/* reset_retr_timers( hash__XX_table, p_cell ); */#endif	/* still in use ... don't delete */	if ( IS_REFFED_UNSAFE(p_cell) ) {		if (unlock) UNLOCK_HASH(p_cell->hash_index);		DBG("DEBUG: delete_cell %p: can't delete -- still reffed\n",			p_cell);		/* it's added to del list for future del */		set_timer( &(p_cell->dele_tl), DELETE_LIST, 0 );	} else {		if (unlock) UNLOCK_HASH(p_cell->hash_index);		DBG("DEBUG: delete transaction %p\n", p_cell );		free_cell( p_cell );	}}static void fake_reply(struct cell *t, int branch, int code ){	branch_bm_t cancel_bitmap;	short do_cancel_branch;	enum rps reply_status;	do_cancel_branch = is_invite(t) && should_cancel_branch(t, branch);	cancel_bitmap=do_cancel_branch ? 1<<branch : 0;	if ( is_local(t) ) {		reply_status=local_reply( t, FAKED_REPLY, branch, 					  code, &cancel_bitmap );		if (reply_status==RPS_COMPLETED) {			     /* don't need to cleanup uac_timers -- they were cleaned				branch by branch and this last branch's timers are				reset now too			     */			     /* don't need to issue cancels -- local cancels have been				issued branch by branch and this last branch was				canceled now too			     */			     /* then the only thing to do now is to put the transaction				on FR/wait state 			     */			     /* there is no need to call set_final_timer here because			      * the transaction is known to be local			      */			put_on_wait(t);		}	} else {		reply_status=relay_reply( t, FAKED_REPLY, branch, code,			&cancel_bitmap );#if 0		if (reply_status==RPS_COMPLETED) {			     /* don't need to cleanup uac_timers -- they were cleaned				branch by branch and this last branch's timers are				reset now too			     */			     /* don't need to issue cancels -- local cancels have been				issued branch by branch and this last branch was				canceled now too			     */			     /* then the only thing to do now is to put the transaction				on FR/wait state 			     */						     /* call to set_final_timer is embedded in relay_reply to avoid			      * race conditions where a reply would be sent but retransmission			      * timers would be set afterwards (which can cause troubles when a			      * reply spirals through the same instance twice and kernel switches			      * context immediately after message has been sent.			      */		}#endif	}	/* now when out-of-lock do the cancel I/O */	if (do_cancel_branch) cancel_branch(t, branch );	/* it's cleaned up on error; if no error occurred and transaction	   completed regularly, I have to clean-up myself	*/}inline static void retransmission_handler( struct timer_link *retr_tl ){	struct retr_buf* r_buf ;	enum lists id;	r_buf = get_retr_timer_payload(retr_tl);#ifdef EXTRA_DEBUG	if (r_buf->my_T->damocles) {		LOG( L_ERR, "ERROR: transaction %p scheduled for deletion and"			" called from RETR timer\n",r_buf->my_T);		abort();	}	#endif	/*the transaction is already removed from RETRANSMISSION_LIST by timer*/	/* retransmission */	if ( r_buf->activ_type==TYPE_LOCAL_CANCEL 		|| r_buf->activ_type==TYPE_REQUEST ) {			DBG("DEBUG: retransmission_handler : "				"request resending (t=%p, %.9s ... )\n", 				r_buf->my_T, r_buf->buffer);			if (SEND_BUFFER( r_buf )==-1) {				reset_timer( &r_buf->fr_timer );				fake_reply(r_buf->my_T, r_buf->branch, 503 );				return;			}	} else {			DBG("DEBUG: retransmission_handler : "				"reply resending (t=%p, %.9s ... )\n", 				r_buf->my_T, r_buf->buffer);			t_retransmit_reply(r_buf->my_T);	}	id = r_buf->retr_list;	r_buf->retr_list = id < RT_T2 ? id + 1 : RT_T2;		retr_tl->timer_list= NULL; /* set to NULL so that set_timer will work */	set_timer( retr_tl, id < RT_T2 ? id + 1 : RT_T2, 0 );	DBG("DEBUG: retransmission_handler : done\n");}inline static void final_response_handler( struct timer_link *fr_tl ){	int silent;	struct retr_buf* r_buf;	struct cell *t;	if (fr_tl==0){		/* or BUG?, ignoring it for now */		LOG(L_CRIT, "ERROR: final_response_handler(0) called\n");		return;	}	r_buf = get_fr_timer_payload(fr_tl);	t=r_buf->my_T;#	ifdef EXTRA_DEBUG	if (t->damocles) 	{		LOG( L_ERR, "ERROR: transaction %p scheduled for deletion and"			" called from FR timer\n",r_buf->my_T);		abort();	}#	endif	reset_timer(  &(r_buf->retr_timer) );	/* the transaction is already removed from FR_LIST by the timer */	/* FR for local cancels.... */	if (r_buf->activ_type==TYPE_LOCAL_CANCEL)	{		DBG("DEBUG: final_response_handler: stop retr for Local Cancel\n");		return;	}	/* FR for replies (negative INVITE replies) */	if (r_buf->activ_type>0) {#		ifdef EXTRA_DEBUG		if (t->uas.request->REQ_METHOD!=METHOD_INVITE			|| t->uas.status < 200 ) {			LOG(L_ERR, "ERROR: final_response_handler: unknown type reply buffer\n");			abort();		}#		endif		put_on_wait( t );		return;	};	/* lock reply processing to determine how to proceed reliably */	LOCK_REPLIES( t );	/* now it can be only a request retransmission buffer;	   try if you can simply discard the local transaction 	   state without compellingly removing it from the	   world */	silent=		/* not for UACs */		!is_local(t)		/* invites only */		&& is_invite(t)		/* parallel forking does not allow silent state discarding */		&& t->nr_of_outgoings==1		/* on_negativ reply handler not installed -- serial forking 		 * could occur otherwise */		&& t->on_negative==0		/* the same for FAILURE callbacks */		&& !has_tran_tmcbs( t, TMCB_ON_FAILURE_RO|TMCB_ON_FAILURE) 		/* something received -- we will not be silent on error */		&& t->uac[r_buf->branch].last_received>0		/* don't go silent if disallowed globally ... */		&& noisy_ctimer==0		/* ... or for this particular transaction */		&& has_noisy_ctimer(t);	if (silent) {		UNLOCK_REPLIES(t);		DBG("DEBUG: final_response_handler: transaction silently dropped (%p)\n",t);		put_on_wait( t );		return;	}	DBG("DEBUG: final_response_handler:stop retr. and send CANCEL (%p)\n", t);	fake_reply(t, r_buf->branch, 408 );	DBG("DEBUG: final_response_handler : done\n");}void cleanup_localcancel_timers( struct cell *t ){	int i;	for (i=0; i<t->nr_of_outgoings; i++ )  {		reset_timer(  &t->uac[i].local_cancel.retr_timer );		reset_timer(  &t->uac[i].local_cancel.fr_timer );	}}inline static void wait_handler( struct timer_link *wait_tl ){	struct cell *p_cell;	p_cell = get_wait_timer_payload( wait_tl );#ifdef EXTRA_DEBUG	if (p_cell->damocles) {		LOG( L_ERR, "ERROR: transaction %p scheduled for deletion and"			" called from WAIT timer\n",p_cell);		abort();	}		DBG("DEBUG: WAIT timer hit\n");#endif	/* stop cancel timers if any running */	if ( is_invite(p_cell) ) cleanup_localcancel_timers( p_cell );	/* the transaction is already removed from WT_LIST by the timer */	/* remove the cell from the hash table */	DBG("DEBUG: wait_handler : removing %p from table \n", p_cell );	LOCK_HASH( p_cell->hash_index );	remove_from_hash_table_unsafe(  p_cell );	/* jku: no more here -- we do it when we put a transaction on wait */#ifdef EXTRA_DEBUG	p_cell->damocles = 1;#endif	/* delete (returns with UNLOCK-ed_HASH) */	delete_cell( p_cell, 1 /* unlock on return */ );	DBG("DEBUG: wait_handler : done\n");}inline static void delete_handler( struct timer_link *dele_tl ){	struct cell *p_cell;	p_cell = get_dele_timer_payload( dele_tl );	DBG("DEBUG: delete_handler : removing %p \n", p_cell );#ifdef EXTRA_DEBUG	if (p_cell->damocles==0) {		LOG( L_ERR, "ERROR: transaction %p not scheduled for deletion"			" and called from DELETE timer\n",p_cell);		abort();	}	#endif

⌨️ 快捷键说明

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