manager: Send fewer packets

The functions that build manager message headers do so in a way that
results in a single messages being split across multiple packets. While
this doesn't matter to the remote end, it makes network captures noisier
and harder to follow, and also means additional system calls.

With this patch, we build up more of the message content into the TLS
buffer before flushing to the network. This change is completely
internal to the manager code and does not affect any of the existing
API's consumers.

Change-Id: I50128b0769060ca5272dbbb5e60242d131eaddf9
This commit is contained in:
Sean Bright 2019-07-24 16:12:49 -04:00
parent 50453846b6
commit 0ebfc4a19d
1 changed files with 64 additions and 21 deletions

View File

@ -3054,6 +3054,15 @@ AST_THREADSTORAGE(userevent_buf);
/*! \brief initial allocated size for the astman_append_buf and astman_send_*_va */
#define ASTMAN_APPEND_BUF_INITSIZE 256
static void astman_flush(struct mansession *s, struct ast_str *buf)
{
if (s->hook || (s->tcptls_session && s->tcptls_session->stream)) {
send_string(s, ast_str_buffer(buf));
} else {
ast_verbose("No connection stream in astman_append, should not happen\n");
}
}
/*!
* utility functions for creating AMI replies
*/
@ -3103,21 +3112,32 @@ void astman_append(struct mansession *s, const char *fmt, ...)
static void astman_send_response_full(struct mansession *s, const struct message *m, char *resp, char *msg, char *listflag)
{
const char *id = astman_get_header(m, "ActionID");
struct ast_str *buf;
astman_append(s, "Response: %s\r\n", resp);
if (!ast_strlen_zero(id)) {
astman_append(s, "ActionID: %s\r\n", id);
}
if (listflag) {
astman_append(s, "EventList: %s\r\n", listflag); /* Start, complete, cancelled */
}
if (msg == MSG_MOREDATA) {
buf = ast_str_thread_get(&astman_append_buf, ASTMAN_APPEND_BUF_INITSIZE);
if (!buf) {
return;
} else if (msg) {
astman_append(s, "Message: %s\r\n\r\n", msg);
} else {
astman_append(s, "\r\n");
}
ast_str_set(&buf, 0, "Response: %s\r\n", resp);
if (!ast_strlen_zero(id)) {
ast_str_append(&buf, 0, "ActionID: %s\r\n", id);
}
if (listflag) {
/* Start, complete, cancelled */
ast_str_append(&buf, 0, "EventList: %s\r\n", listflag);
}
if (msg != MSG_MOREDATA) {
if (msg) {
ast_str_append(&buf, 0, "Message: %s\r\n", msg);
}
ast_str_append(&buf, 0, "\r\n");
}
astman_flush(s, buf);
}
void astman_send_response(struct mansession *s, const struct message *m, char *resp, char *msg)
@ -3172,18 +3192,43 @@ void astman_send_listack(struct mansession *s, const struct message *m, char *ms
astman_send_response_full(s, m, "Success", msg, listflag);
}
void astman_send_list_complete_start(struct mansession *s, const struct message *m, const char *event_name, int count)
static struct ast_str *astman_send_list_complete_start_common(struct mansession *s, const struct message *m, const char *event_name, int count)
{
const char *id = astman_get_header(m, "ActionID");
struct ast_str *buf;
astman_append(s, "Event: %s\r\n", event_name);
if (!ast_strlen_zero(id)) {
astman_append(s, "ActionID: %s\r\n", id);
buf = ast_str_thread_get(&astman_append_buf, ASTMAN_APPEND_BUF_INITSIZE);
if (!buf) {
return NULL;
}
astman_append(s,
ast_str_set(&buf, 0, "Event: %s\r\n", event_name);
if (!ast_strlen_zero(id)) {
ast_str_append(&buf, 0, "ActionID: %s\r\n", id);
}
ast_str_append(&buf, 0,
"EventList: Complete\r\n"
"ListItems: %d\r\n",
count);
return buf;
}
static void astman_send_list_complete(struct mansession *s, const struct message *m, const char *event_name, int count)
{
struct ast_str *buf = astman_send_list_complete_start_common(s, m, event_name, count);
if (buf) {
ast_str_append(&buf, 0, "\r\n");
astman_flush(s, buf);
}
}
void astman_send_list_complete_start(struct mansession *s, const struct message *m, const char *event_name, int count)
{
struct ast_str *buf = astman_send_list_complete_start_common(s, m, event_name, count);
if (buf) {
astman_flush(s, buf);
}
}
void astman_send_list_complete_end(struct mansession *s)
@ -4511,8 +4556,7 @@ static int action_hangup(struct mansession *s, const struct message *m)
regfree(&regexbuf);
ast_free(regex_string);
astman_send_list_complete_start(s, m, "ChannelsHungupListComplete", channels_matched);
astman_send_list_complete_end(s);
astman_send_list_complete(s, m, "ChannelsHungupListComplete", channels_matched);
return 0;
}
@ -6325,8 +6369,7 @@ static int action_coreshowchannels(struct mansession *s, const struct message *m
}
ao2_iterator_destroy(&it_chans);
astman_send_list_complete_start(s, m, "CoreShowChannelsComplete", numchans);
astman_send_list_complete_end(s);
astman_send_list_complete(s, m, "CoreShowChannelsComplete", numchans);
ao2_ref(channels, -1);
return 0;