mirror of
https://github.com/GOSTSec/sgminer
synced 2025-01-11 07:17:58 +00:00
Provide a helper function for reading a single \n terminated string from a socket.
This commit is contained in:
parent
64df34cd4e
commit
07e6bd1262
54
util.c
54
util.c
@ -863,6 +863,41 @@ static bool sock_send(int sock, char *s, ssize_t len)
|
|||||||
|
|
||||||
#define RECVSIZE 8192
|
#define RECVSIZE 8192
|
||||||
|
|
||||||
|
static void clear_sock(SOCKETTYPE sock)
|
||||||
|
{
|
||||||
|
char *s = alloca(RECVSIZE);
|
||||||
|
|
||||||
|
recv(sock, s, RECVSIZE, MSG_DONTWAIT);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 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 */
|
||||||
|
static char *recv_line(SOCKETTYPE sock)
|
||||||
|
{
|
||||||
|
char *sret = NULL, *s;
|
||||||
|
ssize_t len;
|
||||||
|
|
||||||
|
s = alloca(RECVSIZE);
|
||||||
|
if (SOCKETFAIL(recv(sock, s, RECVSIZE, MSG_PEEK))) {
|
||||||
|
applog(LOG_DEBUG, "Failed to recv sock in recv_line");
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
sret = strtok(s, "\n");
|
||||||
|
if (!sret) {
|
||||||
|
applog(LOG_DEBUG, "Failed to parse a \\n terminated string in recv_line");
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
len = strlen(sret);
|
||||||
|
/* We know how much data is in the buffer so this read should not fail */
|
||||||
|
read(sock, s, len);
|
||||||
|
sret = strdup(s);
|
||||||
|
|
||||||
|
out:
|
||||||
|
if (!sret)
|
||||||
|
clear_sock(sock);
|
||||||
|
return sret;
|
||||||
|
}
|
||||||
|
|
||||||
bool initiate_stratum(struct pool *pool)
|
bool initiate_stratum(struct pool *pool)
|
||||||
{
|
{
|
||||||
json_t *val, *res_val, *err_val, *notify_val;
|
json_t *val, *res_val, *err_val, *notify_val;
|
||||||
@ -870,7 +905,6 @@ bool initiate_stratum(struct pool *pool)
|
|||||||
struct timeval timeout;
|
struct timeval timeout;
|
||||||
json_error_t err;
|
json_error_t err;
|
||||||
bool ret = false;
|
bool ret = false;
|
||||||
ssize_t len;
|
|
||||||
fd_set rd;
|
fd_set rd;
|
||||||
|
|
||||||
s = alloca(RECVSIZE);
|
s = alloca(RECVSIZE);
|
||||||
@ -898,22 +932,12 @@ bool initiate_stratum(struct pool *pool)
|
|||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (SOCKETFAIL(recv(pool->sock, s, RECVSIZE, MSG_PEEK | MSG_DONTWAIT))) {
|
sret = recv_line(pool->sock);
|
||||||
applog(LOG_DEBUG, "Failed to recv sock in initiate_stratum");
|
if (!sret)
|
||||||
goto out;
|
goto out;
|
||||||
}
|
|
||||||
|
|
||||||
sret = strtok(s, "\n");
|
val = JSON_LOADS(sret, &err);
|
||||||
if (!sret) {
|
free(sret);
|
||||||
applog(LOG_DEBUG, "Failed to parse a \\n terminated string in initiate_stratum");
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* We know how much data is in the buffer so this read should not fail */
|
|
||||||
len = strlen(sret);
|
|
||||||
read(pool->sock, s, len);
|
|
||||||
|
|
||||||
val = JSON_LOADS(s, &err);
|
|
||||||
if (!val) {
|
if (!val) {
|
||||||
applog(LOG_DEBUG, "JSON decode failed(%d): %s", err.line, err.text);
|
applog(LOG_DEBUG, "JSON decode failed(%d): %s", err.line, err.text);
|
||||||
goto out;
|
goto out;
|
||||||
|
Loading…
Reference in New Issue
Block a user