diff --git a/core/channels.c b/core/channels.c index ea717a19..ce8a4927 100644 --- a/core/channels.c +++ b/core/channels.c @@ -231,6 +231,14 @@ struct uwsgi_buffer *uwsgi_channel_recv(struct wsgi_request *wsgi_req, struct uw return ub; } +void uwsgi_channels_reset_worker_subscriptions(int wid) { + struct uwsgi_channel *channel = uwsgi.channels; + while(channel) { + memset(channel->subscriptions + (uwsgi.cores * (wid-1)), 0, uwsgi.cores); + channel = channel->next; + } +} + void *uwsgi_channels_loop(void *foobar) { // block all signals diff --git a/core/master_utils.c b/core/master_utils.c index 1c01708f..34703741 100644 --- a/core/master_utils.c +++ b/core/master_utils.c @@ -517,6 +517,9 @@ int uwsgi_respawn_worker(int wid) { int i; + // reset channels subscriptions + uwsgi_channels_reset_worker_subscriptions(wid); + if (uwsgi.threaded_logger) { pthread_mutex_lock(&uwsgi.threaded_logger_lock); } diff --git a/core/websockets.c b/core/websockets.c index bbc097b8..6fdf5008 100644 --- a/core/websockets.c +++ b/core/websockets.c @@ -263,26 +263,95 @@ ssize_t uwsgi_websockets_simple_send(struct wsgi_request *wsgi_req, struct uwsgi } ssize_t uwsgi_websockets_simple_recv(struct wsgi_request *wsgi_req) { - int fd = wsgi_req->poll.fd; int ret = -1; + int fd = wsgi_req->poll.fd; + + int count = 0; + struct uwsgi_channel *channel = uwsgi.channels; + while(channel) { + int pos = (uwsgi.cores * (uwsgi.mywid - 1)) + wsgi_req->async_id; + if (channel->subscriptions[pos] == 2) { + count++; + } + channel = channel->next; + } + + struct pollfd *pfd = uwsgi_calloc(sizeof(struct pollfd) * (count+1)); + pfd[0].fd = fd; + pfd[0].events = POLLIN; + channel = uwsgi.channels; + count = 1; + while(channel) { + int pos = (uwsgi.cores * (uwsgi.mywid - 1)) + wsgi_req->async_id; + if (channel->subscriptions[pos] == 2) { + pfd[count].fd = channel->fd[(pos*2)+1]; + pfd[count].events = POLLIN; + count++; + } + channel = channel->next; + } + retry: - ret = uwsgi_waitfd(fd, uwsgi.websockets_pong_freq); - if (ret < 0) return -1; + ret = poll(pfd, count, uwsgi.websockets_pong_freq * 1000); + if (ret < 0) { + uwsgi_error("uwsgi_websockets_simple_recv()/poll()"); + free(pfd); + return -1; + } // send ping if (ret == 0) { //unsolicited pong if (uwsgi_websockets_pong(wsgi_req)) { + free(pfd); return -1; } goto retry; } - ssize_t len = read(fd, wsgi_req->websocket_buf->buf + wsgi_req->websocket_buf->pos, wsgi_req->websocket_buf->len - wsgi_req->websocket_buf->pos); - if (len <= 0) { - uwsgi_error("[uwsgi-websocket] uwsgi_websockets_simple_recv()/read()"); + int i; + for(i=0;iwebsocket_buf->buf + wsgi_req->websocket_buf->pos, wsgi_req->websocket_buf->len - wsgi_req->websocket_buf->pos); + if (len <= 0) { + uwsgi_error("[uwsgi-websocket] uwsgi_websockets_simple_recv()/read()"); + } + free(pfd); + return len; + } + else { + channel = uwsgi.channels; + while(channel) { + int pos = (uwsgi.cores * (uwsgi.mywid - 1)) + wsgi_req->async_id; + int cfd = channel->fd[(pos*2)+1]; + if (cfd == pfd[i].fd) { + struct uwsgi_buffer *ub = uwsgi_buffer_new(channel->max_packet_size); + ssize_t len = read(pfd[i].fd, ub->buf, ub->len); + if (len <= 0) { + uwsgi_buffer_destroy(ub); + uwsgi_error("[uwsgi-websocket] uwsgi_websockets_simple_recv()/read()"); + free(pfd); + return -1; + } + ub->pos += len; + if (uwsgi_websocket_send(wsgi_req, ub->buf, ub->pos) <= 0) { + uwsgi_buffer_destroy(ub); + free(pfd); + return -1; + } + uwsgi_buffer_destroy(ub); + break; + } + channel = channel->next; + } + goto retry; + } + } } - return len; + + free(pfd); + return -1; } void uwsgi_websockets_init() { diff --git a/tests/websockets.py b/tests/websockets.py index 10b194c1..5585d1a9 100644 --- a/tests/websockets.py +++ b/tests/websockets.py @@ -38,7 +38,7 @@ def application(env, sr):

WebSocket

-
+
@@ -49,4 +49,5 @@ def application(env, sr): while True: msg = uwsgi.websocket_recv() print len(msg) - uwsgi.websocket_send("hello %s = %s" % (time.time(), msg)) + #uwsgi.websocket_send("hello %s = %s" % (time.time(), msg)) + uwsgi.channel_send('room001', "channel %s = %s" % (time.time(), msg)) diff --git a/uwsgi.h b/uwsgi.h index 64ad6d33..5d93384b 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -3749,6 +3749,7 @@ void *uwsgi_channels_loop(void *); struct uwsgi_channel *uwsgi_channel_by_name(char *name); void uwsgi_channels_leave(struct wsgi_request *); struct uwsgi_buffer *uwsgi_channel_recv(struct wsgi_request *, struct uwsgi_channel *, int); +void uwsgi_channels_reset_worker_subscriptions(int); void uwsgi_check_emperor(void); #ifdef UWSGI_AS_SHARED_LIBRARY