void handlerThread(int csockIn) { int sopt = 1, csock = csockIn; ws_ctx_t *ws_ctx; bool useHixie = TRUE; ws_ctx = do_handshake(csock, &useHixie); if (ws_ctx == NULL) { handler_msg("No connection after handshake\n"); } else { settings.handler(ws_ctx, useHixie); if (pipe_error) { handler_emsg("Closing due to SIGPIPE\n"); } } if (ws_ctx) { ws_socket_free(ws_ctx); } else { shutdown(csock, SHUT_RDWR); close(csock); } handler_msg("handler exit\n"); }
int start_server() { int lsock, csock, pid, clilen, sopt = 1, i, res; struct sockaddr_in serv_addr, cli_addr; ws_ctx_t *ws_ctx; fd_set myset; struct timeval tv; /* Initialize buffers */ lsock = socket(AF_INET, SOCK_STREAM, 0); if (lsock < 0) { error("ERROR creating listener socket"); } bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(settings.listen_port); /* Resolve listen address */ if (settings.listen_host && (settings.listen_host[0] != '\0')) { if (resolve_host(&serv_addr.sin_addr, settings.listen_host) < -1) { error("Could not resolve listen address"); return 1; } } else { serv_addr.sin_addr.s_addr = INADDR_ANY; } setsockopt(lsock, SOL_SOCKET, SO_REUSEADDR, (char *)&sopt, sizeof(sopt)); if (bind(lsock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { error("ERROR on binding listener socket"); return 1; } int optval = 1; if (setsockopt(lsock, IPPROTO_TCP, TCP_NODELAY, &optval, sizeof(int)) < 0) { error("Cannot set TCP_NODELAY option on listen address"); return 1; } listen(lsock,100); signal(SIGPIPE, signal_handler); // catch pipe if (settings.daemon) { ; // daemonize(lsock); } // Reep zombies signal(SIGCHLD, SIG_IGN); printf("Waiting for connections on %s:%d\n", settings.listen_host, settings.listen_port); while (websockify_loop) { clilen = sizeof(cli_addr); pipe_error = 0; pid = 0; // if (set_non_blocking(lsock) != 0) { // return; // } // Set Accept socket to non blocking to allow to cancel // requests while (websockify_loop) { tv.tv_sec = 0; tv.tv_usec = 200000; FD_ZERO(&myset); FD_SET(lsock, &myset); res = select(lsock+1, &myset, NULL, NULL, &tv); /* EINTR */ if (res < 0 && errno == EINTR) { continue; } if (res < 0) { perror("select"); return 1; } if (res > 0) { break; } } if (!websockify_loop) { fprintf(stderr, "Ending loop before accept\n"); return 0; } csock = accept(lsock, (struct sockaddr *) &cli_addr, &clilen); fprintf(stderr, "After accept\n"); if (csock < 0) { error("ERROR on accept"); continue; } handler_msg("got client connection from %s\n", inet_ntoa(cli_addr.sin_addr)); // if (set_blocking(lsock) != 0) { // return; // } if (!settings.run_once) { handler_msg("forking handler process not supported. Abort\n"); abort(); // pid = fork(); } if (pid == 0) { // handler process ws_ctx = do_handshake(csock); if (settings.run_once) { // Successful connection, stop listening for new // connections close(lsock); } if (ws_ctx == NULL) { handler_msg("No connection after handshake\n"); break; // Child process exits } settings.handler(ws_ctx); if (pipe_error) { handler_emsg("Closing due to SIGPIPE\n"); } break; // Child process exits } else { // parent process settings.handler_id += 1; } } if (pid == 0) { if (ws_ctx) { ws_socket_free(ws_ctx); free_ws_ctx(ws_ctx); } else { shutdown(csock, SHUT_RDWR); close(csock); } handler_msg("handler exit\n"); } else { handler_msg("websockify exit\n"); } return 0; }
void timer_condition::load(const settings_t& s) { settings_t::const_iterator it = s.find("interval"); if (it != s.end()) set_interval(it.value().toInt()); }
void start_server() { int lsock, csock, pid, clilen, sopt = 1, i; struct sockaddr_in serv_addr, cli_addr; ws_ctx_t *ws_ctx; /* Initialize buffers */ lsock = socket(AF_INET, SOCK_STREAM, 0); if (lsock < 0) { error("ERROR creating listener socket"); } bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(settings.listen_port); /* Resolve listen address */ if (settings.listen_host && (settings.listen_host[0] != '\0')) { if (resolve_host(&serv_addr.sin_addr, settings.listen_host) < -1) { fatal("Could not resolve listen address"); } } else { serv_addr.sin_addr.s_addr = INADDR_ANY; } setsockopt(lsock, SOL_SOCKET, SO_REUSEADDR, (char *)&sopt, sizeof(sopt)); if (bind(lsock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { fatal("ERROR on binding listener socket"); } listen(lsock,100); signal(SIGPIPE, signal_handler); // catch pipe if (settings.daemon) { daemonize(lsock); } // Reep zombies signal(SIGCHLD, SIG_IGN); printf("Waiting for connections on %s:%d\n", settings.listen_host, settings.listen_port); while (1) { clilen = sizeof(cli_addr); pipe_error = 0; pid = 0; csock = accept(lsock, (struct sockaddr *) &cli_addr, &clilen); if (csock < 0) { error("ERROR on accept"); continue; } handler_msg("got client connection from %s\n", inet_ntoa(cli_addr.sin_addr)); if (!settings.run_once) { handler_msg("forking handler process\n"); pid = fork(); } if (pid == 0) { // handler process ws_ctx = do_handshake(csock); if (settings.run_once) { if (ws_ctx == NULL) { // Not a real WebSocket connection continue; } else { // Successful connection, stop listening for new // connections close(lsock); } } if (ws_ctx == NULL) { handler_msg("No connection after handshake\n"); break; // Child process exits } settings.handler(ws_ctx); if (pipe_error) { handler_emsg("Closing due to SIGPIPE\n"); } break; // Child process exits } else { // parent process settings.handler_id += 1; } } if (pid == 0) { if (ws_ctx) { ws_socket_free(ws_ctx); free_ws_ctx(ws_ctx); } else { shutdown(csock, SHUT_RDWR); close(csock); } handler_msg("handler exit\n"); } else { handler_msg("websockify exit\n"); } }