Browse Source

Merge commit 'f2ae7fa' into cg_merges_20130818a

Conflicts:
	util.c
Luke Dashjr 12 years ago
parent
commit
8561e5b71e
1 changed files with 13 additions and 12 deletions
  1. 13 12
      util.c

+ 13 - 12
util.c

@@ -54,6 +54,8 @@
 #include "compat.h"
 #include "util.h"
 
+#define DEFAULT_SOCKWAIT 60
+
 bool successful_connect = false;
 struct timeval nettime;
 
@@ -1403,7 +1405,7 @@ bool _stratum_send(struct pool *pool, char *s, ssize_t len, bool force)
 	return (ret == SEND_OK);
 }
 
-static bool socket_full(struct pool *pool, bool wait)
+static bool socket_full(struct pool *pool, int wait)
 {
 	SOCKETTYPE sock = pool->sock;
 	struct timeval timeout;
@@ -1412,10 +1414,7 @@ static bool socket_full(struct pool *pool, bool wait)
 	FD_ZERO(&rd);
 	FD_SET(sock, &rd);
 	timeout.tv_usec = 0;
-	if (wait)
-		timeout.tv_sec = 60;
-	else
-		timeout.tv_sec = 1;
+	timeout.tv_sec = wait;
 	if (select(sock + 1, &rd, NULL, NULL, &timeout) > 0)
 		return true;
 	return false;
@@ -1427,7 +1426,7 @@ bool sock_full(struct pool *pool)
 	if (strlen(pool->sockbuf))
 		return true;
 
-	return (socket_full(pool, false));
+	return (socket_full(pool, 0));
 }
 
 static void clear_sockbuf(struct pool *pool)
@@ -1476,14 +1475,15 @@ static void recalloc_sock(struct pool *pool, size_t len)
  * from the socket and returns that as a malloced char */
 char *recv_line(struct pool *pool)
 {
-	ssize_t len, buflen;
 	char *tok, *sret = NULL;
+	ssize_t len, buflen;
+	int waited = 0;
 
 	if (!strstr(pool->sockbuf, "\n")) {
 		struct timeval rstart, now;
 
 		cgtime(&rstart);
-		if (!socket_full(pool, true)) {
+		if (!socket_full(pool, DEFAULT_SOCKWAIT)) {
 			applog(LOG_DEBUG, "Timed out waiting for data on socket_full");
 			goto out;
 		}
@@ -1500,11 +1500,13 @@ char *recv_line(struct pool *pool)
 				suspend_stratum(pool);
 				break;
 			}
+			cgtime(&now);
+			waited = tdiff(&now, &rstart);
 			if (n < 0) {
 				//Save errno from being overweitten bei socket_ commands 
 				int socket_recv_errno;
 				socket_recv_errno = SOCKERR;
-				if (!sock_blocks() || !socket_full(pool, true)) {
+				if (!sock_blocks() || !socket_full(pool, DEFAULT_SOCKWAIT - waited)) {
 					applog(LOG_DEBUG, "Failed to recv sock in recv_line: %s", bfg_strerror(socket_recv_errno, BST_SOCKET));
 					suspend_stratum(pool);
 					break;
@@ -1514,8 +1516,7 @@ char *recv_line(struct pool *pool)
 				recalloc_sock(pool, slen);
 				strcat(pool->sockbuf, s);
 			}
-			cgtime(&now);
-		} while (tdiff(&now, &rstart) < 60 && !strstr(pool->sockbuf, "\n"));
+		} while (waited < DEFAULT_SOCKWAIT && !strstr(pool->sockbuf, "\n"));
 	}
 
 	buflen = strlen(pool->sockbuf);
@@ -2133,7 +2134,7 @@ resend:
 
 	recvd = true;
 	
-	if (!socket_full(pool, true)) {
+	if (!socket_full(pool, DEFAULT_SOCKWAIT)) {
 		applog(LOG_DEBUG, "Timed out waiting for response in initiate_stratum");
 		goto out;
 	}