Browse code

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

Peter Dunkley authored on 23/06/2012 18:10:03
Showing 5 changed files
... ...
@@ -96,9 +96,14 @@ int sr_event_register_cb(int type, sr_event_cb_f f)
96 96
 					_sr_events_list.tcp_msrp_frame = f;
97 97
 				else return -1;
98 98
 			break;
99
-		case SREV_TCP_WS_FRAME:
100
-				if(_sr_events_list.tcp_ws_frame==0)
101
-					_sr_events_list.tcp_ws_frame = f;
99
+		case SREV_TCP_WS_FRAME_IN:
100
+				if(_sr_events_list.tcp_ws_frame_in==0)
101
+					_sr_events_list.tcp_ws_frame_in = f;
102
+				else return -1;
103
+			break;
104
+		case SREV_TCP_WS_FRAME_OUT:
105
+				if(_sr_events_list.tcp_ws_frame_out==0)
106
+					_sr_events_list.tcp_ws_frame_out = f;
102 107
 				else return -1;
103 108
 			break;
104 109
 		default:
... ...
@@ -192,10 +197,16 @@ int sr_event_exec(int type, void *data)
192 192
 					ret = _sr_events_list.tcp_msrp_frame(data);
193 193
 					return ret;
194 194
 				} else return 1;
195
-		case SREV_TCP_WS_FRAME:
196
-				if(unlikely(_sr_events_list.tcp_ws_frame!=0))
195
+		case SREV_TCP_WS_FRAME_IN:
196
+				if(unlikely(_sr_events_list.tcp_ws_frame_in!=0))
197
+				{
198
+					ret = _sr_events_list.tcp_ws_frame_in(data);
199
+					return ret;
200
+				} else return 1;
201
+		case SREV_TCP_WS_FRAME_OUT:
202
+				if(unlikely(_sr_events_list.tcp_ws_frame_out!=0))
197 203
 				{
198
-					ret = _sr_events_list.tcp_ws_frame(data);
204
+					ret = _sr_events_list.tcp_ws_frame_out(data);
199 205
 					return ret;
200 206
 				} else return 1;
201 207
 		default:
... ...
@@ -227,8 +238,10 @@ int sr_event_enabled(int type)
227 227
 				return (_sr_events_list.tcp_http_100c!=0)?1:0;
228 228
 		case SREV_TCP_MSRP_FRAME:
229 229
 				return (_sr_events_list.tcp_msrp_frame!=0)?1:0;
230
-		case SREV_TCP_WS_FRAME:
231
-				return (_sr_events_list.tcp_ws_frame!=0)?1:0;
230
+		case SREV_TCP_WS_FRAME_IN:
231
+				return (_sr_events_list.tcp_ws_frame_in!=0)?1:0;
232
+		case SREV_TCP_WS_FRAME_OUT:
233
+				return (_sr_events_list.tcp_ws_frame_out!=0)?1:0;
232 234
 	}
233 235
 	return 0;
234 236
 }
... ...
@@ -32,7 +32,8 @@
32 32
 #define SREV_NET_DGRAM_IN		7
33 33
 #define SREV_TCP_HTTP_100C		8
34 34
 #define SREV_TCP_MSRP_FRAME		9
35
-#define SREV_TCP_WS_FRAME		10
35
+#define SREV_TCP_WS_FRAME_IN		10
36
+#define SREV_TCP_WS_FRAME_OUT		11
36 37
 
37 38
 
38 39
 typedef int (*sr_event_cb_f)(void *data);
... ...
@@ -47,7 +48,8 @@ typedef struct sr_event_cb {
47 47
 	sr_event_cb_f net_dgram_in;
48 48
 	sr_event_cb_f tcp_http_100c;
49 49
 	sr_event_cb_f tcp_msrp_frame;
50
-	sr_event_cb_f tcp_ws_frame;
50
+	sr_event_cb_f tcp_ws_frame_in;
51
+	sr_event_cb_f tcp_ws_frame_out;
51 52
 } sr_event_cb_t;
