Browse Source

Change the pool stratum socket buffer to be dynamically allocated to accomodate any size coinbase and keep receiving data in recv line for up to 60s if no end of line has been received.

nfactor-troky
Con Kolivas 12 years ago
parent
commit
e1387dc85c
  1. 7
      miner.h
  2. 53
      util.c

7
miner.h

@ -859,8 +859,8 @@ struct stratum_work {
double diff; double diff;
}; };
#define RECVSIZE 8192 #define RBUFSIZE 8192
#define RBUFSIZE (RECVSIZE + 4) #define RECVSIZE (RBUFSIZE - 4)
struct pool { struct pool {
int pool_no; int pool_no;
@ -929,7 +929,8 @@ struct pool {
char *stratum_port; char *stratum_port;
CURL *stratum_curl; CURL *stratum_curl;
SOCKETTYPE sock; SOCKETTYPE sock;
char sockbuf[RBUFSIZE]; char *sockbuf;
size_t sockbuf_size;
char *sockaddr_url; /* stripped url used for sockaddr */ char *sockaddr_url; /* stripped url used for sockaddr */
char *nonce1; char *nonce1;
uint32_t nonce2; uint32_t nonce2;

53
util.c

@ -968,37 +968,59 @@ static void clear_sock(struct pool *pool)
strcpy(pool->sockbuf, ""); strcpy(pool->sockbuf, "");
} }
/* Make sure the pool sockbuf is large enough to cope with any coinbase size
* by reallocing it to a large enough size rounded up to a multiple of RBUFSIZE
* and zeroing the new memory */
static void recalloc_sock(struct pool *pool, size_t len)
{
size_t old, new;
old = strlen(pool->sockbuf);
new = old + len + 1;
if (new < pool->sockbuf_size)
return;
new = new + (RBUFSIZE - (new % RBUFSIZE));
applog(LOG_DEBUG, "Recallocing pool sockbuf to %d", new);
pool->sockbuf = realloc(pool->sockbuf, new);
if (!pool->sockbuf)
quit(1, "Failed to realloc pool sockbuf in recalloc_sock");
memset(pool->sockbuf + old, 0, new - old);
pool->sockbuf_size = new;
}
/* Peeks at a socket to find the first end of line and then reads just that /* Peeks at a socket to find the first end of line and then reads just that
* from the socket and returns that as a malloced char */ * from the socket and returns that as a malloced char */
char *recv_line(struct pool *pool) char *recv_line(struct pool *pool)
{ {
ssize_t len, buflen; ssize_t len, buflen;
char *tok, *sret = NULL; char *tok, *sret = NULL;
ssize_t n;
if (!strstr(pool->sockbuf, "\n")) { if (!strstr(pool->sockbuf, "\n")) {
char s[RBUFSIZE]; struct timeval rstart, now;
size_t sspace;
gettimeofday(&rstart, NULL);
if (!socket_full(pool, true)) { if (!socket_full(pool, true)) {
applog(LOG_DEBUG, "Timed out waiting for data on socket_full"); applog(LOG_DEBUG, "Timed out waiting for data on socket_full");
goto out; goto out;
} }
memset(s, 0, RBUFSIZE);
mutex_lock(&pool->stratum_lock); mutex_lock(&pool->stratum_lock);
n = recv(pool->sock, s, RECVSIZE, 0); do {
mutex_unlock(&pool->stratum_lock); char s[RBUFSIZE];
size_t slen, n;
memset(s, 0, RBUFSIZE);
n = recv(pool->sock, s, RECVSIZE, 0);
if (n < 1 && errno != EAGAIN && errno != EWOULDBLOCK) { if (n < 1 && errno != EAGAIN && errno != EWOULDBLOCK) {
applog(LOG_DEBUG, "Failed to recv sock in recv_line"); applog(LOG_DEBUG, "Failed to recv sock in recv_line");
goto out; break;
} }
/* Prevent buffer overflows, but if 8k is still not enough, slen = strlen(s);
* likely we have had some comms issues and the data is all recalloc_sock(pool, slen);
* useless anyway */ strcat(pool->sockbuf, s);
sspace = RECVSIZE - strlen(pool->sockbuf); gettimeofday(&now, NULL);
strncat(pool->sockbuf, s, sspace); } while (tdiff(&now, &rstart) < 60 && !strstr(pool->sockbuf, "\n"));
mutex_unlock(&pool->stratum_lock);
} }
buflen = strlen(pool->sockbuf); buflen = strlen(pool->sockbuf);
@ -1350,6 +1372,13 @@ bool initiate_stratum(struct pool *pool)
mutex_unlock(&pool->stratum_lock); mutex_unlock(&pool->stratum_lock);
curl = pool->stratum_curl; curl = pool->stratum_curl;
if (!pool->sockbuf) {
pool->sockbuf = calloc(RBUFSIZE, 1);
if (!pool->sockbuf)
quit(1, "Failed to calloc pool sockbuf in initiate_stratum");
pool->sockbuf_size = RBUFSIZE;
}
/* Create a http url for use with curl */ /* Create a http url for use with curl */
memset(s, 0, RBUFSIZE); memset(s, 0, RBUFSIZE);
sprintf(s, "http://%s:%s", pool->sockaddr_url, pool->stratum_port); sprintf(s, "http://%s:%s", pool->sockaddr_url, pool->stratum_port);

Loading…
Cancel
Save