clean libwebsockets.c
[platform/upstream/libwebsockets.git] / lib / lws-plat-unix.c
1 /*
2  * included from libwebsockets.c for unix builds
3  */
4
5 static unsigned long long time_in_microseconds(void)
6 {
7         struct timeval tv;
8         gettimeofday(&tv, NULL);
9         return (tv.tv_sec * 1000000) + tv.tv_usec;
10 }
11
12 LWS_VISIBLE int libwebsockets_get_random(struct libwebsocket_context *context,
13                                                              void *buf, int len)
14 {
15         return read(context->fd_random, (char *)buf, len);
16 }
17
18 LWS_VISIBLE int lws_send_pipe_choked(struct libwebsocket *wsi)
19 {
20         struct libwebsocket_pollfd fds;
21
22         /* treat the fact we got a truncated send pending as if we're choked */
23         if (wsi->truncated_send_len)
24                 return 1;
25
26         fds.fd = wsi->sock;
27         fds.events = POLLOUT;
28         fds.revents = 0;
29
30         if (poll(&fds, 1, 0) != 1)
31                 return 1;
32
33         if ((fds.revents & POLLOUT) == 0)
34                 return 1;
35
36         /* okay to send another packet without blocking */
37
38         return 0;
39 }
40
41 static int lws_poll_listen_fd(struct libwebsocket_pollfd *fd)
42 {
43         return poll(fd, 1, 0);
44 }
45
46
47 #ifdef LWS_USE_LIBEV
48 LWS_VISIBLE void 
49 libwebsocket_accept_cb(struct ev_loop *loop, struct ev_io *watcher, int revents)
50 {
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;
54
55         if (revents & EV_ERROR)
56                 return;
57
58         eventfd.fd = watcher->fd;
59         eventfd.revents = EV_NONE;
60         if (revents & EV_READ)
61                 eventfd.revents |= LWS_POLLIN;
62
63         if (revents & EV_WRITE)
64                 eventfd.revents |= LWS_POLLOUT;
65
66         libwebsocket_service_fd(context,&eventfd);
67 }
68
69 LWS_VISIBLE void
70 libwebsocket_sigint_cb(
71     struct ev_loop *loop, struct ev_signal* watcher, int revents)
72 {
73     ev_break(loop, EVBREAK_ALL);
74 }
75
76 LWS_VISIBLE int
77 libwebsocket_initloop(
78         struct libwebsocket_context *context,
79         struct ev_loop *loop)
80 {
81         int status = 0;
82         int backend;
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;
86
87         if (!loop)
88                 loop = ev_default_loop(0);
89
90         context->io_loop = loop;
91    
92         /*
93          * Initialize the accept w_accept with the listening socket
94          * and register a callback for read operations:
95          */
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);
102
103         switch (backend) {
104         case EVBACKEND_SELECT:
105                 backend_name = "select";
106                 break;
107         case EVBACKEND_POLL:
108                 backend_name = "poll";
109                 break;
110         case EVBACKEND_EPOLL:
111                 backend_name = "epoll";
112                 break;
113         case EVBACKEND_KQUEUE:
114                 backend_name = "kqueue";
115                 break;
116         case EVBACKEND_DEVPOLL:
117                 backend_name = "/dev/poll";
118                 break;
119         case EVBACKEND_PORT:
120                 backend_name = "Solaris 10 \"port\"";
121                 break;
122         default:
123                 backend_name = "Unknown libev backend";
124                 break;
125         };
126
127         lwsl_notice(" libev backend: %s\n", backend_name);
128
129         return status;
130 }
131
132 #endif /* LWS_USE_LIBEV */
133
134 /**
135  * libwebsocket_cancel_service() - Cancel servicing of pending websocket activity
136  * @context:    Websocket context
137  *
138  *      This function let a call to libwebsocket_service() waiting for a timeout
139  *      immediately return.
140  */
141 LWS_VISIBLE void
142 libwebsocket_cancel_service(struct libwebsocket_context *context)
143 {
144         char buf = 0;
145
146         if (write(context->dummy_pipe_fds[1], &buf, sizeof(buf)) != 1)
147                 lwsl_err("Cannot write to dummy pipe");
148 }
149
150 LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
151 {
152         int syslog_level = LOG_DEBUG;
153
154         switch (level) {
155         case LLL_ERR:
156                 syslog_level = LOG_ERR;
157                 break;
158         case LLL_WARN:
159                 syslog_level = LOG_WARNING;
160                 break;
161         case LLL_NOTICE:
162                 syslog_level = LOG_NOTICE;
163                 break;
164         case LLL_INFO:
165                 syslog_level = LOG_INFO;
166                 break;
167         }
168         syslog(syslog_level, "%s", line);
169 }