52 53
 
53 54
 void sr_event_cb_init(void);
... ...
@@ -54,6 +54,7 @@
54 54
 #include "udp_server.h"
55 55
 #ifdef USE_TCP
56 56
 #include "tcp_server.h"
57
+#include "tcp_conn.h"
57 58
 #endif
58 59
 #ifdef USE_SCTP
59 60
 #include "sctp_server.h"
... ...
@@ -128,12 +129,34 @@ static inline int msg_send(struct dest_info* dst, char* buf, int len)
128 128
 #ifdef USE_TCP 
129 129
 	union sockaddr_union* from;
130 130
 	union sockaddr_union local_addr;
131
+	struct tcp_connection *con = NULL;
132
+	struct ws_event_info wsev;
131 133
 #endif
132 134
 	
133 135
 	outb.s = buf;
134 136
 	outb.len = len;
135 137
 	sr_event_exec(SREV_NET_DATA_OUT, (void*)&outb);
136
-	
138
+
139
+#ifdef USE_TCP
140
+	if (unlikely(sr_event_enabled(SREV_TCP_WS_FRAME_OUT))) {
141
+		if (dst->proto == PROTO_TCP)
142
+			con = tcpconn_get(dst->id, 0, 0, 0, 0);
143
+#ifdef USE_TLS
144
+		else if (dst->proto == PROTO_TLS)
145
+			con = tcpconn_get(dst->id, 0, 0, 0, 0);
146
+#endif
147
+		if (con->flags & F_CONN_WS)
148
+		{
149
+			memset(&wsev, 0, sizeof(ws_event_info_t));
150
+			wsev.type = SREV_TCP_WS_FRAME_OUT;
151
+			wsev.buf = outb.s;
152
+			wsev.len = outb.len;
153
+			wsev.id = dst->id;
154
+			return sr_event_exec(SREV_TCP_WS_FRAME_OUT, (void *) &wsev);
155
+		}
156
+	}
157
+#endif
158
+
137 159
 	if (likely(dst->proto==PROTO_UDP)){
138 160
 		if (unlikely((dst->send_sock==0) || 
139 161
 					(dst->send_sock->flags & SI_IS_MCAST))){
... ...
@@ -355,6 +355,13 @@ typedef struct tcp_event_info {
355 355
 	struct tcp_connection *con;
356 356
 } tcp_event_info_t;
357 357
 
358
+typedef struct ws_event_info {
359
+	int type;
360
+	char *buf;
361
+	unsigned int len;
362
+	int id;
363
+} ws_event_info_t;
364
+
358 365
 #endif
359 366
 
360 367
 
... ...
@@ -1132,14 +1132,14 @@ static int ws_process_msg(char* tcpbuf, unsigned int len,
1132 1132
 
1133 1133
 	ret = 0;
1134 1134
 	LM_DBG("WebSocket Message: [[>>>\n%.*s<<<]]\n", len, tcpbuf);
1135
-	if(likely(sr_event_enabled(SREV_TCP_WS_FRAME))) {
1135
+	if(likely(sr_event_enabled(SREV_TCP_WS_FRAME_IN))) {
1136 1136
 		memset(&tev, 0, sizeof(tcp_event_info_t));
1137
-		tev.type = SREV_TCP_WS_FRAME;
1137
+		tev.type = SREV_TCP_WS_FRAME_IN;
1138 1138
 		tev.buf = tcpbuf;
1139 1139
 		tev.len = len;
1140 1140
 		tev.rcv = rcv_info;
1141 1141
 		tev.con = con;
1142
-		ret = sr_event_exec(SREV_TCP_WS_FRAME, (void*)(&tev));
1142
+		ret = sr_event_exec(SREV_TCP_WS_FRAME_IN, (void*)(&tev));
1143 1143
 	} else {
1144 1144
 		LM_DBG("no callback registering for handling WebSockets - dropping!\n");
1145 1145
 	}