瀏覽代碼

tcp: minor optimization

- tcp_req complete & has_content_len transformed into flags
  (4 bytes saved per connection)
Andrei Pelinescu-Onciul 16 年之前
父節點
當前提交
0051630c26
共有 2 個文件被更改,包括 31 次插入24 次删除
  1. 7 2
      tcp_conn.h
  2. 24 22
      tcp_read.c

+ 7 - 2
tcp_conn.h

@@ -114,13 +114,18 @@ struct tcp_req{
 	char* body; /* body position */
 	char* body; /* body position */
 	unsigned int b_size; /* buffer size-1 (extra space for 0-term)*/
 	unsigned int b_size; /* buffer size-1 (extra space for 0-term)*/
 	int content_len;
 	int content_len;
-	int has_content_len; /* 1 if content_length was parsed ok*/
-	int complete; /* 1 if one req has been fully read, 0 otherwise*/
+	unsigned short flags; /* F_TCP_REQ_HAS_CLEN | F_TCP_REQ_COMPLETE */
 	int bytes_to_go; /* how many bytes we have still to read from the body*/
 	int bytes_to_go; /* how many bytes we have still to read from the body*/
 	enum tcp_req_errors error;
 	enum tcp_req_errors error;
 	enum tcp_req_states state;
 	enum tcp_req_states state;
 };
 };
 
 
+/* tcp_req flags */
+#define F_TCP_REQ_HAS_CLEN 1
+#define F_TCP_REQ_COMPLETE 2
+
+#define TCP_REQ_HAS_CLEN(tr)  ((tr)->flags & F_TCP_REQ_HAS_CLEN)
+#define TCP_REQ_COMPLETE(tr)  ((tr)->flags & F_TCP_REQ_COMPLETE)
 
 
 
 
 struct tcp_connection;
 struct tcp_connection;

+ 24 - 22
tcp_read.c

@@ -218,19 +218,19 @@ int tcp_read_headers(struct tcp_connection *c, int* read_flags)
 	#define content_len_beg_case \
 	#define content_len_beg_case \
 					case ' ': \
 					case ' ': \
 					case '\t': \
 					case '\t': \
-						if (!r->has_content_len) r->state=H_STARTWS; \
+						if (!TCP_REQ_HAS_CLEN(r)) r->state=H_STARTWS; \
 						else r->state=H_SKIP; \
 						else r->state=H_SKIP; \
 							/* not interested if we already found one */ \
 							/* not interested if we already found one */ \
 						break; \
 						break; \
 					case 'C': \
 					case 'C': \
 					case 'c': \
 					case 'c': \
-						if(!r->has_content_len) r->state=H_CONT_LEN1; \
+						if(!TCP_REQ_HAS_CLEN(r)) r->state=H_CONT_LEN1; \
 						else r->state=H_SKIP; \
 						else r->state=H_SKIP; \
 						break; \
 						break; \
 					case 'l': \
 					case 'l': \
 					case 'L': \
 					case 'L': \
 						/* short form for Content-Length */ \
 						/* short form for Content-Length */ \
-						if (!r->has_content_len) r->state=H_L_COLON; \
+						if (!TCP_REQ_HAS_CLEN(r)) r->state=H_L_COLON; \
 						else r->state=H_SKIP; \
 						else r->state=H_SKIP; \
 						break
 						break
 						
 						
@@ -272,7 +272,7 @@ int tcp_read_headers(struct tcp_connection *c, int* read_flags)
 				r->bytes_to_go-=remaining;
 				r->bytes_to_go-=remaining;
 				p+=remaining;
 				p+=remaining;
 				if (r->bytes_to_go==0){
 				if (r->bytes_to_go==0){
-					r->complete=1;
+					r->flags|=F_TCP_REQ_COMPLETE;
 					goto skip;
 					goto skip;
 				}
 				}
 				break;
 				break;
@@ -298,11 +298,11 @@ int tcp_read_headers(struct tcp_connection *c, int* read_flags)
 					case '\n':
 					case '\n':
 						/* found LF LF */
 						/* found LF LF */
 						r->state=H_BODY;
 						r->state=H_BODY;
