res_pjsip_transport_websocket: Fix a progressive memory growth.
[asterisk/asterisk.git] / res / res_pjsip_transport_websocket.c
index 92e018d..b90b609 100644 (file)
@@ -90,6 +90,10 @@ static pj_status_t ws_destroy(pjsip_transport *transport)
 
        pjsip_endpt_release_pool(wstransport->transport.endpt, wstransport->transport.pool);
 
+       if (wstransport->rdata.tp_info.pool) {
+               pjsip_endpt_release_pool(wstransport->transport.endpt, wstransport->rdata.tp_info.pool);
+       }
+
        return PJ_SUCCESS;
 }
 
@@ -162,6 +166,15 @@ static int transport_create(void *data)
 
        pjsip_transport_register(newtransport->transport.tpmgr, (pjsip_transport *)newtransport);
 
+       newtransport->rdata.tp_info.transport = &newtransport->transport;
+       newtransport->rdata.tp_info.pool = pjsip_endpt_create_pool(endpt, "rtd%p",
+               PJSIP_POOL_RDATA_LEN, PJSIP_POOL_RDATA_INC);
+       if (!newtransport->rdata.tp_info.pool) {
+               ast_log(LOG_ERROR, "Failed to allocate WebSocket rdata.\n");
+               pjsip_endpt_release_pool(endpt, pool);
+               return -1;
+       }
+
        create_data->transport = newtransport;
        return 0;
 }
@@ -185,9 +198,6 @@ static int transport_read(void *data)
        int recvd;
        pj_str_t buf;
 
-       rdata->tp_info.pool = newtransport->transport.pool;
-       rdata->tp_info.transport = &newtransport->transport;
-
        pj_gettimeofday(&rdata->pkt_info.timestamp);
 
        pj_memcpy(rdata->pkt_info.packet, read_data->payload, sizeof(rdata->pkt_info.packet));
@@ -204,6 +214,8 @@ static int transport_read(void *data)
 
        recvd = pjsip_tpmgr_receive_packet(rdata->tp_info.transport->tpmgr, rdata);
 
+       pj_pool_reset(rdata->tp_info.pool);
+
        return (read_data->payload_len == recvd) ? 0 : -1;
 }