From b762a0aa7f2cfa24889cc308680d97a672591e0d Mon Sep 17 00:00:00 2001
From: Mathieu Parent <math.parent@gmail.com>
Date: Fri, 3 Sep 2010 19:08:41 +0200
Subject: [PATCH] Skinny: block socket to decrease CPU usage

non-blocking mode can still be used by setting MOD_SKINNY_NONBLOCK
---
 src/mod/endpoints/mod_skinny/mod_skinny.c      |  9 +++++----
 src/mod/endpoints/mod_skinny/skinny_protocol.c | 10 +++-------
 2 files changed, 8 insertions(+), 11 deletions(-)

diff --git a/src/mod/endpoints/mod_skinny/mod_skinny.c b/src/mod/endpoints/mod_skinny/mod_skinny.c
index 5da1367a35..cbeae6cf5a 100644
--- a/src/mod/endpoints/mod_skinny/mod_skinny.c
+++ b/src/mod/endpoints/mod_skinny/mod_skinny.c
@@ -1391,9 +1391,13 @@ static void *SWITCH_THREAD_FUNC listener_run(switch_thread_t *thread, void *obj)
 
 	switch_assert(listener != NULL);
 
+#if MOD_SKINNY_NONBLOCK
 	switch_socket_opt_set(listener->sock, SWITCH_SO_TCP_NODELAY, TRUE);
 	switch_socket_opt_set(listener->sock, SWITCH_SO_NONBLOCK, TRUE);
-
+#else
+	switch_socket_opt_set(listener->sock, SWITCH_SO_NONBLOCK, FALSE);
+	switch_socket_timeout_set(listener->sock, 5000000);
+#endif
 	if (listener->profile->debug > 0) {
 		if (zstr(listener->remote_ip)) {
 			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Connection Open\n");
@@ -1402,7 +1406,6 @@ static void *SWITCH_THREAD_FUNC listener_run(switch_thread_t *thread, void *obj)
 		}
 	}
 
-	switch_socket_opt_set(listener->sock, SWITCH_SO_NONBLOCK, TRUE);
 	switch_set_flag_locked(listener, LFLAG_RUNNING);
 	keepalive_listener(listener, NULL);
 	add_listener(listener);
@@ -1413,8 +1416,6 @@ static void *SWITCH_THREAD_FUNC listener_run(switch_thread_t *thread, void *obj)
 
 		if (status != SWITCH_STATUS_SUCCESS) {
 			switch(status) {
-				case SWITCH_STATUS_BREAK:
-					break;
 				case SWITCH_STATUS_TIMEOUT:
 					switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Communication Time Out with %s:%d.\n",
 						listener->remote_ip, listener->remote_port);
diff --git a/src/mod/endpoints/mod_skinny/skinny_protocol.c b/src/mod/endpoints/mod_skinny/skinny_protocol.c
index 28699b9c03..c519dc4bf5 100644
--- a/src/mod/endpoints/mod_skinny/skinny_protocol.c
+++ b/src/mod/endpoints/mod_skinny/skinny_protocol.c
@@ -118,10 +118,6 @@ switch_status_t skinny_read_packet(listener_t *listener, skinny_message_t **req)
 		return SWITCH_STATUS_MEMERR;
 	}
 
-	if (!listener_is_ready(listener)) {
-		return SWITCH_STATUS_BREAK;
-	}
-
 	ptr = mbuf;
 
 	while (listener_is_ready(listener)) {
@@ -137,10 +133,10 @@ switch_status_t skinny_read_packet(listener_t *listener, skinny_message_t **req)
 		status = switch_socket_recv(listener->sock, ptr, &mlen);
 
 		if (!listener_is_ready(listener)) {
-			return SWITCH_STATUS_BREAK;
+			break;
 		}
-		if (!SWITCH_STATUS_IS_BREAK(status) && status != SWITCH_STATUS_SUCCESS) {
-			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Socket break.\n");
+		if ((status != 70007 /* APR_TIMEUP */) && !SWITCH_STATUS_IS_BREAK(status) && (status != SWITCH_STATUS_SUCCESS)) {
+			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Socket break with status=%d.\n", status);
 			return SWITCH_STATUS_FALSE;
 		}