From: Jiří Matěják Date: Fri, 18 May 2018 02:32:58 +0000 (+0200) Subject: queue improved X-Git-Url: https://rtime.felk.cvut.cz/gitweb/coffee/mt-apps.git/commitdiff_plain/5d21c6510930cfee2d7f7b38bf8c0d7fe1437a56 queue improved --- diff --git a/mt_server.c b/mt_server.c index 7575fed..dc70aab 100644 --- a/mt_server.c +++ b/mt_server.c @@ -27,9 +27,9 @@ void free_line(char *line) free(line - LWS_PRE); } -list *list_init(char *line) +node *node_init(char *line) { - list *res = (list *)malloc(sizeof(list)); + node *res = (node *)malloc(sizeof(node)); if (!res) { perror("malloc"); return res; @@ -39,29 +39,47 @@ list *list_init(char *line) return res; } -int list_add(list **in, char *line) +list *list_init() { - list *new = list_init(line); + list *res = (list *)calloc(1, sizeof(list)); + if (!res) { + perror("malloc"); + } + return res; +} + +int list_add(list *in, char *line) +{ + node *new = node_init(line); if (!new) { return -1; } - (*in)->next = new; - *in = new; + if (!in->first) { + in->first = new; + } else { + in->last->next = new; + } + in->last = new; return 0; } -void list_remove(list **in) +void list_remove(list *in) { - list *tmp = *in; - *in = (*in)->next; - free_line(tmp->line); - free(tmp); + node *tmp = in->first; + if (tmp) { + in->first = tmp->next; + if (!tmp->next) { + in->last = NULL; + } + free_line(tmp->line); + free(tmp); + } } void list_deinit(list *in) { - while (in) { - list_remove(&in); + while (in->first) { + list_remove(in); } } @@ -135,7 +153,7 @@ static int callback_merica_terminal(struct lws *wsi, int n, m; - list **lines = (list **)&(prot->user); + list *lines = (list *)prot->user; char *line; switch (reason) { @@ -159,13 +177,13 @@ static int callback_merica_terminal(struct lws *wsi, break; case LWS_CALLBACK_SERVER_WRITEABLE: - line = (*lines)->line; + line = lines->first->line; n = strlen(line); m = lws_write(wsi, (unsigned char *)line, n, LWS_WRITE_TEXT); - if ((*lines)->next != NULL) { + if (lines->first->next != NULL) { list_remove(lines); lws_callback_on_writable_all_protocol(lws_get_context(wsi), - &protocols[PROTOCOL_MERICA_TERMINAL]); + &protocols[PROTOCOL_MERICA_TERMINAL]); } if (m < n) { fprintf(stderr, "ERROR %d writing to di socket\n", n); @@ -175,9 +193,13 @@ static int callback_merica_terminal(struct lws *wsi, case LWS_CALLBACK_RECEIVE: if (strcmp((const char *)in, "reset") == 0) { - strcpy((*lines)->line, JSON_EMPTY); - lws_callback_on_writable_all_protocol(lws_get_context(wsi), - &protocols[PROTOCOL_MERICA_TERMINAL]); + line = copy_line(JSON_EMPTY); + if (line) { + if (list_add(lines, line) == 0) { + lws_callback_on_writable_all_protocol(lws_get_context(wsi), + &protocols[PROTOCOL_MERICA_TERMINAL]); + } + } } else if (strcmp((const char *)in, "close") == 0) { fprintf(stderr, "closing websocket\n"); lws_close_reason(wsi, LWS_CLOSE_STATUS_GOINGAWAY, @@ -203,7 +225,7 @@ static void fd_cb(EV_P_ ev_io *w_, int revents) if (*pos == '\n' || (w->pos - w->text) == INPUT_LINE_LENGTH) { *pos = 0; - if (list_add(&(w->lines), w->text) == 0) { + if (list_add(w->lines, w->text) == 0) { char *line = new_line(); if (line) { w->text = line; @@ -242,11 +264,15 @@ int mt_server_init(mt_server_t *self, struct ev_loop *loop, int fd) if (!line) { return -1; } - w->lines = list_init(line); + 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(); if (!line) { diff --git a/mt_server.h b/mt_server.h index f08dfbd..ead05f1 100644 --- a/mt_server.h +++ b/mt_server.h @@ -16,9 +16,14 @@ #define JSON_EMPTY "{\"type\":\"empty\"}" // default message -typedef struct list { +typedef struct node { char *line; - struct list *next; + struct node *next; +} node; + +typedef struct list { + struct node *first; + struct node *last; } list; typedef struct ev_io_ws {