Index: trunk/src_plugins/hid_srv_ws/c2s.c =================================================================== --- trunk/src_plugins/hid_srv_ws/c2s.c (revision 10621) +++ trunk/src_plugins/hid_srv_ws/c2s.c (revision 10622) @@ -32,18 +32,37 @@ #include "server.h" #include "c2s.h" +typedef struct { + pcb_c2s_msg_t msg; +} dgram_t; + void hid_ws_recv_msg(hid_srv_ws_t *ctx) { - char msg[256]; + dgram_t dg; int len; - *msg = 0; - len = recv(ctx->c2s[0], msg, sizeof(msg), MSG_DONTWAIT); - msg[len] = '\0'; - pcb_message(PCB_MSG_INFO, "SERVER MSG: %d/'%s'\n", len, msg); + len = recv(ctx->c2s[0], &dg, sizeof(dg), MSG_DONTWAIT); + if (len != sizeof(dg)) { + pcb_message(PCB_MSG_ERROR, "websocket [%d]: server msg invliad size %d (expected %d)\n", ctx->pid, len, sizeof(dg)); + return; + } + + switch(dg.msg) { + case PCB_C2S_MSG_START: + if (ctx->pollfds[ctx->listen_idx].fd < 0) + ctx->pollfds[ctx->listen_idx].fd = -ctx->pollfds[ctx->listen_idx].fd; /* enable listen because the client finished accepting the connection */ + pcb_message(PCB_MSG_INFO, "websocket [%d]: new client conn acked, reenabling listen\n", ctx->pid); + break; + default: + pcb_message(PCB_MSG_ERROR, "websocket [%d]: server msg invliad msg %d\n", ctx->pid, dg.msg); + } } -void hid_ws_send_msg(hid_srv_ws_t *ctx) +void hid_ws_send_msg(hid_srv_ws_t *ctx, pcb_c2s_msg_t msg) { - send(ctx->c2s[1], "jajj", 4, 0); + dgram_t dg; + + dg.msg = msg; + + send(ctx->c2s[1], &dg, sizeof(dg), 0); } Index: trunk/src_plugins/hid_srv_ws/c2s.h =================================================================== --- trunk/src_plugins/hid_srv_ws/c2s.h (revision 10621) +++ trunk/src_plugins/hid_srv_ws/c2s.h (revision 10622) @@ -21,5 +21,10 @@ */ #include "server.h" + +typedef enum { + PCB_C2S_MSG_START +} pcb_c2s_msg_t; + void hid_ws_recv_msg(hid_srv_ws_t *ctx); -void hid_ws_send_msg(hid_srv_ws_t *ctx); +void hid_ws_send_msg(hid_srv_ws_t *ctx, pcb_c2s_msg_t msg); Index: trunk/src_plugins/hid_srv_ws/server.c =================================================================== --- trunk/src_plugins/hid_srv_ws/server.c (revision 10621) +++ trunk/src_plugins/hid_srv_ws/server.c (revision 10622) @@ -62,8 +62,10 @@ } /* assume the first fd added via this callback is the listener */ - if (ctx->listen_fd < 0) + if (ctx->listen_fd < 0) { ctx->listen_fd = pa->fd; + ctx->listen_idx = ctx->count_pollfds; + } ctx->fd_lookup[pa->fd] = ctx->count_pollfds; ctx->pollfds[ctx->count_pollfds].fd = pa->fd; @@ -205,16 +207,18 @@ } else pcb_message(PCB_MSG_INFO, "websocket [%d]: new client conn accepted\n", ctx->pid); - hid_ws_send_msg(ctx); + hid_ws_send_msg(ctx, PCB_C2S_MSG_START); } else { - /* parent: announce the fork but don't do anything else */ + /* parent */ + if (ctx->pollfds[n].fd > 0) + ctx->pollfds[n].fd = -ctx->pollfds[n].fd; /* disable listen until the client finished accepting */ pcb_message(PCB_MSG_INFO, "websocket [%d]: new client conn forked\n", ctx->pid); -#warning TODO: rather communicate back from the client - sleep(1); ctx->num_clients++; - if (ctx->num_clients >= max_clients) /* throttle excess forking */ + if (ctx->num_clients >= max_clients) { /* throttle excess forking */ sleep(5); + ctx->num_clients++; + } } } else { Index: trunk/src_plugins/hid_srv_ws/server.h =================================================================== --- trunk/src_plugins/hid_srv_ws/server.h (revision 10621) +++ trunk/src_plugins/hid_srv_ws/server.h (revision 10622) @@ -35,7 +35,7 @@ struct lws_pollfd pollfds[WS_MAX_FDS]; int fd_lookup[1024]; int count_pollfds; - int listen_fd; + int listen_fd, listen_idx; pid_t pid; int num_clients;