[Sheepdog] [PATCH 4/7] make socket non-blocking

MORITA Kazutaka morita.kazutaka at lab.ntt.co.jp
Thu Apr 7 02:56:20 CEST 2011


Signed-off-by: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>
---
 lib/net.c     |   16 ++++++----------
 sheep/sdnet.c |   12 +++++++-----
 2 files changed, 13 insertions(+), 15 deletions(-)

diff --git a/lib/net.c b/lib/net.c
index 10fbbd8..96173d4 100644
--- a/lib/net.c
+++ b/lib/net.c
@@ -70,25 +70,21 @@ int rx(struct connection *conn, enum conn_state next_state)
 int tx(struct connection *conn, enum conn_state next_state, int flags)
 {
 	int ret;
-again:
+
 	ret = send(conn->fd, conn->tx_buf, conn->tx_length, flags);
 	if (ret < 0) {
-		if (errno == EAGAIN)
-			goto again;
-
-		conn->c_tx_state = C_IO_CLOSED;
+		if (errno != EAGAIN)
+			conn->c_tx_state = C_IO_CLOSED;
 		return 0;
 	}
 
 	conn->tx_length -= ret;
 	conn->tx_buf = (char *)conn->tx_buf + ret;
 
-	if (conn->tx_length)
-		goto again;
+	if (!conn->tx_length)
+		conn->c_tx_state = next_state;
 
-	conn->c_tx_state = next_state;
-
-	return 1;
+	return ret;
 }
 
 int create_listen_ports(int port, int (*callback)(int fd, void *), void *data)
diff --git a/sheep/sdnet.c b/sheep/sdnet.c
index 7e21072..a172a4f 100644
--- a/sheep/sdnet.c
+++ b/sheep/sdnet.c
@@ -429,11 +429,7 @@ again:
 
 	switch (ci->conn.c_tx_state) {
 	case C_IO_HEADER:
-		if (rsp->data_length)
-			ret = tx(&ci->conn, C_IO_DATA_INIT, MSG_MORE);
-		else
-			ret = tx(&ci->conn, C_IO_DATA_INIT, 0);
-
+		ret = tx(&ci->conn, C_IO_DATA_INIT, 0);
 		if (!ret)
 			break;
 
@@ -548,6 +544,12 @@ static void listen_handler(int listen_fd, int events, void *data)
 		return;
 	}
 
+	ret = set_nonblocking(fd);
+	if (ret) {
+		close(fd);
+		return;
+	}
+
 	ci = create_client(fd, data);
 	if (!ci) {
 		close(fd);
-- 
1.5.6.5




More information about the sheepdog mailing list