1
0
mirror of https://github.com/GOSTSec/sgminer synced 2025-01-10 23:08:07 +00:00

Windows doesn't work with MSG_PEEK on recv so move to a continuously updating buffer for incoming messages.

This commit is contained in:
Con Kolivas 2012-10-04 23:18:33 +10:00
parent 77c5a006aa
commit deb0a9b644
4 changed files with 47 additions and 27 deletions

View File

@ -4161,7 +4161,7 @@ static void *stratum_thread(void *userdata)
sleep(5); sleep(5);
} }
} }
s = recv_line(pool->sock); s = recv_line(pool);
if (unlikely(!s)) if (unlikely(!s))
continue; continue;
if (!parse_method(pool, s) && !parse_stratum_response(s)) if (!parse_method(pool, s) && !parse_stratum_response(s))

View File

@ -795,6 +795,9 @@ struct stratum_work {
int diff; int diff;
}; };
#define RECVSIZE 8191
#define RBUFSIZE (RECVSIZE + 1)
struct pool { struct pool {
int pool_no; int pool_no;
int prio; int prio;
@ -861,6 +864,7 @@ struct pool {
/* Stratum variables */ /* Stratum variables */
char *stratum_url; char *stratum_url;
SOCKETTYPE sock; SOCKETTYPE sock;
char sockbuf[RBUFSIZE];
struct sockaddr_in *server, client; struct sockaddr_in *server, client;
char *sockaddr_url; /* stripped url used for sockaddr */ char *sockaddr_url; /* stripped url used for sockaddr */
char *nonce1; char *nonce1;

62
util.c
View File

@ -888,19 +888,24 @@ out_unlock:
#define RECVSIZE 8191 #define RECVSIZE 8191
#define RBUFSIZE (RECVSIZE + 1) #define RBUFSIZE (RECVSIZE + 1)
static void clear_sock(SOCKETTYPE sock) static void clear_sock(struct pool *pool)
{ {
char s[RBUFSIZE]; SOCKETTYPE sock = pool->sock;
recv(sock, s, RECVSIZE, MSG_DONTWAIT); recv(sock, pool->sockbuf, RECVSIZE, MSG_DONTWAIT);
strcpy(pool->sockbuf, "");
} }
/* Check to see if Santa's been good to you */ /* Check to see if Santa's been good to you */
static bool sock_full(SOCKETTYPE sock, bool wait) static bool sock_full(struct pool *pool, bool wait)
{ {
SOCKETTYPE sock = pool->sock;
struct timeval timeout; struct timeval timeout;
fd_set rd; fd_set rd;
if (strlen(pool->sockbuf))
return true;
FD_ZERO(&rd); FD_ZERO(&rd);
FD_SET(sock, &rd); FD_SET(sock, &rd);
timeout.tv_usec = 0; timeout.tv_usec = 0;
@ -915,30 +920,41 @@ static bool sock_full(SOCKETTYPE sock, bool wait)
/* 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(SOCKETTYPE sock) char *recv_line(struct pool *pool)
{ {
char *sret = NULL, s[RBUFSIZE], c; SOCKETTYPE sock = pool->sock;
ssize_t offset = 0; ssize_t len, buflen;
char *tok, *sret = NULL;
if (SOCKETFAIL(recv(sock, s, RECVSIZE, MSG_PEEK))) { if (!strstr(pool->sockbuf, "\n")) {
char s[RBUFSIZE];
memset(s, 0, RBUFSIZE);
if (SOCKETFAIL(recv(sock, s, RECVSIZE, 0))) {
applog(LOG_DEBUG, "Failed to recv sock in recv_line"); applog(LOG_DEBUG, "Failed to recv sock in recv_line");
goto out; goto out;
} }
sret = strtok(s, "\n"); strcat(pool->sockbuf, s);
if (!sret) { }
buflen = strlen(pool->sockbuf);
tok = strtok(pool->sockbuf, "\n");
if (!tok) {
applog(LOG_DEBUG, "Failed to parse a \\n terminated string in recv_line"); applog(LOG_DEBUG, "Failed to parse a \\n terminated string in recv_line");
goto out; goto out;
} }
sret = strdup(tok);
len = strlen(sret);
do { /* Copy what's left in the buffer after the \n, including the
read(sock, &c, 1); * terminating \0 */
memcpy(s + offset++, &c, 1); if (buflen > len + 1)
} while (strncmp(&c, "\n", 1)); memmove(pool->sockbuf, pool->sockbuf + len + 1, buflen - len + 1);
sret = strdup(s); else
strcpy(sret + offset - 1, "\0"); strcpy(pool->sockbuf, "");
out: out:
if (!sret) if (!sret)
clear_sock(sock); clear_sock(pool);
else if (opt_protocol) else if (opt_protocol)
applog(LOG_DEBUG, "RECVD: %s", sret); applog(LOG_DEBUG, "RECVD: %s", sret);
return sret; return sret;
@ -1138,10 +1154,10 @@ bool auth_stratum(struct pool *pool)
swork_id++, pool->rpc_user, pool->rpc_pass); swork_id++, pool->rpc_user, pool->rpc_pass);
/* Parse all data prior sending auth request */ /* Parse all data prior sending auth request */
while (sock_full(pool->sock, false)) { while (sock_full(pool, false)) {
sret = recv_line(pool->sock); sret = recv_line(pool);
if (!parse_method(pool, sret)) { if (!parse_method(pool, sret)) {
clear_sock(pool->sock); clear_sock(pool);
applog(LOG_WARNING, "Failed to parse stratum buffer"); applog(LOG_WARNING, "Failed to parse stratum buffer");
free(sret); free(sret);
return ret; return ret;
@ -1152,7 +1168,7 @@ bool auth_stratum(struct pool *pool)
if (!stratum_send(pool, s, strlen(s))) if (!stratum_send(pool, s, strlen(s)))
goto out; goto out;
sret = recv_line(pool->sock); sret = recv_line(pool);
if (!sret) if (!sret)
goto out; goto out;
val = JSON_LOADS(sret, &err); val = JSON_LOADS(sret, &err);
@ -1208,12 +1224,12 @@ bool initiate_stratum(struct pool *pool)
goto out; goto out;
} }
if (!sock_full(pool->sock, true)) { if (!sock_full(pool, true)) {
applog(LOG_DEBUG, "Timed out waiting for response in initiate_stratum"); applog(LOG_DEBUG, "Timed out waiting for response in initiate_stratum");
goto out; goto out;
} }
sret = recv_line(pool->sock); sret = recv_line(pool);
if (!sret) if (!sret)
goto out; goto out;

2
util.h
View File

@ -44,7 +44,7 @@
struct pool; struct pool;
bool stratum_send(struct pool *pool, char *s, ssize_t len); bool stratum_send(struct pool *pool, char *s, ssize_t len);
char *recv_line(SOCKETTYPE sock); char *recv_line(struct pool *pool);
bool parse_method(struct pool *pool, char *s); bool parse_method(struct pool *pool, char *s);
bool extract_sockaddr(struct pool *pool, char *url); bool extract_sockaddr(struct pool *pool, char *url);
bool auth_stratum(struct pool *pool); bool auth_stratum(struct pool *pool);