libuv initial replace libev version

This commit is contained in:
Alex Hultman 2016-02-01 08:31:54 +08:00 committed by Andy Green
parent 1b89b933a6
commit fa4619416e
4 changed files with 62 additions and 46 deletions

View file

@ -378,9 +378,10 @@ lws_context_destroy(struct lws_context *context)
}
}
#ifdef LWS_USE_LIBEV
ev_io_stop(context->io_loop, &context->w_accept.watcher);
if (context->use_ev_sigint)
ev_signal_stop(context->io_loop, &context->w_sigint.watcher);
uv_poll_stop(&context->w_accept.watcher);
//ev_io_stop(context->io_loop, &context->w_accept.watcher);
//if (context->use_ev_sigint)
//ev_signal_stop(context->io_loop, &context->w_sigint.watcher);
#endif /* LWS_USE_LIBEV */
for (n = 0; n < context->count_threads; n++) {

View file

@ -30,24 +30,24 @@ void lws_feature_status_libev(struct lws_context_creation_info *info)
}
static void
lws_accept_cb(struct ev_loop *loop, struct ev_io *watcher, int revents)
lws_accept_cb(uv_poll_t *watcher, int status, int revents)
{
struct lws_io_watcher *lws_io = container_of(watcher,
struct lws_io_watcher, watcher);
struct lws_context *context = lws_io->context;
struct lws_pollfd eventfd;
if (revents & EV_ERROR)
return;
if (status < 0/*revents & EV_ERROR*/)
return;
eventfd.fd = watcher->fd;
eventfd.fd = watcher->io_watcher.fd;
eventfd.events = 0;
eventfd.revents = EV_NONE;
if (revents & EV_READ) {
eventfd.revents = 0;//EV_NONE;
if (revents & UV_READABLE) {
eventfd.events |= LWS_POLLIN;
eventfd.revents |= LWS_POLLIN;
}
if (revents & EV_WRITE) {
if (revents & UV_WRITABLE) {
eventfd.events |= LWS_POLLOUT;
eventfd.revents |= LWS_POLLOUT;
}
@ -55,9 +55,9 @@ lws_accept_cb(struct ev_loop *loop, struct ev_io *watcher, int revents)
}
LWS_VISIBLE void
lws_sigint_cb(struct ev_loop *loop, struct ev_signal *watcher, int revents)
lws_sigint_cb(uv_loop_t *loop, uv_signal_t *watcher, int revents)
{
ev_break(loop, EVBREAK_ALL);
//ev_break(loop, EVBREAK_ALL);
}
LWS_VISIBLE int
@ -74,17 +74,17 @@ lws_sigint_cfg(struct lws_context *context, int use_ev_sigint,
}
LWS_VISIBLE int
lws_initloop(struct lws_context *context, struct ev_loop *loop)
lws_initloop(struct lws_context *context, uv_loop_t *loop)
{
struct ev_signal *w_sigint = &context->w_sigint.watcher;
struct ev_io *w_accept = &context->w_accept.watcher;
const char * backend_name;
//uv_signal_t *w_sigint = &context->w_sigint.watcher;
uv_poll_t *w_accept = &context->w_accept.watcher;
//const char * backend_name;
int status = 0;
int backend;
int m = 0; /* !!! TODO add pt support */
//int backend;
int m = 0; /* !!! TODO add pt support */
if (!loop)
loop = ev_default_loop(0);
loop = uv_default_loop();
context->io_loop = loop;
@ -92,15 +92,17 @@ lws_initloop(struct lws_context *context, struct ev_loop *loop)
* Initialize the accept w_accept with the listening socket
* and register a callback for read operations
*/
ev_io_init(w_accept, lws_accept_cb, context->pt[m].lserv_fd, EV_READ);
ev_io_start(context->io_loop,w_accept);
uv_poll_init(context->io_loop, w_accept, context->pt[m].lserv_fd);
uv_poll_start(w_accept, UV_READABLE, lws_accept_cb);
//ev_io_init(w_accept, lws_accept_cb, context->pt[m].lserv_fd, UV_READABLE);
//ev_io_start(context->io_loop,w_accept);
/* Register the signal watcher unless the user says not to */
if (context->use_ev_sigint) {
ev_signal_init(w_sigint, context->lws_ev_sigint_cb, SIGINT);
ev_signal_start(context->io_loop,w_sigint);
//ev_signal_init(w_sigint, context->lws_ev_sigint_cb, SIGINT);
//ev_signal_start(context->io_loop,w_sigint);
}
backend = ev_backend(loop);
/*backend = ev_backend(loop);
switch (backend) {
case EVBACKEND_SELECT:
@ -126,7 +128,7 @@ lws_initloop(struct lws_context *context, struct ev_loop *loop)
break;
}
lwsl_notice(" libev backend: %s\n", backend_name);
lwsl_notice(" libev backend: %s\n", backend_name);*/
return status;
}
@ -135,22 +137,24 @@ LWS_VISIBLE void
lws_libev_accept(struct lws *new_wsi, int accept_fd)
{
struct lws_context *context = lws_get_context(new_wsi);
struct ev_io *r = &new_wsi->w_read.watcher;
struct ev_io *w = &new_wsi->w_write.watcher;
uv_poll_t *r = &new_wsi->w_read.watcher;
//uv_poll_t *w = &new_wsi->w_write.watcher;
if (!LWS_LIBEV_ENABLED(context))
return;
new_wsi->w_read.context = context;
new_wsi->w_write.context = context;
ev_io_init(r, lws_accept_cb, accept_fd, EV_READ);
ev_io_init(w, lws_accept_cb, accept_fd, EV_WRITE);
uv_poll_init(context->io_loop, r, accept_fd);
//ev_io_init(r, lws_accept_cb, accept_fd, UV_READABLE);
//ev_io_init(w, lws_accept_cb, accept_fd, UV_WRITABLE);
}
LWS_VISIBLE void
lws_libev_io(struct lws *wsi, int flags)
{
struct lws_context *context = lws_get_context(wsi);
int current_events = wsi->w_read.watcher.io_watcher.pevents & (UV_READABLE | UV_WRITABLE);
if (!LWS_LIBEV_ENABLED(context))
return;
@ -162,15 +166,26 @@ lws_libev_io(struct lws *wsi, int flags)
(flags & (LWS_EV_READ | LWS_EV_WRITE)));
if (flags & LWS_EV_START) {
if (flags & LWS_EV_WRITE)
ev_io_start(context->io_loop, &wsi->w_write.watcher);
if (flags & LWS_EV_READ)
ev_io_start(context->io_loop, &wsi->w_read.watcher);
if (flags & LWS_EV_WRITE)
current_events |= UV_WRITABLE;
//ev_io_start(context->io_loop, &wsi->w_write.watcher);
if (flags & LWS_EV_READ)
current_events |= UV_READABLE;
//ev_io_start(context->io_loop, &wsi->w_read.watcher);
uv_poll_start(&wsi->w_read.watcher, current_events, lws_accept_cb);
} else {
if (flags & LWS_EV_WRITE)
ev_io_stop(context->io_loop, &wsi->w_write.watcher);
if (flags & LWS_EV_READ)
ev_io_stop(context->io_loop, &wsi->w_read.watcher);
if (flags & LWS_EV_WRITE)
current_events &= ~UV_WRITABLE;
//ev_io_stop(context->io_loop, &wsi->w_write.watcher);
if (flags & LWS_EV_READ)
current_events &= ~UV_READABLE;
//ev_io_stop(context->io_loop, &wsi->w_read.watcher);
if (!(current_events & (UV_READABLE | UV_WRITABLE)))
uv_poll_stop(&wsi->w_read.watcher);
else
uv_poll_start(&wsi->w_read.watcher, current_events, lws_accept_cb);
}
}
@ -190,5 +205,5 @@ LWS_VISIBLE void
lws_libev_run(const struct lws_context *context)
{
if (context->io_loop && LWS_LIBEV_ENABLED(context))
ev_run(context->io_loop, 0);
uv_run(context->io_loop, 0);
}

View file

@ -169,7 +169,7 @@ struct sockaddr_in;
#endif
#ifdef LWS_USE_LIBEV
#include <ev.h>
#include <uv.h>
#endif /* LWS_USE_LIBEV */
#ifndef LWS_EXTERN
@ -1456,17 +1456,17 @@ LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
lws_http_transaction_completed(struct lws *wsi);
#ifdef LWS_USE_LIBEV
typedef void (lws_ev_signal_cb)(EV_P_ struct ev_signal *w, int revents);
typedef void (lws_ev_signal_cb)(uv_loop_t *l, uv_signal_t *w, int revents);
LWS_VISIBLE LWS_EXTERN int
lws_sigint_cfg(struct lws_context *context, int use_ev_sigint,
lws_ev_signal_cb *cb);
LWS_VISIBLE LWS_EXTERN int
lws_initloop(struct lws_context *context, struct ev_loop *loop);
lws_initloop(struct lws_context *context, uv_loop_t *loop);
LWS_VISIBLE void
lws_sigint_cb(struct ev_loop *loop, struct ev_signal *watcher, int revents);
lws_sigint_cb(uv_loop_t *loop, uv_signal_t *watcher, int revents);
#endif /* LWS_USE_LIBEV */
LWS_VISIBLE LWS_EXTERN int

View file

@ -117,7 +117,7 @@
#include <arpa/inet.h>
#include <poll.h>
#ifdef LWS_USE_LIBEV
#include <ev.h>
#include <uv.h>
#endif /* LWS_USE_LIBEV */
#include <sys/mman.h>
@ -439,12 +439,12 @@ struct lws;
#ifdef LWS_USE_LIBEV
struct lws_io_watcher {
struct ev_io watcher;
uv_poll_t watcher;
struct lws_context* context;
};
struct lws_signal_watcher {
struct ev_signal watcher;
uv_signal_t watcher;
struct lws_context* context;
};
#endif /* LWS_USE_LIBEV */
@ -560,7 +560,7 @@ struct lws_context {
struct lws **lws_lookup; /* fd to wsi */
#endif
#ifdef LWS_USE_LIBEV
struct ev_loop* io_loop;
uv_loop_t *io_loop;
struct lws_io_watcher w_accept;
struct lws_signal_watcher w_sigint;
lws_ev_signal_cb* lws_ev_sigint_cb;