synchrnous websockets + channels implementation

This commit is contained in:
Roberto De Ioris
2013-01-13 10:53:11 +01:00
parent 50fe00d142
commit 7ac4282047
5 changed files with 91 additions and 9 deletions
+8
View File
@@ -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
+3
View File
@@ -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);
}
+76 -7
View File
@@ -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;i<count;i++) {
if (pfd[i].revents & POLLIN) {
if (pfd[i].fd == fd) {
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()");
}
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() {
+3 -2
View File
@@ -38,7 +38,7 @@ def application(env, sr):
<h1>WebSocket</h1>
<input type="text" id="testo"/>
<input type="button" value="invia" onClick="invia();"/>
<div id="blackboard" style="width:640px;height:480px;background-color:black;color:white;border: solid 2px red">
<div id="blackboard" style="width:640px;height:480px;background-color:black;color:white;border: solid 2px red;overflow:auto">
</div>
</body>
</html>
@@ -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))
+1
View File
@@ -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