Skip to content

Commit

Permalink
ws: for now, we only need the state in shmem
Browse files Browse the repository at this point in the history
  • Loading branch information
razvancrainea committed Mar 9, 2015
1 parent 5d659ff commit 922fc63
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 220 deletions.
122 changes: 3 additions & 119 deletions modules/proto_ws/proto_ws.c
Expand Up @@ -31,24 +31,21 @@

#include "../../pt.h"
#include "../../sr_module.h"
#include "../../net/net_tcp.h"
#include "../../net/api_proto.h"
#include "../../net/api_proto_net.h"
#include "../../socket_info.h"
#include "../../tsend.h"
#include "../../receive.h"
#include "proto_ws.h"
#include "ws_handshake.h"
#include "ws.h"

static int mod_init(void);
static int proto_ws_init(struct proto_info *pi);
static int proto_ws_init_listener(struct socket_info *si);
static int proto_ws_send(struct socket_info* send_sock,
char* buf, unsigned int len, union sockaddr_union* to, int id);

static int ws_read_req(struct tcp_connection* con, int* bytes_read);
static int ws_conn_init(struct tcp_connection* c);
static void ws_conn_clean(struct tcp_connection* c);

/* parameters*/
int ws_max_msg_chunks = TCP_CHILD_MAX_MSG_CHUNK;
Expand Down Expand Up @@ -104,8 +101,6 @@ static int proto_ws_init(struct proto_info *pi)

pi->net.flags = PROTO_NET_USE_TCP;
pi->net.read = (proto_net_read_f)ws_read_req;
pi->net.conn_init = ws_conn_init;
pi->net.conn_clean = ws_conn_clean;

return 0;
}
Expand All @@ -127,40 +122,6 @@ static int proto_ws_init_listener(struct socket_info *si)
}


static int ws_conn_init(struct tcp_connection* c)
{
struct ws_data *d;
struct ws_hs *hs;

d = (struct ws_data *)shm_malloc(sizeof(struct ws_data));
if (!d) {
LM_ERR("Failed to create ws in shm memory\n");
return -1;
}
hs = (struct ws_hs *)shm_malloc(sizeof(struct ws_hs));
if (!hs) {
LM_ERR("Failed to alloc handshake structure in shm memory\n");
shm_free(d);
return -1;
}
memset(hs, 0, sizeof(struct ws_hs));

/* initialize all fields here */
d->state = WS_CON_HANDSHAKE;
d->handshake = hs;
c->proto_data = (void *)d;
return 0;
}


static void ws_conn_clean(struct tcp_connection* c)
{
struct ws_data *wsd = (struct ws_data *)c->proto_data;
wsd->handshake = NULL;
shm_free(wsd);
c->proto_data = NULL;
}




Expand Down Expand Up @@ -249,80 +210,6 @@ static int proto_ws_send(struct socket_info* send_sock,
}


