[doubango Programmer's Guide] 4.3.2 tinySigComp -->SigComp UDP compression 翻译

来源:互联网 发布:艺术签名php源码 编辑:程序博客网 时间:2024/06/06 14:25

 原文:http://doubango.org/API/tinySigComp/tcomp_udp_compression_page.html

SigComp之UDP压缩

-------------------

对于压缩一个SIP消息体并以UDP的方式发送出去是很就简单的。由于tinySigComp是线程安全的,所以压缩过程能够在多线程程序中很安全的完成。为了能编译你的代码,你需要做以下与tinySigComp相关的操作。

头文件包含:

 #include "tsk_debug.h" // tinySAK 调试函数 #include "tcomp_manager.h" // tinySigComp API 接口函数.


分割标识符:用在SIP消息体内(sigomp-id) 并且tinySigComp分配和释放内存与此标识符有关。

 #define COMPARTMENT_ID         "urn:uuid:2e5fdc76-00be-4314-8202-1116fa82a475"


准备:

 #define MAX_BUFFER_SIZE 0xFFFF  int i = 0; tsk_size_t outLen = 0; tcomp_result_t *result = 0; char outputBuffer[MAX_BUFFER_SIZE];   tcomp_manager_handle_t *manager = 0; // Create SigComp manager manager = TCOMP_MANAGER_CREATE(); // Add SIP/Presence dictionnaries (not mandatory) tcomp_manager_addSipSdpDictionary(manager); tcomp_manager_addPresenceDictionary(manager); // Create result object and set compartment id --> It is recomanded to use one result object per manager. result = TCOMP_RESULT_CREATE(); tcomp_result_setCompartmentId(result, COMPARTMENT_ID, strlen(COMPARTMENT_ID)); // Set user parameters (not mandatory) tcomp_manager_setDecompression_Memory_Size(manager, 8192); tcomp_manager_setCycles_Per_Bit(manager, 64); tcomp_manager_setState_Memory_Size(manager, 8192);

利用如下方式压缩一个或者多个消息体:

// Compress the SIP message outLen = tcomp_manager_compress(manager,  COMPARTMENT_ID, strlen(COMPARTMENT_ID), // Compartment "REGISTER ...", strlen("REGISTER ..."), // Sip message to compress and it's size outputBuffer, sizeof(outputBuffer), // The ouptut buffer and it's size FALSE // Indicates whether to compress as stream (SCTP, TCP...) message or not );  if(outLen){                // send SigComp message over UDP connection                sendto(sock, outputBuffer, outLen , 0, (SOCKADDR *)address, sizeof(address));        }        else{                // MUST never happen. }


安全的释放所有资源:

 // Close the compartment tcomp_manager_closeCompartment(manager, COMPARTMENT_ID, strlen(COMPARTMENT_ID)); // Delete the result object TSK_OBJECT_SAFE_FREE(result); // Delete the manager TSK_OBJECT_SAFE_FREE(manager);--------------------------------------------------------------------------------