2 * included from libwebsockets.c for unix builds
5 static unsigned long long time_in_microseconds(void)
8 gettimeofday(&tv, NULL);
9 return (tv.tv_sec * 1000000) + tv.tv_usec;
12 LWS_VISIBLE int libwebsockets_get_random(struct libwebsocket_context *context,
15 return read(context->fd_random, (char *)buf, len);
18 LWS_VISIBLE int lws_send_pipe_choked(struct libwebsocket *wsi)
20 struct libwebsocket_pollfd fds;
22 /* treat the fact we got a truncated send pending as if we're choked */
23 if (wsi->truncated_send_len)
30 if (poll(&fds, 1, 0) != 1)
33 if ((fds.revents & POLLOUT) == 0)
36 /* okay to send another packet without blocking */
41 static int lws_poll_listen_fd(struct libwebsocket_pollfd *fd)
43 return poll(fd, 1, 0);
49 libwebsocket_accept_cb(struct ev_loop *loop, struct ev_io *watcher, int revents)
51 struct libwebsocket_pollfd eventfd;
52 struct lws_io_watcher *lws_io = (struct lws_io_watcher*)watcher;
53 struct libwebsocket_context *context = lws_io->context;
55 if (revents & EV_ERROR)
58 eventfd.fd = watcher->fd;
59 eventfd.revents = EV_NONE;
60 if (revents & EV_READ)
61 eventfd.revents |= LWS_POLLIN;
63 if (revents & EV_WRITE)
64 eventfd.revents |= LWS_POLLOUT;
66 libwebsocket_service_fd(context,&eventfd);
70 libwebsocket_sigint_cb(
71 struct ev_loop *loop, struct ev_signal* watcher, int revents)
73 ev_break(loop, EVBREAK_ALL);
77 libwebsocket_initloop(
78 struct libwebsocket_context *context,
83 const char * backend_name;
84 struct ev_io *w_accept = (ev_io *)&context->w_accept;
85 struct ev_signal *w_sigint = (ev_signal *)&context->w_sigint;
88 loop = ev_default_loop(0);
90 context->io_loop = loop;
93 * Initialize the accept w_accept with the listening socket
94 * and register a callback for read operations:
96 ev_io_init(w_accept, libwebsocket_accept_cb,
97 context->listen_service_fd, EV_READ);
98 ev_io_start(context->io_loop,w_accept);
99 ev_signal_init(w_sigint, libwebsocket_sigint_cb, SIGINT);
100 ev_signal_start(context->io_loop,w_sigint);
101 backend = ev_backend(loop);
104 case EVBACKEND_SELECT:
105 backend_name = "select";
108 backend_name = "poll";
110 case EVBACKEND_EPOLL:
111 backend_name = "epoll";
113 case EVBACKEND_KQUEUE:
114 backend_name = "kqueue";
116 case EVBACKEND_DEVPOLL:
117 backend_name = "/dev/poll";
120 backend_name = "Solaris 10 \"port\"";
123 backend_name = "Unknown libev backend";
127 lwsl_notice(" libev backend: %s\n", backend_name);
132 #endif /* LWS_USE_LIBEV */
135 * libwebsocket_cancel_service() - Cancel servicing of pending websocket activity
136 * @context: Websocket context
138 * This function let a call to libwebsocket_service() waiting for a timeout
139 * immediately return.
142 libwebsocket_cancel_service(struct libwebsocket_context *context)
146 if (write(context->dummy_pipe_fds[1], &buf, sizeof(buf)) != 1)
147 lwsl_err("Cannot write to dummy pipe");
150 LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
152 int syslog_level = LOG_DEBUG;
156 syslog_level = LOG_ERR;
159 syslog_level = LOG_WARNING;
162 syslog_level = LOG_NOTICE;
165 syslog_level = LOG_INFO;
168 syslog(syslog_level, "%s", line);