/* Responsible for writing the TCP send chunks - called under con write lock
* * if returns = 1 : the connection will be released for more writting
* * if returns = 0 : the connection will be released
* * if returns < 0 : the connection will be released as BAD / broken
*/
#if 0
static int ws_write_async_req(struct tcp_connection* con)
{
int n,left;
struct tcp_send_chunk *chunk;
struct tcp_data *d = (struct tcp_data*)con->proto_data;

if (d->async_chunks_no == 0) {
LM_DBG("The connection has been triggered "
" for a write event - but we have no pending write chunks\n");
return 0;
}

next_chunk:
chunk=d->async_chunks[0];
again:
left = (int)((chunk->buf+chunk->len)-chunk->pos);
LM_DBG("Trying to send %d bytes from chunk %p in conn %p - %d %d \n",
left,chunk,con,chunk->ticks,get_ticks());
n=send(con->fd, chunk->pos, left,
#ifdef HAVE_MSG_NOSIGNAL
MSG_NOSIGNAL
#else
0
#endif
);

if (n<0) {
if (errno==EINTR)
goto again;
else if (errno==EAGAIN || errno==EWOULDBLOCK) {
LM_DBG("Can't finish to write chunk %p on conn %p\n",
chunk,con);
/* report back we have more writting to be done */
return 1;
} else {
LM_ERR("Error occured while sending async chunk %d (%s)\n",
errno,strerror(errno));
/* report the conn as broken */
return -1;
}
}

if (n < left) {
/* partial write */
chunk->pos+=n;
goto again;
} else {
/* written a full chunk - move to the next one, if any */
shm_free(chunk);
d->async_chunks_no--;
if (d->async_chunks_no == 0) {
LM_DBG("We have finished writing all our async chunks in %p\n",con);
d->oldest_chunk=0;
/* report back everything ok */
return 0;
} else {
LM_DBG("We still have %d chunks pending on %p\n",
d->async_chunks_no,con);
memmove(&d->async_chunks[0],&d->async_chunks[1],
d->async_chunks_no * sizeof(struct tcp_send_chunk*));
d->oldest_chunk = d->async_chunks[0]->ticks;
goto next_chunk;
}
}
return 0;
}
#endif



/************** READ related functions ***************/
Expand All @@ -336,12 +223,9 @@ static int ws_write_async_req(struct tcp_connection* con)
*/
static int ws_read_req(struct tcp_connection* con, int* bytes_read)
{
struct ws_data* wsd;
int size;

wsd = (struct ws_data *)con->proto_data;

if (wsd->state != WS_CON_HANDSHAKE_DONE) {
if (WS_STATE(con) != WS_CON_HANDSHAKE_DONE) {

size = ws_handshake(con);
if (size < 0) {
Expand All @@ -351,7 +235,7 @@ static int ws_read_req(struct tcp_connection* con, int* bytes_read)
if (size == 0)
goto done;
}
if (wsd->state == WS_CON_HANDSHAKE_DONE && ws_process(con) < 0)
if (WS_STATE(con) == WS_CON_HANDSHAKE_DONE && ws_process(con) < 0)
goto error;

done:
Expand Down
17 changes: 15 additions & 2 deletions modules/proto_ws/proto_ws.h
Expand Up @@ -29,11 +29,23 @@
#define WS_SUPPORTED_VERSION 13 /*!< WebSocket supported version */
#define WS_DEFAULT_PORT 80 /*!< WebSocket default port */

#include "ws_handshake.h"

enum ws_conn_states { WS_CON_INIT, WS_CON_HANDSHAKE, WS_CON_HANDSHAKE_DONE,
enum ws_conn_state { WS_CON_INIT, WS_CON_HANDSHAKE, WS_CON_HANDSHAKE_DONE,
WS_CON_BAD_REQ };

#define WS_STATE(_c) \
((enum ws_conn_state)(unsigned long)((_c)->proto_data))
#define WS_SET_STATE(_c, _s) \
(_c)->proto_data = (((void *)(unsigned long)(_s)))

/*
* For now we only need the state stored in the connection
* Later, we should probably store info about origin, resoruce. versions,
* protocols supported, etc. - razvanc
*/
#if 0
#include "ws_handshake.h"

struct ws_data {
/* the state of the connection */
enum ws_conn_states state;
Expand All @@ -42,5 +54,6 @@ struct ws_data {
* it after the handshake is completed */
struct ws_hs *handshake;
};
#endif

#endif /* _PROTO_WS_H_ */
1 change: 1 addition & 0 deletions modules/proto_ws/ws.h
Expand Up @@ -35,6 +35,7 @@ extern int ws_max_msg_chunks;
extern int ws_send_timeout;

int ws_process(struct tcp_connection *con);
int ws_handshake(struct tcp_connection *con);
int ws_req_write(struct tcp_connection *con, int fd, char *buf, int len);

#endif /* _WS_H_ */

0 comments on commit 922fc63

Please sign in to comment.