[sheepdog] [PATCH 5/7] net: refactor client_rx_handler()

Liu Yuan namei.unix at gmail.com
Wed Jul 18 11:35:38 CEST 2012


From: Liu Yuan <tailai.ly at taobao.com>


Signed-off-by: Liu Yuan <tailai.ly at taobao.com>
---
 sheep/sdnet.c |   32 ++++++++++++++++++++++++--------
 1 file changed, 24 insertions(+), 8 deletions(-)

diff --git a/sheep/sdnet.c b/sheep/sdnet.c
index 54ce6bd..7208413 100644
--- a/sheep/sdnet.c
+++ b/sheep/sdnet.c
@@ -506,7 +506,7 @@ static void init_rx_hdr(struct client_info *ci)
 	ci->conn.rx_buf = &ci->conn.rx_hdr;
 }
 
-static void client_rx_handler(struct client_info *ci)
+static inline int prepare_rx(struct client_info *ci)
 {
 	int ret;
 	uint64_t data_len;
@@ -547,14 +547,23 @@ static void client_rx_handler(struct client_info *ci)
 		eprintf("bug: unknown state %d\n", conn->c_rx_state);
 	}
 
-	if (is_conn_dead(conn))
-		return clear_client_info(ci);
+	if (is_conn_dead(conn)) {
+		clear_client_info(ci);
+		return -1;
+	}
 
 	/* Short read happens */
 	if (conn->c_rx_state != C_IO_END)
-		return;
+		return -1;
+
+	return 0;
+}
+
+static inline void finish_rx(struct client_info *ci)
+{
+	struct request *req;
+	struct sd_req *hdr = &ci->conn.rx_hdr;
 
-	/* now we have a complete command */
 	req = ci->rx_req;
 	init_rx_hdr(ci);
 	if (hdr->flags & SD_FLAG_CMD_WRITE)
@@ -562,11 +571,18 @@ static void client_rx_handler(struct client_info *ci)
 	else
 		req->rp.data_length = hdr->data_length;
 
-	dprintf("connection from: %d, %s:%d\n", ci->conn.fd,
-		ci->conn.ipstr, ci->conn.port);
+	dprintf("%d, %s:%d\n", ci->conn.fd, ci->conn.ipstr, ci->conn.port);
 	queue_request(req);
 }
 
+static void do_client_rx(struct client_info *ci)
+{
+	if (prepare_rx(ci) < 0)
+		return;
+
+	finish_rx(ci);
+}
+
 static void init_tx_hdr(struct client_info *ci)
 {
 	struct sd_rsp *rsp = (struct sd_rsp *)&ci->conn.tx_hdr;
@@ -741,7 +757,7 @@ static void client_handler(int fd, int events, void *data)
 		return clear_client_info(ci);
 
 	if (events & EPOLLIN)
-		client_rx_handler(ci);
+		do_client_rx(ci);
 
 	if (events & EPOLLOUT)
 		client_tx_handler(ci);
-- 
1.7.10.2




More information about the sheepdog mailing list