gcc (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1
c89
只是想知道是否有更好的方法可以使用我在下面提供的代码执行此操作。
我正在从一些参数构建一个sdp(会话描述协议)字符串。
但是,我可能需要扩展sdp以包含其他参数,即视频编解码器。
但是,我真的不想要另一个if else来构建我在下面完成的完整字符串。
我只是想知道这是否足够可扩展?我能用的技术是否比我所做的更好。
我刚刚复制了执行sdp构建的函数:
void create_sdp_string(char *sdp_string, char reinvite)
{
char session_id[MAX_STRING_LEN];
char session_version[MAX_STRING_LEN];
const char *local_ip_addr = "10.10.10.244";
apr_time_t time_usec = 0;
char session_identifier[MAX_STRING_LEN];
char media_transport[MAX_STRING_LEN];
char connection_info[MAX_STRING_LEN];
const char *audio_port = "49152";
/* Required sdp attributes */
#define V_PROTOCOL_VERSION "0"
#define USERNAME "JOEBLOGGS"
#define NETTYPE "IN"
#define ADDR_TYPE "IP4"
#define S_SESSION_NAME "SIP_CALL"
#define T_TIME_DESCRIPTION "0 0"
#define M_MEDIA_NAME_TRANSPORT_ADDR "RTP/AVP 0 8 101"
#define A_PCMU "rtpmap:0 PCMU/8000"
#define A_PCMA "rtpmap:8 PCMA/8000"
#define A_TELEPHONE "rtpmap:101 telephone-event/8000"
/* Get the time in micro seconds to create an unique session id */
time_usec = apr_time_usec(apr_time_now());
apr_snprintf(session_id, MAX_STRING_LEN, "%lu", (unsigned long)time_usec);
/* Get the time in micro seconds to create an unique session version */
time_usec = apr_time_usec(apr_time_now());
apr_snprintf(session_version, MAX_STRING_LEN, "%lu", (unsigned long)time_usec);
/* Build session identifier */
apr_snprintf(session_identifier,
MAX_STRING_LEN,
"o="USERNAME" "NETTYPE" %s %s "ADDR_TYPE" %s\n",
session_id,
session_version,
local_ip_addr);
/* Build media transport */
apr_snprintf(media_transport,
MAX_STRING_LEN,
"m=audio %s "M_MEDIA_NAME_TRANSPORT_ADDR"\n",
audio_port);
/* Build connection information */
apr_snprintf(connection_info,
MAX_STRING_LEN,
"c="NETTYPE" "ADDR_TYPE" %s\n",
local_ip_addr);
if(reinvite == FALSE) {
/* Build complete sdp string for cases that are not reinvite */
apr_snprintf(sdp_string,
MAX_SDP_STRING_LEN,
"v="V_PROTOCOL_VERSION"\n"
"%s"
"s="S_SESSION_NAME"\n"
"%s"
"t="T_TIME_DESCRIPTION"\n"
"%s"
"a="A_PCMU"\n"
"a="A_PCMA"\n"
"a="A_TELEPHONE"\n",
session_identifier,
connection_info,
media_transport);
}
else if(reinvite == TRUE) {
/* Build complete sdp string for cases that are for reinvite 'inactive' */
#define INACTIVE "inactive"
apr_snprintf(sdp_string,
MAX_SDP_STRING_LEN,
"v="V_PROTOCOL_VERSION"\n"
"%s"
"s="S_SESSION_NAME"\n"
"%s"
"t="T_TIME_DESCRIPTION"\n"
"%s"
"a="A_PCMU"\n"
"a="A_PCMA"\n"
"a="A_TELEPHONE"\n"
"a="INACTIVE"\n",
session_identifier,
connection_info,
media_transport);
}
}
答案 0 :(得分:2)
我认为代码底部的if/else if
语句非常难看,因为您已将大量代码复制到两个不同的位置。您可以在一个地方编辑它,但忘记在另一个地方编辑它。你应该尽量避免这种情况。一种方法是将if/else if
语句替换为:
apr_snprintf(sdp_string,
MAX_SDP_STRING_LEN,
"v="V_PROTOCOL_VERSION"\n"
"%s"
"s="S_SESSION_NAME"\n"
"%s"
"t="T_TIME_DESCRIPTION"\n"
"%s"
"a="A_PCMU"\n"
"a="A_PCMA"\n"
"a="A_TELEPHONE"\n"
"%s",
session_identifier,
connection_info,
media_transport,
reinvite ? "a=inactive\n" : "");
EDIT1:使这段代码变得更好的另一种方法是编写自己的printf版本,将字节添加到sdp_string,然后为要添加的每条不同信息调用该函数一次字符串。
答案 1 :(得分:2)
如果你想要更通用的东西,请看:
void append_to_sdp_string(char *sdp_string, char *param)
{
int size = strlen(sdp_string);
apr_snprintf(sdp_string + size, MAX_SDP_STRING_LEN - size, "%s\n", param);
}
void create_sdp_string(char *sdp_string, char reinvite)
{
/* Defines and variables here */
apr_snprintf(session_id, MAX_STRING_LEN, "%lu", (unsigned long) apr_time_usec(apr_time_now());
apr_snprintf(session_version, MAX_STRING_LEN, "%lu", (unsigned long) apr_time_usec(apr_time_now());
apr_snprintf(session_identifier, MAX_STRING_LEN, "o="USERNAME" "NETTYPE" %s %s "ADDR_TYPE" %s", session_id, session_version, local_ip_addr);
apr_snprintf(media_transport, MAX_STRING_LEN, "m=audio %s "M_MEDIA_NAME_TRANSPORT_ADDR"", audio_port);
apr_snprintf(connection_info, MAX_STRING_LEN, "c="NETTYPE" "ADDR_TYPE" %s", local_ip_addr);
append_to_sdp_string(sdp_string, "v="V_PROTOCOL_VERSION);
append_to_sdp_string(sdp_string, session_identifier);
append_to_sdp_string(sdp_string, "s="S_SESSION_NAME);
append_to_sdp_string(sdp_string, connection_info);
append_to_sdp_string(sdp_string, "t="T_TIME_DESCRIPTION);
append_to_sdp_string(sdp_string, media_transport);
append_to_sdp_string(sdp_string, "a="A_PCMU);
append_to_sdp_string(sdp_string, "a="A_PCMA);
append_to_sdp_string(sdp_string, "a="A_TELEPHONE);
if(reinvite == TRUE)
append_to_sdp_string(sdp_string, "a="INACTIVE);
/* Here you can add as many simple conditional appends as you need */
}
这样可以很容易地将任何内容附加到已经创建的字符串中。
答案 2 :(得分:1)
如果代码中存在性能瓶颈,则这不是if/else
结构,而是您多次扫描所有字符串的事实。
基本上你使用snprintf
的变体来主要连接字符串,这是非常低效的。
切换到一个接一个地写入字符串的策略,就像使用fputs
一样,如果这是一个文件:
snprintf
返回。#define
中,将其置于真实数组中,例如static char const V_PROTOCOL_VERSION = { "0" };
。这样一个字符串的大小是一个编译时常量,您可以通过(sizeof V_PROTOCOL_VERSION)-1
来获取。这可确保您每个复制的字符只触摸一次所有常量字符串。