[sr-dev] git:pd/websocket: core: added events and data-structures so that Kamailio core can transmit through the WebSocket module

Peter Dunkley peter.dunkley at crocodile-rcs.com
Sat Jun 23 20:10:44 CEST 2012


Module: sip-router
Branch: pd/websocket
Commit: 1f139814262a4c5979c5301812f81304d885e92c
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=1f139814262a4c5979c5301812f81304d885e92c

Author: Peter Dunkley <peter.dunkley at crocodile-rcs.com>
Committer: Peter Dunkley <peter.dunkley at crocodile-rcs.com>
Date:   Sat Jun 23 19:10:03 2012 +0100

core: added events and data-structures so that Kamailio core can transmit through the WebSocket module

---

 events.c   |   29 +++++++++++++++++++++--------
 events.h   |    6 ++++--
 forward.h  |   25 ++++++++++++++++++++++++-
 tcp_conn.h |    7 +++++++
 tcp_read.c |    6 +++---
 5 files changed, 59 insertions(+), 14 deletions(-)

diff --git a/events.c b/events.c
index d4a24f8..d26ae9b 100644
--- a/events.c
+++ b/events.c
@@ -96,9 +96,14 @@ int sr_event_register_cb(int type, sr_event_cb_f f)
 					_sr_events_list.tcp_msrp_frame = f;
 				else return -1;
 			break;
-		case SREV_TCP_WS_FRAME:
-				if(_sr_events_list.tcp_ws_frame==0)
-					_sr_events_list.tcp_ws_frame = f;
+		case SREV_TCP_WS_FRAME_IN:
+				if(_sr_events_list.tcp_ws_frame_in==0)
+					_sr_events_list.tcp_ws_frame_in = f;
+				else return -1;
+			break;
+		case SREV_TCP_WS_FRAME_OUT:
+				if(_sr_events_list.tcp_ws_frame_out==0)
+					_sr_events_list.tcp_ws_frame_out = f;
 				else return -1;
 			break;
 		default:
@@ -192,10 +197,16 @@ int sr_event_exec(int type, void *data)
 					ret = _sr_events_list.tcp_msrp_frame(data);
 					return ret;
 				} else return 1;
-		case SREV_TCP_WS_FRAME:
-				if(unlikely(_sr_events_list.tcp_ws_frame!=0))
+		case SREV_TCP_WS_FRAME_IN:
+				if(unlikely(_sr_events_list.tcp_ws_frame_in!=0))
+				{
+					ret = _sr_events_list.tcp_ws_frame_in(data);
+					return ret;
+				} else return 1;
+		case SREV_TCP_WS_FRAME_OUT:
+				if(unlikely(_sr_events_list.tcp_ws_frame_out!=0))
 				{
-					ret = _sr_events_list.tcp_ws_frame(data);
+					ret = _sr_events_list.tcp_ws_frame_out(data);
 					return ret;
 				} else return 1;
 		default:
@@ -227,8 +238,10 @@ int sr_event_enabled(int type)
 				return (_sr_events_list.tcp_http_100c!=0)?1:0;
 		case SREV_TCP_MSRP_FRAME:
 				return (_sr_events_list.tcp_msrp_frame!=0)?1:0;
-		case SREV_TCP_WS_FRAME:
-				return (_sr_events_list.tcp_ws_frame!=0)?1:0;
+		case SREV_TCP_WS_FRAME_IN:
+				return (_sr_events_list.tcp_ws_frame_in!=0)?1:0;
+		case SREV_TCP_WS_FRAME_OUT:
+				return (_sr_events_list.tcp_ws_frame_out!=0)?1:0;
 	}
 	return 0;
 }
diff --git a/events.h b/events.h
index f919a00..734e90c 100644
--- a/events.h
+++ b/events.h
@@ -32,7 +32,8 @@
 #define SREV_NET_DGRAM_IN		7
 #define SREV_TCP_HTTP_100C		8
 #define SREV_TCP_MSRP_FRAME		9
-#define SREV_TCP_WS_FRAME		10
+#define SREV_TCP_WS_FRAME_IN		10
+#define SREV_TCP_WS_FRAME_OUT		11
 
 
 typedef int (*sr_event_cb_f)(void *data);
