Commit b21eaab5 authored by Wandenberg Peixoto's avatar Wandenberg Peixoto

refactoring ngx_http_push_stream_msg_t type members

parent 09827759
...@@ -78,12 +78,11 @@ static ngx_str_t ngx_http_push_stream_shm_name = ngx_string("push_stream_modu ...@@ -78,12 +78,11 @@ static ngx_str_t ngx_http_push_stream_shm_name = ngx_string("push_stream_modu
// message queue // message queue
typedef struct { typedef struct {
ngx_queue_t queue; // this MUST be first ngx_queue_t queue; // this MUST be first
ngx_buf_t *buf;
time_t expires; time_t expires;
time_t time; time_t time;
ngx_flag_t deleted; ngx_flag_t deleted;
ngx_int_t id; ngx_int_t id;
ngx_str_t raw; ngx_str_t *raw;
ngx_str_t *formatted_messages; ngx_str_t *formatted_messages;
} ngx_http_push_stream_msg_t; } ngx_http_push_stream_msg_t;
......
...@@ -206,7 +206,7 @@ static ngx_int_t ngx_http_push_stream_send_only_header_response(ngx_h ...@@ -206,7 +206,7 @@ static ngx_int_t ngx_http_push_stream_send_only_header_response(ngx_h
static u_char * ngx_http_push_stream_str_replace(u_char *org, u_char *find, u_char *replace, ngx_uint_t offset, ngx_pool_t *temp_pool); static u_char * ngx_http_push_stream_str_replace(u_char *org, u_char *find, u_char *replace, ngx_uint_t offset, ngx_pool_t *temp_pool);
static ngx_str_t * ngx_http_push_stream_get_formatted_chunk(const u_char *text, off_t len, ngx_pool_t *temp_pool); static ngx_str_t * ngx_http_push_stream_get_formatted_chunk(const u_char *text, off_t len, ngx_pool_t *temp_pool);
static ngx_str_t * ngx_http_push_stream_get_formatted_message(ngx_http_request_t *r, ngx_http_push_stream_channel_t *channel, ngx_http_push_stream_msg_t *msg, ngx_pool_t *temp_pool); static ngx_str_t * ngx_http_push_stream_get_formatted_message(ngx_http_request_t *r, ngx_http_push_stream_channel_t *channel, ngx_http_push_stream_msg_t *msg, ngx_pool_t *temp_pool);
static ngx_str_t * ngx_http_push_stream_format_message(ngx_http_push_stream_channel_t *channel, ngx_http_push_stream_msg_t *message, ngx_str_t *message_template, ngx_pool_t *temp_pool); static ngx_str_t * ngx_http_push_stream_format_message(ngx_http_push_stream_channel_t *channel, ngx_http_push_stream_msg_t *message, ngx_str_t *text, ngx_str_t *message_template, ngx_pool_t *temp_pool);
static ngx_int_t ngx_http_push_stream_send_response_content_header(ngx_http_request_t *r, ngx_http_push_stream_loc_conf_t *pslcf); static ngx_int_t ngx_http_push_stream_send_response_content_header(ngx_http_request_t *r, ngx_http_push_stream_loc_conf_t *pslcf);
static ngx_int_t ngx_http_push_stream_send_response_text(ngx_http_request_t *r, const u_char *text, uint len, ngx_flag_t last_buffer); static ngx_int_t ngx_http_push_stream_send_response_text(ngx_http_request_t *r, const u_char *text, uint len, ngx_flag_t last_buffer);
static ngx_int_t ngx_http_push_stream_send_ping(ngx_log_t *log, ngx_http_push_stream_loc_conf_t *pslcf); static ngx_int_t ngx_http_push_stream_send_ping(ngx_log_t *log, ngx_http_push_stream_loc_conf_t *pslcf);
......
...@@ -148,38 +148,29 @@ ngx_http_push_stream_convert_char_to_msg_on_shared_locked(u_char *data, size_t l ...@@ -148,38 +148,29 @@ ngx_http_push_stream_convert_char_to_msg_on_shared_locked(u_char *data, size_t l
msg->formatted_messages = NULL; msg->formatted_messages = NULL;
if ((msg->buf = ngx_slab_alloc_locked(shpool, sizeof(ngx_buf_t))) == NULL) { if ((msg->raw = ngx_slab_alloc_locked(shpool, sizeof(ngx_str_t) + len + 1)) == NULL) {
ngx_http_push_stream_free_message_memory_locked(shpool, msg); ngx_http_push_stream_free_message_memory_locked(shpool, msg);
return NULL; return NULL;
} }
if ((msg->buf->start = ngx_slab_alloc_locked(shpool, len + 1)) == NULL) { msg->raw->len = len;
ngx_http_push_stream_free_message_memory_locked(shpool, msg); msg->raw->data = (u_char *) (msg->raw + 1);
return NULL; ngx_memset(msg->raw->data, '\0', len + 1);
}
ngx_memset(msg->buf->start, '\0', len + 1);
// copy the message to shared memory // copy the message to shared memory
msg->buf->last = ngx_copy(msg->buf->start, data, len); ngx_memcpy(msg->raw->data, data, len);
msg->buf->pos = msg->buf->start;
msg->buf->end = msg->buf->last;
msg->buf->temporary = 1;
msg->buf->memory = 1;
msg->deleted = 0; msg->deleted = 0;
msg->expires = 0; msg->expires = 0;
msg->queue.prev = NULL; msg->queue.prev = NULL;
msg->queue.next = NULL; msg->queue.next = NULL;
msg->id = id; msg->id = id;
msg->raw.data = msg->buf->start;
msg->raw.len = len;
if ((msg->formatted_messages = ngx_slab_alloc_locked(shpool, sizeof(ngx_str_t)*ngx_http_push_stream_module_main_conf->qtd_templates)) == NULL) { if ((msg->formatted_messages = ngx_slab_alloc_locked(shpool, sizeof(ngx_str_t)*ngx_http_push_stream_module_main_conf->qtd_templates)) == NULL) {
ngx_http_push_stream_free_message_memory_locked(shpool, msg); ngx_http_push_stream_free_message_memory_locked(shpool, msg);
return NULL; return NULL;
} }
while ((cur = (ngx_http_push_stream_template_queue_t *) ngx_queue_next(&cur->queue)) != sentinel) { while ((cur = (ngx_http_push_stream_template_queue_t *) ngx_queue_next(&cur->queue)) != sentinel) {
ngx_str_t *aux = ngx_http_push_stream_format_message(channel, msg, cur->template, temp_pool); ngx_str_t *aux = ngx_http_push_stream_format_message(channel, msg, msg->raw, cur->template, temp_pool);
if (aux == NULL) { if (aux == NULL) {
ngx_http_push_stream_free_message_memory_locked(shpool, msg); ngx_http_push_stream_free_message_memory_locked(shpool, msg);
return NULL; return NULL;
...@@ -520,8 +511,7 @@ ngx_http_push_stream_free_message_memory_locked(ngx_slab_pool_t *shpool, ngx_htt ...@@ -520,8 +511,7 @@ ngx_http_push_stream_free_message_memory_locked(ngx_slab_pool_t *shpool, ngx_htt
ngx_slab_free_locked(shpool, msg->formatted_messages); ngx_slab_free_locked(shpool, msg->formatted_messages);
} }
if ((msg->buf != NULL) && (msg->buf->start != NULL)) ngx_slab_free_locked(shpool, msg->buf->start); if (msg->raw != NULL) ngx_slab_free_locked(shpool, msg->raw);
if (msg->buf != NULL) ngx_slab_free_locked(shpool, msg->buf);
if (msg != NULL) ngx_slab_free_locked(shpool, msg); if (msg != NULL) ngx_slab_free_locked(shpool, msg);
} }
...@@ -711,54 +701,32 @@ ngx_http_push_stream_get_formatted_message(ngx_http_request_t *r, ngx_http_push_ ...@@ -711,54 +701,32 @@ ngx_http_push_stream_get_formatted_message(ngx_http_request_t *r, ngx_http_push_
if (pslcf->message_template_index > 0) { if (pslcf->message_template_index > 0) {
return message->formatted_messages + pslcf->message_template_index - 1; return message->formatted_messages + pslcf->message_template_index - 1;
} }
return &message->raw; return message->raw;
} }
static ngx_str_t * static ngx_str_t *
ngx_http_push_stream_format_message(ngx_http_push_stream_channel_t *channel, ngx_http_push_stream_msg_t *message, ngx_str_t *message_template, ngx_pool_t *pool) ngx_http_push_stream_format_message(ngx_http_push_stream_channel_t *channel, ngx_http_push_stream_msg_t *message, ngx_str_t *text, ngx_str_t *message_template, ngx_pool_t *temp_pool)
{ {
ngx_uint_t len = 0;
u_char *txt = NULL; u_char *txt = NULL;
ngx_str_t *str = NULL; ngx_str_t *str = NULL;
u_char template[message_template->len + 1];
ngx_memset(template, '\0', message_template->len + 1);
ngx_memcpy(template, message_template->data, message_template->len);
u_char char_id[NGX_INT_T_LEN]; u_char char_id[NGX_INT_T_LEN];
ngx_memset(char_id, '\0', NGX_INT_T_LEN); ngx_memset(char_id, '\0', NGX_INT_T_LEN);
u_char *msg = NGX_HTTP_PUSH_STREAM_EMPTY.data; u_char *channel_id = (channel != NULL) ? channel->id.data : NGX_HTTP_PUSH_STREAM_EMPTY.data;
u_char *channel_id = NGX_HTTP_PUSH_STREAM_EMPTY.data;
ngx_int_t message_id = 0;
if (channel != NULL) {
channel_id = channel->id.data;
}
if (message != NULL) {
message_id = message->id;
len = ngx_buf_size(message->buf);
if ((msg = ngx_pcalloc(pool, len + 1)) == NULL) {
ngx_log_error(NGX_LOG_ERR, pool->log, 0, "push stream module: unable to allocate memory to copy message text");
return NULL;
}
ngx_memset(msg, '\0', len + 1);
ngx_memcpy(msg, message->buf->pos, len);
}
ngx_sprintf(char_id, "%d", message_id); ngx_sprintf(char_id, "%d", message->id);
txt = ngx_http_push_stream_str_replace(template, NGX_HTTP_PUSH_STREAM_TOKEN_MESSAGE_ID.data, char_id, 0, pool); txt = ngx_http_push_stream_str_replace(message_template->data, NGX_HTTP_PUSH_STREAM_TOKEN_MESSAGE_ID.data, char_id, 0, temp_pool);
txt = ngx_http_push_stream_str_replace(txt, NGX_HTTP_PUSH_STREAM_TOKEN_MESSAGE_CHANNEL.data, channel_id, 0, pool); txt = ngx_http_push_stream_str_replace(txt, NGX_HTTP_PUSH_STREAM_TOKEN_MESSAGE_CHANNEL.data, channel_id, 0, temp_pool);
txt = ngx_http_push_stream_str_replace(txt, NGX_HTTP_PUSH_STREAM_TOKEN_MESSAGE_TEXT.data, msg, 0, pool); txt = ngx_http_push_stream_str_replace(txt, NGX_HTTP_PUSH_STREAM_TOKEN_MESSAGE_TEXT.data, text->data, 0, temp_pool);
if (txt == NULL) { if (txt == NULL) {
ngx_log_error(NGX_LOG_ERR, pool->log, 0, "push stream module: unable to allocate memory to replace message values on template"); ngx_log_error(NGX_LOG_ERR, temp_pool->log, 0, "push stream module: unable to allocate memory to replace message values on template");
return NULL; return NULL;
} }
if ((str = ngx_pcalloc(pool, sizeof(ngx_str_t))) == NULL) { if ((str = ngx_pcalloc(temp_pool, sizeof(ngx_str_t))) == NULL) {
ngx_log_error(NGX_LOG_ERR, pool->log, 0, "push stream module: unable to allocate memory to return message applied to template"); ngx_log_error(NGX_LOG_ERR, temp_pool->log, 0, "push stream module: unable to allocate memory to return message applied to template");
return NULL; return NULL;
} }
str->data = txt; str->data = txt;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment