Browse Source

websocket: clang-format for coherent indentation and coding style

Victor Seva 2 năm trước cách đây
mục cha
commit
90eea708c8

+ 373 - 23
src/modules/websocket/utf8_decode.h

@@ -35,43 +35,393 @@
 #define UTF8_REJECT 12
 
 static const uint8_t utf8d[] = {
-	// The first part of the table maps bytes to character classes that
-	// to reduce the size of the transition table and create bitmasks.
-	0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
-	0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
-	0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
-	0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
-	1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,  9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
-	7, 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,  7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
-	8, 8,2,2,2,2,2,2,2,2,2,2,2,2,2,2,  2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
-	10,3,3,3,3,3,3,3,3,3,3,3,3,4,3,3, 11,6,6,6,5,8,8,8,8,8,8,8,8,8,8,8,
+		// The first part of the table maps bytes to character classes that
+		// to reduce the size of the transition table and create bitmasks.
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		0,
+		1,
+		1,
+		1,
+		1,
+		1,
+		1,
+		1,
+		1,
+		1,
+		1,
+		1,
+		1,
+		1,
+		1,
+		1,
+		1,
+		9,
+		9,
+		9,
+		9,
+		9,
+		9,
+		9,
+		9,
+		9,
+		9,
+		9,
+		9,
+		9,
+		9,
+		9,
+		9,
+		7,
+		7,
+		7,
+		7,
+		7,
+		7,
+		7,
+		7,
+		7,
+		7,
+		7,
+		7,
+		7,
+		7,
+		7,
+		7,
+		7,
+		7,
+		7,
+		7,
+		7,
+		7,
+		7,
+		7,
+		7,
+		7,
+		7,
+		7,
+		7,
+		7,
+		7,
+		7,
+		8,
+		8,
+		2,
+		2,
+		2,
+		2,
+		2,
+		2,
+		2,
+		2,
+		2,
+		2,
+		2,
+		2,
+		2,
+		2,
+		2,
+		2,
+		2,
+		2,
+		2,
+		2,
+		2,
+		2,
+		2,
+		2,
+		2,
+		2,
+		2,
+		2,
+		2,
+		2,
+		10,
+		3,
+		3,
+		3,
+		3,
+		3,
+		3,
+		3,
+		3,
+		3,
+		3,
+		3,
+		3,
+		4,
+		3,
+		3,
+		11,
+		6,
+		6,
+		6,
+		5,
+		8,
+		8,
+		8,
+		8,
+		8,
+		8,
+		8,
+		8,
+		8,
+		8,
+		8,
 
-	// The second part is a transition table that maps a combination
-	// of a state of the automation and a character class to a state.
-	0, 12,24,36,60,96,84,12,12,12,48,72, 12,12,12,12,12,12,12,12,12,12,12,12,
-	12, 0,12,12,12,12,12, 0,12, 0,12,12, 12,24,12,12,12,12,12,24,12,24,12,12,
-	12,12,12,12,12,12,12,24,12,12,12,12, 12,24,12,12,12,12,12,12,12,24,12,12,
-	12,12,12,12,12,12,12,36,12,36,12,12, 12,36,12,12,12,12,12,36,12,36,12,12,
-	12,36,12,12,12,12,12,12,12,12,12,12,
+		// The second part is a transition table that maps a combination
+		// of a state of the automation and a character class to a state.
+		0,
+		12,
+		24,
+		36,
+		60,
+		96,
+		84,
+		12,
+		12,
+		12,
+		48,
+		72,
+		12,
+		12,
+		12,
+		12,
+		12,
+		12,
+		12,
+		12,
+		12,
+		12,
+		12,
+		12,
+		12,
+		0,
+		12,
+		12,
+		12,
+		12,
+		12,
+		0,
+		12,
+		0,
+		12,
+		12,
+		12,
+		24,
+		12,
+		12,
+		12,
+		12,
+		12,
+		24,
+		12,
+		24,
+		12,
+		12,
+		12,
+		12,
+		12,
+		12,
+		12,
+		12,
+		12,
+		24,
+		12,
+		12,
+		12,
+		12,
+		12,
+		24,
+		12,
+		12,
+		12,
+		12,
+		12,
+		12,
+		12,
+		24,
+		12,
+		12,
+		12,
+		12,
+		12,
+		12,
+		12,
+		12,
+		12,
+		36,
+		12,
+		36,
+		12,
+		12,
+		12,
+		36,
+		12,
+		12,
+		12,
+		12,
+		12,
+		36,
+		12,
+		36,
+		12,
+		12,
+		12,
+		36,
+		12,
+		12,
+		12,
+		12,
+		12,
+		12,
+		12,
+		12,
+		12,
+		12,
 };
 
-static inline uint32_t decode(uint32_t* state, uint32_t* codep, uint32_t byte)
+static inline uint32_t decode(uint32_t *state, uint32_t *codep, uint32_t byte)
 {
 	uint32_t type = utf8d[byte];
 
-	*codep = (*state != UTF8_ACCEPT) ?
-		(byte & 0x3fu) | (*codep << 6) :
-		(0xff >> type) & (byte);
+	*codep = (*state != UTF8_ACCEPT) ? (byte & 0x3fu) | (*codep << 6)
+									 : (0xff >> type) & (byte);
 
 	*state = utf8d[256 + *state + type];
 	return *state;
 }
 
-static inline int IsUTF8(uint8_t* s, size_t len)
+static inline int IsUTF8(uint8_t *s, size_t len)
 {
 	uint32_t codepoint, state = 0;
 
-	while (len--)
+	while(len--)
 		decode(&state, &codepoint, *s++);
 
 	return state == UTF8_ACCEPT;

+ 6 - 7
src/modules/websocket/websocket.c

@@ -301,20 +301,19 @@ static int child_init(int rank)
 		if(ws_keepalive_mechanism != KEEPALIVE_MECHANISM_NONE) {
 			for(i = 0; i < ws_keepalive_processes; i++) {
 				if(fork_sync_timer(PROC_TIMER, "WEBSOCKET KEEPALIVE", 1,
-						   ws_keepalive, (void*)(long)i, ws_keepalive_interval)
+						   ws_keepalive, (void *)(long)i, ws_keepalive_interval)
 						< 0) {
 					LM_ERR("starting keepalive process\n");
 					return -1;
 				}
 			}
 		}
-		if(fork_sync_timer(PROC_TIMER, "WEBSOCKET TIMER", 1,
-			   ws_timer, NULL, ws_timer_interval)
-					< 0) {
-				LM_ERR("starting timer process\n");
-				return -1;
+		if(fork_sync_timer(PROC_TIMER, "WEBSOCKET TIMER", 1, ws_timer, NULL,
+				   ws_timer_interval)
+				< 0) {
+			LM_ERR("starting timer process\n");
+			return -1;
 		}
-
 	}
 
 	return 0;

+ 15 - 14
src/modules/websocket/ws_conn.c

@@ -68,7 +68,7 @@ stat_var *ws_msrp_max_concurrent_connections;
 char *wsconn_state_str[] = {
 		"CONNECTING", /* WS_S_CONNECTING */
 		"OPEN",		  /* WS_S_OPEN */
-		"CLOSING",	/* WS_S_CLOSING */
+		"CLOSING",	  /* WS_S_CLOSING */
 		"CLOSED"	  /* WS_S_CLOSED */
 };
 
@@ -106,8 +106,8 @@ int wsconn_init(void)
 	memset((void *)wsconn_id_hash, 0,
 			TCP_ID_HASH_SIZE * sizeof(ws_connection_t *));
 
