From 1017f2ff20af7d93e21284058939c2782d6d356a Mon Sep 17 00:00:00 2001 From: =?utf8?q?Ji=C5=99=C3=AD=20Mat=C4=9Bj=C3=A1k?= Date: Mon, 21 May 2018 21:05:44 +0200 Subject: [PATCH] each line is sent only once, default is empty --- mt_server.c | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/mt_server.c b/mt_server.c index f7315e1..cb5ba02 100644 --- a/mt_server.c +++ b/mt_server.c @@ -10,6 +10,7 @@ char *new_line() char *line = (char *)malloc((LWS_PRE + INPUT_LINE_LENGTH)*sizeof(char)); if (!line) { perror("malloc"); + return NULL; } return line + LWS_PRE; } @@ -19,7 +20,7 @@ char *copy_line(char *line) { if (!copy) { return NULL; } - strcpy(copy, line); + strncpy(copy, line, INPUT_LINE_LENGTH); } void free_line(char *line) @@ -178,13 +179,24 @@ static int callback_merica_terminal(struct lws *wsi, break; case LWS_CALLBACK_SERVER_WRITEABLE: - line = lines->first->line; - n = strlen(line); - m = lws_write(wsi, (unsigned char *)line, n, LWS_WRITE_TEXT); - if (lines->first->next != NULL) { + if (lines->first) { + line = lines->first->line; + n = strlen(line); + m = lws_write(wsi, (unsigned char *)line, n, LWS_WRITE_TEXT); list_remove(lines); - lws_callback_on_writable_all_protocol(lws_get_context(wsi), - &protocols[PROTOCOL_MERICA_TERMINAL]); + if (lines->first) { + lws_callback_on_writable_all_protocol(lws_get_context(wsi), + &protocols[PROTOCOL_MERICA_TERMINAL]); + } + } else { + line = copy_line(JSON_EMPTY); + if (line) { + n = strlen(line); + m = lws_write(wsi, (unsigned char *)line, n, LWS_WRITE_TEXT); + free_line(line); + } else { + return -1; + } } if (m < n) { fprintf(stderr, "ERROR %d writing to di socket\n", n); @@ -264,21 +276,12 @@ int mt_server_init(mt_server_t *self, struct ev_loop *loop, int fd) ev_io_ws *w = &(self->fd_watcher); w->context = context; - char *line = copy_line(JSON_EMPTY); - if (!line) { - return -1; - } w->lines = list_init(); if (!w->lines) { - free_line(line); - return -1; - } - if (list_add(w->lines, line) != 0) { - list_deinit(w->lines); return -1; } protocols[PROTOCOL_MERICA_TERMINAL].user = (void *)w->lines; - line = new_line(); + char *line = new_line(); if (!line) { list_deinit(w->lines); return -1; -- 2.39.2