-						if (r->has_content_len){
+						if (TCP_REQ_HAS_CLEN(r)){
 							r->body=p+1;
 							r->body=p+1;
 							r->bytes_to_go=r->content_len;
 							r->bytes_to_go=r->content_len;
 							if (r->bytes_to_go==0){
 							if (r->bytes_to_go==0){
-								r->complete=1;
+								r->flags|=F_TCP_REQ_COMPLETE;
 								p++;
 								p++;
 								goto skip;
 								goto skip;
 							}
 							}
@@ -322,11 +322,11 @@ int tcp_read_headers(struct tcp_connection *c, int* read_flags)
 				if (*p=='\n'){
 				if (*p=='\n'){
 					/* found LF CR LF */
 					/* found LF CR LF */
 					r->state=H_BODY;
 					r->state=H_BODY;
-					if (r->has_content_len){
+					if (TCP_REQ_HAS_CLEN(r)){
 						r->body=p+1;
 						r->body=p+1;
 						r->bytes_to_go=r->content_len;
 						r->bytes_to_go=r->content_len;
 						if (r->bytes_to_go==0){
 						if (r->bytes_to_go==0){
-							r->complete=1;
+							r->flags|=F_TCP_REQ_COMPLETE;
 							p++;
 							p++;
 							goto skip;
 							goto skip;
 						}
 						}
@@ -410,8 +410,8 @@ int tcp_read_headers(struct tcp_connection *c, int* read_flags)
 			case H_SKIP_EMPTY_CRLFCR_FOUND:
 			case H_SKIP_EMPTY_CRLFCR_FOUND:
 				if (*p=='\n'){
 				if (*p=='\n'){
 					r->state = H_PING_CRLF;
 					r->state = H_PING_CRLF;
-					r->complete = 1;
-					r->has_content_len = 1; /* hack to avoid error check */
+					r->flags |= F_TCP_REQ_HAS_CLEN |
+							F_TCP_REQ_COMPLETE; /* hack to avoid error check */
 					p++;
 					p++;
 					goto skip;
 					goto skip;
 				}else{
 				}else{
@@ -437,7 +437,7 @@ int tcp_read_headers(struct tcp_connection *c, int* read_flags)
 					/* using has_content_len as a flag if there should be
 					/* using has_content_len as a flag if there should be
 					 * fingerprint or no
 					 * fingerprint or no
 					 */
 					 */
-					r->has_content_len = (mc == MAGIC_COOKIE) ? 1 : 0;
+					r->flags |= (mc == MAGIC_COOKIE) ? F_TCP_REQ_HAS_CLEN : 0;
 					
 					
 					r->body += sizeof(struct stun_hdr);
 					r->body += sizeof(struct stun_hdr);
 					p = r->body; 
 					p = r->body; 
@@ -485,8 +485,8 @@ int tcp_read_headers(struct tcp_connection *c, int* read_flags)
 					r->body += body_len;
 					r->body += body_len;
 					p = r->body;
 					p = r->body;
 					r->state = H_STUN_END;
 					r->state = H_STUN_END;
-					r->complete = 1;
-					r->has_content_len = 1; /* hack to avoid error check */
+					r->flags |= F_TCP_REQ_COMPLETE |
+						F_TCP_REQ_HAS_CLEN; /* hack to avoid error check */
 					goto skip;
 					goto skip;
 				}
 				}
 				else {
 				else {
@@ -563,12 +563,12 @@ int tcp_read_headers(struct tcp_connection *c, int* read_flags)
 					case ' ':
 					case ' ':
 					case '\t': /* FIXME: check if line contains only WS */
 					case '\t': /* FIXME: check if line contains only WS */
 						r->state=H_SKIP;
 						r->state=H_SKIP;
-						r->has_content_len=1;
+						r->flags|=F_TCP_REQ_HAS_CLEN;
 						break;
 						break;
 					case '\n':
 					case '\n':
 						/* end of line, parse successful */
 						/* end of line, parse successful */
 						r->state=H_LF;
 						r->state=H_LF;
-						r->has_content_len=1;
+						r->flags|=F_TCP_REQ_HAS_CLEN;
 						break;
 						break;
 					default:
 					default:
 						LOG(L_ERR, "ERROR: tcp_read_headers: bad "
 						LOG(L_ERR, "ERROR: tcp_read_headers: bad "
@@ -643,7 +643,8 @@ again:
 			 * if req. is complete we might have a second unparsed
 			 * if req. is complete we might have a second unparsed
 			 * request after it, so postpone release_with_eof
 			 * request after it, so postpone release_with_eof
 			 */
 			 */
-			if (unlikely((con->state==S_CONN_EOF) && (req->complete==0))) {
+			if (unlikely((con->state==S_CONN_EOF) && 
+						(! TCP_REQ_COMPLETE(req)))) {
 				DBG( "tcp_read_req: EOF\n");
 				DBG( "tcp_read_req: EOF\n");
 				resp=CONN_EOF;
 				resp=CONN_EOF;
 				goto end_req;
 				goto end_req;
@@ -660,7 +661,7 @@ again:
 			resp=CONN_ERROR;
 			resp=CONN_ERROR;
 			goto end_req;
 			goto end_req;
 		}
 		}
-		if (likely(req->complete)){
+		if (likely(TCP_REQ_COMPLETE(req))){
 #ifdef EXTRA_DEBUG
 #ifdef EXTRA_DEBUG
 			DBG("tcp_read_req: end of header part\n");
 			DBG("tcp_read_req: end of header part\n");
 			DBG("- received from: port %d\n", con->rcv.src_port);
 			DBG("- received from: port %d\n", con->rcv.src_port);
@@ -668,7 +669,7 @@ again:
 			DBG("tcp_read_req: headers:\n%.*s.\n",
 			DBG("tcp_read_req: headers:\n%.*s.\n",
 					(int)(req->body-req->start), req->start);
 					(int)(req->body-req->start), req->start);
 #endif
 #endif
-			if (likely(req->has_content_len)){
+			if (likely(TCP_REQ_HAS_CLEN(req))){
 				DBG("tcp_read_req: content-length= %d\n", req->content_len);
 				DBG("tcp_read_req: content-length= %d\n", req->content_len);
 #ifdef EXTRA_DEBUG
 #ifdef EXTRA_DEBUG
 				DBG("tcp_read_req: body:\n%.*s\n", req->content_len,req->body);
 				DBG("tcp_read_req: body:\n%.*s\n", req->content_len,req->body);
@@ -733,7 +734,8 @@ again:
 			req->body=0;
 			req->body=0;
 			req->error=TCP_REQ_OK;
 			req->error=TCP_REQ_OK;
 			req->state=H_SKIP_EMPTY;
 			req->state=H_SKIP_EMPTY;
-			req->complete=req->content_len=req->has_content_len=0;
+			req->flags=0;
+			req->content_len=0;
 			req->bytes_to_go=0;
 			req->bytes_to_go=0;
 			req->pos=req->buf+size;
 			req->pos=req->buf+size;
 			
 			
@@ -1085,15 +1087,15 @@ error:
 #ifdef USE_STUN
 #ifdef USE_STUN
 int is_msg_complete(struct tcp_req* r)
 int is_msg_complete(struct tcp_req* r)
 {
 {
-	if (r->has_content_len == 1) {
+	if (TCP_REQ_HAS_CLEN(r)) {
 		r->state = H_STUN_FP;
 		r->state = H_STUN_FP;
 		return 0;
 		return 0;
 	}
 	}
 	else {
 	else {
 		/* STUN message is complete */
 		/* STUN message is complete */
 		r->state = H_STUN_END;
 		r->state = H_STUN_END;
-		r->complete = 1;
-		r->has_content_len = 1; /* hack to avoid error check */
+		r->flags |= F_TCP_REQ_COMPLETE |
+					F_TCP_REQ_HAS_CLEN; /* hack to avoid error check */
 		return 1;
 		return 1;
 	}
 	}
 }
 }