-	wsconn_used_list = (ws_connection_list_t *)shm_malloc(
-			sizeof(ws_connection_list_t));
+	wsconn_used_list =
+			(ws_connection_list_t *)shm_malloc(sizeof(ws_connection_list_t));
 	if(wsconn_used_list == NULL) {
 		SHM_MEM_ERROR_FMT("for WebSocket used list\n");
 		goto error;
@@ -210,8 +210,7 @@ int wsconn_add(struct receive_info *rcv, unsigned int sub_protocol)
 	wsc->frag_buf.s = ((char *)wsc) + sizeof(ws_connection_t);
 	atomic_set(&wsc->refcnt, 0);
 
-	LM_DBG("new wsc => [%p], ref => [%d]\n", wsc,
-			atomic_get(&wsc->refcnt));
+	LM_DBG("new wsc => [%p], ref => [%d]\n", wsc, atomic_get(&wsc->refcnt));
 
 	WSCONN_LOCK;
 	/* Add to WebSocket connection table */
@@ -309,8 +308,10 @@ static void wsconn_run_route(ws_connection_t *wsc)
 	init_run_actions_ctx(&ctx);
 	if(rt < 0) {
 		/* kemi script event route callback */
-		if(keng && sr_kemi_route(keng,fmsg, EVENT_ROUTE, &ws_event_callback,
-					&evrtname) < 0) {
+		if(keng
+				&& sr_kemi_route(keng, fmsg, EVENT_ROUTE, &ws_event_callback,
+						   &evrtname)
+						   < 0) {
 			LM_ERR("error running event route kemi callback\n");
 		}
 	} else {
@@ -487,8 +488,7 @@ int wsconn_put_id(int id)
 	WSCONN_LOCK;
 	for(wsc = wsconn_id_hash[id_hash]; wsc; wsc = wsc->id_next) {
 		if(wsc->id == id) {
-			LM_DBG("wsc [%p] refcnt [%d]\n", wsc,
-					atomic_get(&wsc->refcnt));
+			LM_DBG("wsc [%p] refcnt [%d]\n", wsc, atomic_get(&wsc->refcnt));
 			wsconn_put_mode(wsc, 0);
 
 			WSCONN_UNLOCK;
@@ -667,7 +667,7 @@ int wsconn_put_list_ids(ws_connection_id_t *list_head)
 		return -1;
 
 	list = list_head;
-	for(i=0; list[i].id!=-1; i++) {
+	for(i = 0; list[i].id != -1; i++) {
 		wsconn_put_id(list[i].id);
 	}
 
@@ -713,7 +713,7 @@ void ws_timer(unsigned int ticks, void *param)
 	}
 	WSCONN_UNLOCK;
 
-	for(wsc = rmlist.head; wsc; ) {
+	for(wsc = rmlist.head; wsc;) {
 		next = wsc->id_next;
 		wsconn_dtor(wsc);
 		wsc = next;
@@ -748,9 +748,10 @@ static int ws_rpc_add_node(
 		else
 			sub_protocol = "**UNKNOWN**";
 
-		if(snprintf(rplbuf, 512, "%d: %s:%s:%hu -> %s:%s:%hu (state: %s"
-								 ", %s last used %ds ago"
-								 ", sub-protocol: %s)",
+		if(snprintf(rplbuf, 512,
+				   "%d: %s:%s:%hu -> %s:%s:%hu (state: %s"
+				   ", %s last used %ds ago"
+				   ", sub-protocol: %s)",
 				   wsc->id, src_proto, strlen(src_ip) ? src_ip : "*",
 				   con->rcv.src_port, dst_proto, strlen(dst_ip) ? dst_ip : "*",
 				   con->rcv.dst_port, wsconn_state_str[wsc->state], pong,

+ 4 - 2
src/modules/websocket/ws_conn.h

@@ -33,7 +33,8 @@
 #include "../../core/rpc.h"
 #include "../../core/timer.h"
 
-typedef enum {
+typedef enum
+{
 	WS_S_CONNECTING = 0, /* Never used - included for completeness */
 	WS_S_OPEN,
 	WS_S_CLOSING,
@@ -77,7 +78,8 @@ typedef struct
 	ws_connection_t *tail;
 } ws_connection_list_t;
 
-typedef enum {
+typedef enum
+{
 	WSCONN_EVENTROUTE_NO = 0,
 	WSCONN_EVENTROUTE_YES
 } ws_conn_eventroute_t;

+ 21 - 14
src/modules/websocket/ws_frame.c

@@ -79,7 +79,11 @@ typedef struct
 	ws_connection_t *wsc;
 } ws_frame_t;
 
-typedef enum { CONN_CLOSE_DO = 0, CONN_CLOSE_DONT } conn_close_t;
+typedef enum
+{
+	CONN_CLOSE_DO = 0,
+	CONN_CLOSE_DONT
+} conn_close_t;
 
 #define BYTE0_MASK_FIN (0x80)
 #define BYTE0_MASK_RSV1 (0x40)
@@ -630,9 +634,9 @@ int ws_frame_receive(sr_event_param_t *evp)
 				if((frame.payload_len == CRLF_LEN
 						   && strncmp(frame.payload_data, CRLF, CRLF_LEN) == 0)
 						|| (frame.payload_len == CRLFCRLF_LEN
-								   && strncmp(frame.payload_data, CRLFCRLF,
-											  CRLFCRLF_LEN)
-											  == 0)) {
+								&& strncmp(frame.payload_data, CRLFCRLF,
+										   CRLFCRLF_LEN)
+										   == 0)) {
 					ws_send_crlf(frame.wsc, opcode);
 					wsconn_put(frame.wsc);
 					return 0;
@@ -676,7 +680,8 @@ int ws_frame_receive(sr_event_param_t *evp)
 					return -1;
 				}
 			} else {
-				LM_ERR("Unrecognized WebSocket subprotocol: %u\n", frame.wsc->sub_protocol);
+				LM_ERR("Unrecognized WebSocket subprotocol: %u\n",
+						frame.wsc->sub_protocol);
 				return -1;
 			}
 
@@ -802,20 +807,22 @@ void ws_keepalive(unsigned int ticks, void *param)
 	if(!list_head)
 		return;
 
-	while(list_head[i].id!=-1) {
+	while(list_head[i].id != -1) {
 		wsc = wsconn_get(list_head[i].id);
 		if(wsc && wsc->last_used < check_time) {
 			if(wsc->state == WS_S_CLOSING || wsc->awaiting_pong) {
 				LM_WARN("forcibly closing connection\n");
 				wsconn_close_now(wsc);
-			} else if (ws_keepalive_mechanism == KEEPALIVE_MECHANISM_CONCHECK) {
+			} else if(ws_keepalive_mechanism == KEEPALIVE_MECHANISM_CONCHECK) {
 				if(wsc->state == WS_S_REMOVING) {
-					LM_DBG("ws (id: %d wsc: %p) in removing state ignoring keepalive\n",
+					LM_DBG("ws (id: %d wsc: %p) in removing state ignoring "
+						   "keepalive\n",
 							wsc->id, wsc);
 				} else {
 					tcp_connection_t *con = tcpconn_get(wsc->id, 0, 0, 0, 0);
-					if(con==NULL) {
-						LM_INFO("tcp connection has been lost (id: %d wsc: %p)\n",
+					if(con == NULL) {
+						LM_INFO("tcp connection has been lost (id: %d wsc: "
+								"%p)\n",
 								wsc->id, wsc);
 						wsc->state = WS_S_CLOSING;
 					} else {
@@ -823,9 +830,10 @@ void ws_keepalive(unsigned int ticks, void *param)
 					}
 				}
 			} else {
-				int opcode = (ws_keepalive_mechanism == KEEPALIVE_MECHANISM_PING)
-								 ? OPCODE_PING
-								 : OPCODE_PONG;
+				int opcode =
+						(ws_keepalive_mechanism == KEEPALIVE_MECHANISM_PING)
+								? OPCODE_PING
+								: OPCODE_PONG;
 				ping_pong(wsc, opcode);
 			}
 		}
@@ -833,7 +841,6 @@ void ws_keepalive(unsigned int ticks, void *param)
 			wsconn_put_id(list_head[i].id);
 		}
 		i++;
-
 	}
 
 	wsconn_put_list_ids(list_head);

+ 5 - 1
src/modules/websocket/ws_frame.h

@@ -34,7 +34,11 @@
 #include "../../core/rpc.h"
 #include "ws_conn.h"
 
-typedef enum { LOCAL_CLOSE = 0, REMOTE_CLOSE } ws_close_type_t;
+typedef enum
+{
+	LOCAL_CLOSE = 0,
+	REMOTE_CLOSE
+} ws_close_type_t;
 
 enum
 {