Ticket #390: Register session will keep transport instance so that keep-alive is sent

git-svn-id: https://svn.pjsip.org/repos/pjproject/trunk@1472 74dad513-b988-da41-8d7b-12977e46ad98
diff --git a/pjsip/src/pjsip-ua/sip_reg.c b/pjsip/src/pjsip-ua/sip_reg.c
index bbabf0c..7e06153 100644
--- a/pjsip/src/pjsip-ua/sip_reg.c
+++ b/pjsip/src/pjsip-ua/sip_reg.c
@@ -84,6 +84,11 @@
 
     /* Transport selector */
     pjsip_tpselector		 tp_sel;
+
+    /* Last transport used. We acquire the transport to keep
+     * it open.
+     */
+    pjsip_transport		*last_transport;
 };
 
 
@@ -133,6 +138,10 @@
 	regc->cb = NULL;
     } else {
 	pjsip_tpselector_dec_ref(&regc->tp_sel);
+	if (regc->last_transport) {
+	    pjsip_transport_dec_ref(regc->last_transport);
+	    regc->last_transport = NULL;
+	}
 	pjsip_endpt_release_pool(regc->endpt, regc->pool);
     }
 
@@ -609,6 +618,19 @@
     pj_assert(regc->has_tsx);
     regc->has_tsx = PJ_FALSE;
 
+    /* Add reference to the transport */
+    if (tsx->transport != regc->last_transport) {
+	if (regc->last_transport) {
+	    pjsip_transport_dec_ref(regc->last_transport);
+	    regc->last_transport = NULL;
+	}
+
+	if (tsx->transport) {
+	    regc->last_transport = tsx->transport;
+	    pjsip_transport_add_ref(regc->last_transport);
+	}
+    }
+
     /* Handle 401/407 challenge (even when _delete_flag is set) */
     if (tsx->status_code == PJSIP_SC_PROXY_AUTHENTICATION_REQUIRED ||
 	tsx->status_code == PJSIP_SC_UNAUTHORIZED)