示例#1
0
/*
 * this function is to avoid logging too much or non-ascii data received.
 */
static char *make_logbuf(belle_sip_log_level level, const char *buffer, size_t size){
	char *logbuf;
	char truncate_msg[128]={0};
	int limit=7000; /*big message when many ice candidates*/

	if (!belle_sip_log_level_enabled(level)){
		return belle_sip_malloc0(1);
	}
	limit=find_non_printable(buffer,MIN(size,limit));
	if (limit==0){
		snprintf(truncate_msg,sizeof(truncate_msg)-1,"... (binary data)");
	} else if (size>limit){
		snprintf(truncate_msg,sizeof(truncate_msg)-1,"... (first %i bytes shown)",limit);
		size=limit;
	}

	if (truncate_msg[0]!=0){
		size+=sizeof(truncate_msg);
	}

	logbuf=belle_sip_malloc(size+1);
	strncpy(logbuf,buffer,limit);
	if (truncate_msg[0]!=0){
		strncpy(logbuf+limit,truncate_msg,sizeof(truncate_msg));
	}
	logbuf[size]='\0';
	return logbuf;
}
示例#2
0
/*
 * this function is to avoid logging too much or non-ascii data received.
 */
static char *make_logbuf(belle_sip_log_level level, const char *buffer, size_t size){
	char *logbuf;
	char truncate_msg[128]={0};
	int limit=3000;
	size_t non_ascii_pos;
	
	if (!belle_sip_log_level_enabled(level)){
		return belle_sip_malloc0(1);
	}
	non_ascii_pos=find_non_asci(buffer,MIN(size,limit));
	if (non_ascii_pos<2){
		limit=0;
		snprintf(truncate_msg,sizeof(truncate_msg)-1,"... (binary data)");
	}else limit=non_ascii_pos;
	if (limit!=0 && size>limit){
		snprintf(truncate_msg,sizeof(truncate_msg)-1," ... (first %i bytes shown)",limit);
		size=limit;
	}
	if (size<limit)
		limit=size;
	if (truncate_msg[0]!=0){
		size+=sizeof(truncate_msg);
	}
	logbuf=belle_sip_malloc(size+1);
	strncpy(logbuf,buffer,limit);
	if (truncate_msg[0]!=0){
		strncpy(logbuf+limit,truncate_msg,sizeof(truncate_msg));
	}
	logbuf[size]='\0';
	return logbuf;
}
示例#3
0
void belle_sip_logv(int level, const char *fmt, va_list args)
{
	if (belle_sip_logv_out!=NULL && belle_sip_log_level_enabled(level))
		belle_sip_logv_out(level,fmt,args);
	if ((level)==BELLE_SIP_LOG_FATAL) abort();
}
示例#4
0
static void belle_sip_main_loop_iterate(belle_sip_main_loop_t *ml){
	size_t pfd_size = ml->nsources * sizeof(belle_sip_pollfd_t);
	belle_sip_pollfd_t *pfd=(belle_sip_pollfd_t*)belle_sip_malloc0(pfd_size);
	int i=0;
	belle_sip_source_t *s;
	belle_sip_list_t *elem,*next;
	uint64_t min_time_ms=(uint64_t)-1;
	int duration=-1;
	int ret;
	uint64_t cur;
	belle_sip_list_t *to_be_notified=NULL;
	int can_clean=belle_sip_object_pool_cleanable(ml->pool); /*iterate might not be called by the thread that created the main loop*/ 
	belle_sip_object_pool_t *tmp_pool=NULL;
	
	if (!can_clean){
		/*Push a temporary pool for the time of the iterate loop*/
		tmp_pool=belle_sip_object_pool_push();
	}
	
	/*Step 1: prepare the pollfd table and get the next timeout value */
	for(elem=ml->sources;elem!=NULL;elem=next){
		next=elem->next;
		s=(belle_sip_source_t*)elem->data;
		if (!s->cancelled){
			if (s->fd!=(belle_sip_fd_t)-1){
				belle_sip_source_to_poll(s,pfd,i);
				++i;
			}
			if (s->timeout>=0){
				if (min_time_ms>s->expire_ms){
					min_time_ms=s->expire_ms;
				}
			}
		}
	}
	
	if (min_time_ms!=(uint64_t)-1 ){
		int64_t diff;
		/* compute the amount of time to wait for shortest timeout*/
		cur=belle_sip_time_ms();
		diff=min_time_ms-cur;
		if (diff>0)
			duration=(int)diff;
		else 
			duration=0;
	}
	/* do the poll */
	ret=belle_sip_poll(pfd,i,duration);
	if (ret==-1){
		goto end;
	}
	
	/* Step 2: examine poll results and determine the list of source to be notified */
	cur=belle_sip_time_ms();
	for(elem=ml->sources;elem!=NULL;elem=elem->next){
		unsigned revents=0;
		s=(belle_sip_source_t*)elem->data;

		if (!s->cancelled){
			if (s->fd!=(belle_sip_fd_t)-1){
				if (s->notify_required) { /*for testing purpose to force channel to read*/
					revents=BELLE_SIP_EVENT_READ;
					s->notify_required=0; /*reset*/
				} else {
					revents=belle_sip_source_get_revents(s,pfd);
				}
				s->revents=revents;
			}
			if (revents!=0 || (s->timeout>=0 && cur>=s->expire_ms)){
				to_be_notified=belle_sip_list_append(to_be_notified,belle_sip_object_ref(s));
				s->expired=TRUE;
				if (s->revents==0)
					s->revents=BELLE_SIP_EVENT_TIMEOUT;
			}
		}else to_be_notified=belle_sip_list_append(to_be_notified,belle_sip_object_ref(s));
	}
	
	/* Step 3: notify those to be notified */
	for(elem=to_be_notified;elem!=NULL;){
		s=(belle_sip_source_t*)elem->data;
		next=elem->next;
		if (!s->cancelled){
			
			if (s->timeout > 0 && belle_sip_log_level_enabled(BELLE_SIP_LOG_DEBUG)) {
				/*to avoid too many traces*/ 
				char *objdesc=belle_sip_object_to_string((belle_sip_object_t*)s);
				belle_sip_debug("source %s notified revents=%u, timeout=%i",objdesc,revents,s->timeout);
				belle_sip_free(objdesc);
			}
			
			ret=s->notify(s->data,s->revents);
			if (ret==BELLE_SIP_STOP || s->oneshot){
				/*this source needs to be removed*/
				belle_sip_main_loop_remove_source(ml,s);
			}else if (s->revents==BELLE_SIP_EVENT_TIMEOUT){
				/*timeout needs to be started again */
				if (ret==BELLE_SIP_CONTINUE_WITHOUT_CATCHUP){
					s->expire_ms=cur+s->timeout;
				}else{
					s->expire_ms+=s->timeout;
				}
				s->expired=FALSE;
			}
		}else belle_sip_main_loop_remove_source(ml,s);
		belle_sip_object_unref(s);
		belle_sip_free(elem); /*free just the element*/
		elem=next;
	}
	
	if (can_clean) belle_sip_object_pool_clean(ml->pool);
	else if (tmp_pool) belle_sip_object_unref(tmp_pool);
end:
	belle_sip_free(pfd);
}