[asterisk-commits] mmichelson: branch group/pimp_my_sip r379701 - /team/group/pimp_my_sip/res/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Jan 21 10:52:32 CST 2013


Author: mmichelson
Date: Mon Jan 21 10:52:29 2013
New Revision: 379701

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=379701
Log:
Make the monitor thread stoppable.

This makes it so that if transports fail to start on module load, then
there won't be a crash after we destroy the PJSIP endpoint.


Modified:
    team/group/pimp_my_sip/res/res_sip.c

Modified: team/group/pimp_my_sip/res/res_sip.c
URL: http://svnview.digium.com/svn/asterisk/team/group/pimp_my_sip/res/res_sip.c?view=diff&rev=379701&r1=379700&r2=379701
==============================================================================
--- team/group/pimp_my_sip/res/res_sip.c (original)
+++ team/group/pimp_my_sip/res/res_sip.c Mon Jan 21 10:52:29 2013
@@ -465,10 +465,11 @@
 pj_caching_pool caching_pool;
 pj_pool_t *memory_pool;
 pj_thread_t *monitor_thread;
+static int monitor_continue;
 
 static void *monitor_thread_exec(void *endpt)
 {
-	for (;;) {
+	while (monitor_continue) {
 		const pj_time_val delay = {0, 10};
 		pjsip_endpt_handle_events(ast_pjsip_endpoint, &delay);
 	}
@@ -485,15 +486,27 @@
 	pjsip_transport *hardcoded_transport = NULL;
 	pj_sockaddr addr;
 	pj_str_t home = { "172.16.1.6:5060", 15 };
+	pj_status_t status;
+	char errbuf[20];
 
 	if (pj_sockaddr_parse(pj_AF_UNSPEC(), 0, &home, &addr) != PJ_SUCCESS) {
-		return -1;
-	}
-
-	if (pjsip_udp_transport_start(ast_pjsip_endpoint, &addr.ipv4, NULL, 2, &hardcoded_transport) != PJ_SUCCESS) {
-		return -1;
-	}
-	return 0;
+		ast_log(LOG_ERROR, "Failed to parse IP address\n");
+		return -1;
+	}
+
+	status = pjsip_udp_transport_start(ast_pjsip_endpoint, &addr.ipv4, NULL, 2, &hardcoded_transport);
+	if (status != PJ_SUCCESS) {
+		pj_strerror(status, errbuf, sizeof(errbuf));
+		ast_log(LOG_ERROR, "Failed to start PJSIP UDP transport, %s\n", errbuf);
+		return -1;
+	}
+	return 0;
+}
+
+static void stop_monitor_thread(void)
+{
+	monitor_continue = 0;
+	pj_thread_join(monitor_thread);
 }
 
 static int load_module(void)
@@ -528,13 +541,17 @@
 	}
 
 	pj_caching_pool_init(&caching_pool, NULL, 1024 * 1024);
-	pjsip_endpt_create(&caching_pool.factory, "SIP", &ast_pjsip_endpoint);
+	if (pjsip_endpt_create(&caching_pool.factory, "SIP", &ast_pjsip_endpoint) != PJ_SUCCESS) {
+		ast_log(LOG_ERROR, "Failed to create PJSIP endpoint structure. Aborting load\n");
+		goto error;
+	}
 	memory_pool = pj_pool_create(&caching_pool.factory, "SIP", 1024, 1024, NULL);
 	if (!memory_pool) {
 		ast_log(LOG_ERROR, "Failed to create memory pool for SIP. Aborting load\n");
 		goto error;
 	}
 
+	monitor_continue = 1;
 	status = pj_thread_create(memory_pool, "SIP", (pj_thread_proc *) &monitor_thread_exec,
 			NULL, PJ_THREAD_DEFAULT_STACK_SIZE * 2, 0, &monitor_thread);
 	if (status != PJ_SUCCESS) {
@@ -548,6 +565,9 @@
 	return AST_MODULE_LOAD_SUCCESS;
 
 error:
+	if (monitor_thread) {
+		stop_monitor_thread();
+	}
 	if (memory_pool) {
 		pj_pool_release(memory_pool);
 	}
@@ -561,7 +581,16 @@
 
 static int unload_module(void)
 {
-	/* XXX stub */
+	if (monitor_thread) {
+		stop_monitor_thread();
+	}
+	if (memory_pool) {
+		pj_pool_release(memory_pool);
+	}
+	if (ast_pjsip_endpoint) {
+		pjsip_endpt_destroy(ast_pjsip_endpoint);
+	}
+	pj_caching_pool_destroy(&caching_pool);
 	return 0;
 }
 




More information about the asterisk-commits mailing list