@@ -47,7 +48,8 @@ typedef struct sr_event_cb {
 	sr_event_cb_f net_dgram_in;
 	sr_event_cb_f tcp_http_100c;
 	sr_event_cb_f tcp_msrp_frame;
-	sr_event_cb_f tcp_ws_frame;
+	sr_event_cb_f tcp_ws_frame_in;
+	sr_event_cb_f tcp_ws_frame_out;
 } sr_event_cb_t;
 
 void sr_event_cb_init(void);
diff --git a/forward.h b/forward.h
index 674a6f0..d3831f9 100644
--- a/forward.h
+++ b/forward.h
@@ -54,6 +54,7 @@
 #include "udp_server.h"
 #ifdef USE_TCP
 #include "tcp_server.h"
+#include "tcp_conn.h"
 #endif
 #ifdef USE_SCTP
 #include "sctp_server.h"
@@ -128,12 +129,34 @@ static inline int msg_send(struct dest_info* dst, char* buf, int len)
 #ifdef USE_TCP 
 	union sockaddr_union* from;
 	union sockaddr_union local_addr;
+	struct tcp_connection *con = NULL;
+	struct ws_event_info wsev;
 #endif
 	
 	outb.s = buf;
 	outb.len = len;
 	sr_event_exec(SREV_NET_DATA_OUT, (void*)&outb);
-	
+
+#ifdef USE_TCP
+	if (unlikely(sr_event_enabled(SREV_TCP_WS_FRAME_OUT))) {
+		if (dst->proto == PROTO_TCP)
+			con = tcpconn_get(dst->id, 0, 0, 0, 0);
+#ifdef USE_TLS
+		else if (dst->proto == PROTO_TLS)
+			con = tcpconn_get(dst->id, 0, 0, 0, 0);
+#endif
+		if (con->flags & F_CONN_WS)
+		{
+			memset(&wsev, 0, sizeof(ws_event_info_t));
+			wsev.type = SREV_TCP_WS_FRAME_OUT;
+			wsev.buf = outb.s;
+			wsev.len = outb.len;
+			wsev.id = dst->id;
+			return sr_event_exec(SREV_TCP_WS_FRAME_OUT, (void *) &wsev);
+		}
+	}
+#endif
+
 	if (likely(dst->proto==PROTO_UDP)){
 		if (unlikely((dst->send_sock==0) || 
 					(dst->send_sock->flags & SI_IS_MCAST))){
diff --git a/tcp_conn.h b/tcp_conn.h
index 3b3f546..ec7c44f 100644
--- a/tcp_conn.h
+++ b/tcp_conn.h
@@ -355,6 +355,13 @@ typedef struct tcp_event_info {
 	struct tcp_connection *con;
 } tcp_event_info_t;
 
+typedef struct ws_event_info {
+	int type;
+	char *buf;
+	unsigned int len;
+	int id;
+} ws_event_info_t;
+
 #endif
 
 
diff --git a/tcp_read.c b/tcp_read.c
index d8b4653..e33984b 100644
--- a/tcp_read.c
+++ b/tcp_read.c
@@ -1132,14 +1132,14 @@ static int ws_process_msg(char* tcpbuf, unsigned int len,
 
 	ret = 0;
 	LM_DBG("WebSocket Message: [[>>>\n%.*s<<<]]\n", len, tcpbuf);
-	if(likely(sr_event_enabled(SREV_TCP_WS_FRAME))) {
+	if(likely(sr_event_enabled(SREV_TCP_WS_FRAME_IN))) {
 		memset(&tev, 0, sizeof(tcp_event_info_t));
-		tev.type = SREV_TCP_WS_FRAME;
+		tev.type = SREV_TCP_WS_FRAME_IN;
 		tev.buf = tcpbuf;
 		tev.len = len;
 		tev.rcv = rcv_info;
 		tev.con = con;
-		ret = sr_event_exec(SREV_TCP_WS_FRAME, (void*)(&tev));
+		ret = sr_event_exec(SREV_TCP_WS_FRAME_IN, (void*)(&tev));
 	} else {
 		LM_DBG("no callback registering for handling WebSockets - dropping!\n");
 	}




More information about the sr-dev mailing list