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

📄 tsx_uac_test.c

📁 基于sip协议的网络电话源码
💻 C
📖 第 1 页 / 共 3 页
字号:
			  tsx->status_code, 302));		test_complete = -761;	    }	    /* Must have correct retransmission count. */	    if (tsx->retransmit_count != 0) {		PJ_LOG(3,(THIS_FILE, 			  "    error: retransmit cnt is %d instead of %d",			  tsx->retransmit_count, 0));		test_complete = -762;	    }	    /* Must still keep last_tx */	    if (tsx->last_tx == NULL) {		PJ_LOG(3,(THIS_FILE, 			  "    error: transaction lost last_tx"));		test_complete = -763;	    }	    /* last_tx MUST be INVITE. 	     * (authorization depends on this behavior)	     */	    if (tsx->last_tx && tsx->last_tx->msg->line.req.method.id !=		PJSIP_INVITE_METHOD)	    {		PJ_LOG(3,(THIS_FILE, 			  "    error: last_tx is not INVITE"));		test_complete = -764;	    }	}	else if (tsx->state == PJSIP_TSX_STATE_TERMINATED) {	    test_complete = 1;	    /* Previous state must be COMPLETED. */	    if (e->body.tsx_state.prev_state != PJSIP_TSX_STATE_COMPLETED) {		test_complete = -767;	    }	    /* Status code must be 302. */	    if (tsx->status_code != 302) {		PJ_LOG(3,(THIS_FILE, 			  "    error: status code is %d instead of %d",			  tsx->status_code, 302));		test_complete = -768;	    }	}    }}/* * This timer callback is called to send delayed response. */struct response{    pjsip_response_addr	 res_addr;    pjsip_tx_data	*tdata;};static void send_response_callback( pj_timer_heap_t *timer_heap,				    struct pj_timer_entry *entry){    struct response *r = entry->user_data;    pjsip_transport *tp = r->res_addr.transport;    pjsip_endpt_send_response(endpt, &r->res_addr, r->tdata, NULL, NULL);    if (tp)	pjsip_transport_dec_ref(tp);}/* Timer callback to terminate a transaction. */static void terminate_tsx_callback( pj_timer_heap_t *timer_heap,				    struct pj_timer_entry *entry){    struct my_timer *m = (struct my_timer *)entry;    pjsip_transaction *tsx = pjsip_tsx_layer_find_tsx(&m->tsx_key, PJ_FALSE);    int status_code = entry->id;    if (tsx) {	pjsip_tsx_terminate(tsx, status_code);    }}#define DIFF(a,b)   ((a<b) ? (b-a) : (a-b))/* * This is the handler to receive message for this test. It is used to * control and verify the behavior of the message transmitted by the * transaction. */static pj_bool_t msg_receiver_on_rx_request(pjsip_rx_data *rdata){    if (pj_strcmp2(&rdata->msg_info.via->branch_param, TEST1_BRANCH_ID) == 0) {	/*	 * The TEST1_BRANCH_ID test performs the verifications for transaction	 * retransmission mechanism. It will not answer the incoming request	 * with any response.	 */	pjsip_msg *msg = rdata->msg_info.msg;	PJ_LOG(4,(THIS_FILE, "    received request"));	/* Only wants to take INVITE or OPTIONS method. */	if (msg->line.req.method.id != PJSIP_INVITE_METHOD &&	    msg->line.req.method.id != PJSIP_OPTIONS_METHOD)	{	    PJ_LOG(3,(THIS_FILE, "    error: received unexpected method %.*s",			  msg->line.req.method.name.slen,			  msg->line.req.method.name.ptr));	    test_complete = -600;	    return PJ_TRUE;	}	if (recv_count == 0) {	    recv_count++;	    //pj_gettimeofday(&recv_last);	    recv_last = rdata->pkt_info.timestamp;	} else {	    pj_time_val now;	    unsigned msec_expected, msec_elapsed;	    int max_received;	    //pj_gettimeofday(&now);	    now = rdata->pkt_info.timestamp;	    PJ_TIME_VAL_SUB(now, recv_last);	    msec_elapsed = now.sec*1000 + now.msec;	    ++recv_count;    	    msec_expected = (1<<(recv_count-2))*PJSIP_T1_TIMEOUT;	    if (msg->line.req.method.id != PJSIP_INVITE_METHOD) {		if (msec_expected > PJSIP_T2_TIMEOUT)		    msec_expected = PJSIP_T2_TIMEOUT;		max_received = 11;	    } else {		max_received = 7;	    }	    if (DIFF(msec_expected, msec_elapsed) > TEST1_ALLOWED_DIFF) {		PJ_LOG(3,(THIS_FILE,			  "    error: expecting retransmission no. %d in %d "			  "ms, received in %d ms",			  recv_count-1, msec_expected, msec_elapsed));		test_complete = -610;	    }	    	    if (recv_count > max_received) {		PJ_LOG(3,(THIS_FILE, 			  "    error: too many messages (%d) received",			  recv_count));		test_complete = -620;	    }	    //pj_gettimeofday(&recv_last);	    recv_last = rdata->pkt_info.timestamp;	}	return PJ_TRUE;    } else    if (pj_strcmp2(&rdata->msg_info.via->branch_param, TEST4_BRANCH_ID) == 0) {	/*	 * The TEST4_BRANCH_ID test simulates transport failure after several	 * retransmissions.	 */	recv_count++;	if (recv_count == TEST4_RETRANSMIT_CNT) {	    /* Simulate transport failure. */	    pjsip_loop_set_failure(loop, 2, NULL);	} else if (recv_count > TEST4_RETRANSMIT_CNT) {	    PJ_LOG(3,(THIS_FILE,"   error: not expecting %d-th packet!",		      recv_count));	    test_complete = -631;	}	return PJ_TRUE;    } else    if (pj_strcmp2(&rdata->msg_info.via->branch_param, TEST5_BRANCH_ID) == 0) {	/*	 * The TEST5_BRANCH_ID test simulates user terminating the transaction	 * after several retransmissions.	 */	recv_count++;	if (recv_count == TEST5_RETRANSMIT_CNT+1) {	    pj_str_t key;	    pjsip_transaction *tsx;	    pjsip_tsx_create_key( rdata->tp_info.pool, &key, PJSIP_ROLE_UAC,				  &rdata->msg_info.msg->line.req.method, rdata);	    tsx = pjsip_tsx_layer_find_tsx(&key, PJ_TRUE);	    if (tsx) {		pjsip_tsx_terminate(tsx, PJSIP_SC_REQUEST_TERMINATED);		pj_mutex_unlock(tsx->mutex);	    } else {		PJ_LOG(3,(THIS_FILE, "    error: uac transaction not found!"));		test_complete = -633;	    }	} else if (recv_count > TEST5_RETRANSMIT_CNT+1) {	    PJ_LOG(3,(THIS_FILE,"   error: not expecting %d-th packet!",		      recv_count));	    test_complete = -634;	}	return PJ_TRUE;    } else    if (pj_strcmp2(&rdata->msg_info.via->branch_param, TEST6_BRANCH_ID) == 0) {	/*	 * The TEST6_BRANCH_ID test successfull non-INVITE transaction.	 */	pj_status_t status;	recv_count++;	if (recv_count > 1) {	    PJ_LOG(3,(THIS_FILE,"   error: not expecting %d-th packet!",		      recv_count));	    test_complete = -635;	}	status = pjsip_endpt_respond_stateless(endpt, rdata, 202, NULL,					       NULL, NULL);	if (status != PJ_SUCCESS) {	    app_perror("    error: unable to send response", status);	    test_complete = -636;	}	return PJ_TRUE;    } else    if (pj_strcmp2(&rdata->msg_info.via->branch_param, TEST7_BRANCH_ID) == 0) {	/*	 * The TEST7_BRANCH_ID test successfull non-INVITE transaction	 * with provisional response.	 */	pj_status_t status;	pjsip_response_addr res_addr;	struct response *r;	pjsip_tx_data *tdata;	pj_time_val delay = { 2, 0 };	recv_count++;	if (recv_count > 1) {	    PJ_LOG(3,(THIS_FILE,"   error: not expecting %d-th packet!",		      recv_count));	    test_complete = -640;	    return PJ_TRUE;	}	/* Respond with provisional response */	status = pjsip_endpt_create_response(endpt, rdata, 100, NULL, &tdata);	pj_assert(status == PJ_SUCCESS);	status = pjsip_get_response_addr(tdata->pool, rdata, &res_addr);	pj_assert(status == PJ_SUCCESS);	status = pjsip_endpt_send_response(endpt, &res_addr, tdata, 					   NULL, NULL);	pj_assert(status == PJ_SUCCESS);	/* Create the final response. */	status = pjsip_endpt_create_response(endpt, rdata, 202, NULL, &tdata);	pj_assert(status == PJ_SUCCESS);	/* Schedule sending final response in couple of of secs. */	r = pj_pool_alloc(tdata->pool, sizeof(*r));	r->res_addr = res_addr;	r->tdata = tdata;	if (r->res_addr.transport)	    pjsip_transport_add_ref(r->res_addr.transport);	timer.entry.cb = &send_response_callback;	timer.entry.user_data = r;	pjsip_endpt_schedule_timer(endpt, &timer.entry, &delay);	return PJ_TRUE;    } else    if (pj_strcmp2(&rdata->msg_info.via->branch_param, TEST8_BRANCH_ID) == 0) {	/*	 * The TEST8_BRANCH_ID test failed INVITE transaction.	 */	pjsip_method *method;	pj_status_t status;	method = &rdata->msg_info.msg->line.req.method;	recv_count++;	if (method->id == PJSIP_INVITE_METHOD) {	    if (recv_count > 1) {		PJ_LOG(3,(THIS_FILE,"   error: not expecting %d-th packet!",			  recv_count));		test_complete = -635;	    }	    status = pjsip_endpt_respond_stateless(endpt, rdata, 301, NULL,						   NULL, NULL);	    if (status != PJ_SUCCESS) {		app_perror("    error: unable to send response", status);		test_complete = -636;	    }	} else if (method->id == PJSIP_ACK_METHOD) {	    if (recv_count == 2) {		pj_str_t key;		pj_time_val delay = { 5, 0 };				/* Schedule timer to destroy transaction after 5 seconds.		 * This is to make sure that transaction does not 		 * retransmit ACK.		 */		pjsip_tsx_create_key(rdata->tp_info.pool, &key,				     PJSIP_ROLE_UAC, &pjsip_invite_method,				     rdata);		pj_strcpy(&timer.tsx_key, &key);		timer.entry.id = 301;		timer.entry.cb = &terminate_tsx_callback;		pjsip_endpt_schedule_timer(endpt, &timer.entry, &delay);	    }	    if (recv_count > 2) {		PJ_LOG(3,(THIS_FILE,"   error: not expecting %d-th packet!",			  recv_count));		test_complete = -638;	    }	} else {	    PJ_LOG(3,(THIS_FILE,"   error: not expecting %s",		      pjsip_rx_data_get_info(rdata)));	    test_complete = -639;	}    } else    if (pj_strcmp2(&rdata->msg_info.via->branch_param, TEST9_BRANCH_ID) == 0) {	/*	 * The TEST9_BRANCH_ID test failed INVITE transaction with	 * provisional response.	 */	pjsip_method *method;	pj_status_t status;	method = &rdata->msg_info.msg->line.req.method;	recv_count++;	if (method->id == PJSIP_INVITE_METHOD) {	    pjsip_response_addr res_addr;	    struct response *r;	    pjsip_tx_data *tdata;	    pj_time_val delay = { 2, 0 };	    if (recv_count > 1) {		PJ_LOG(3,(THIS_FILE,"   error: not expecting %d-th packet!",			  recv_count));		test_complete = -650;		return PJ_TRUE;	    }	    /* Respond with provisional response */	    status = pjsip_endpt_create_response(endpt, rdata, 100, NULL, 						 &tdata);	    pj_assert(status == PJ_SUCCESS);	    status = pjsip_get_response_addr(tdata->pool, rdata, &res_addr);	    pj_assert(status == PJ_SUCCESS);	    status = pjsip_endpt_send_response(endpt, &res_addr, tdata, 					       NULL, NULL);	    pj_assert(status == PJ_SUCCESS);	    /* Create the final response. */	    status = pjsip_endpt_create_response(endpt, rdata, 302, NULL, 						 &tdata);	    pj_assert(status == PJ_SUCCESS);	    /* Schedule sending final response in couple of of secs. */	    r = pj_pool_alloc(tdata->pool, sizeof(*r));	    r->res_addr = res_addr;	    r->tdata = tdata;	    if (r->res_addr.transport)		pjsip_transport_add_ref(r->res_addr.transport);	    timer.entry.cb = &send_response_callback;	    timer.entry.user_data = r;	    pjsip_endpt_schedule_timer(endpt, &timer.entry, &delay);	} else if (method->id == PJSIP_ACK_METHOD) {	    if (recv_count == 2) {		pj_str_t key;		pj_time_val delay = { 5, 0 };				/* Schedule timer to destroy transaction after 5 seconds.		 * This is to make sure that transaction does not 		 * retransmit ACK.		 */		pjsip_tsx_create_key(rdata->tp_info.pool, &key,				     PJSIP_ROLE_UAC, &pjsip_invite_method,				     rdata);		pj_strcpy(&timer.tsx_key, &key);		timer.entry.id = 302;		timer.entry.cb = &terminate_tsx_callback;		pjsip_endpt_schedule_timer(endpt, &timer.entry, &delay);	    }	    if (recv_count > 2) {		PJ_LOG(3,(THIS_FILE,"   error: not expecting %d-th packet!",			  recv_count));		test_complete = -638;	    }	} else {	    PJ_LOG(3,(THIS_FILE,"   error: not expecting %s",		      pjsip_rx_data_get_info(rdata)));	    test_complete = -639;	}	return PJ_TRUE;    }    return PJ_FALSE;}/*  * The generic test framework, used by most of the tests.  */static int perform_tsx_test(int dummy, char *target_uri, char *from_uri, 			    char *branch_param, int test_time, 			    const pjsip_method *method){    pjsip_tx_data *tdata;    pjsip_transaction *tsx;    pj_str_t target, from, tsx_key;    pjsip_via_hdr *via;    pj_time_val timeout;    pj_status_t status;    PJ_LOG(3,(THIS_FILE, 	      "   please standby, this will take at most %d seconds..",	      test_time));    /* Reset test. */    recv_count = 0;    test_complete = 0;    /* Init headers. */    target = pj_str(target_uri);    from = pj_str(from_uri);    /* Create request. */    status = pjsip_endpt_create_request( endpt, method, &target,					 &from, &target, NULL, NULL, -1, 					 NULL, &tdata);    if (status != PJ_SUCCESS) {	app_perror("   Error: unable to create request", status);	return -100;    }

⌨️ 快捷键说明

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