Browse Source

Avoid needing to duplicate buffer to parse messages in parse_client_msg

master
Con Kolivas 9 years ago
parent
commit
eedc3eec1c
  1. 18
      src/connector.c

18
src/connector.c

@ -462,8 +462,8 @@ static void parse_client_msg(cdata_t *cdata, client_instance_t *client)
{ {
ckpool_t *ckp = cdata->ckp; ckpool_t *ckp = cdata->ckp;
int buflen, ret; int buflen, ret;
char *msg, *eol;
json_t *val; json_t *val;
char *eol;
retry: retry:
if (unlikely(client->bufofs > MAX_MSGSIZE)) { if (unlikely(client->bufofs > MAX_MSGSIZE)) {
@ -499,16 +499,10 @@ reparse:
return; return;
} }
msg = alloca(round_up_page(buflen + 1)); if (!(val = json_loads(client->buf, JSON_DISABLE_EOF_CHECK, NULL))) {
memcpy(msg, client->buf, buflen);
msg[buflen] = '\0';
client->bufofs -= buflen;
memmove(client->buf, client->buf + buflen, client->bufofs);
client->buf[client->bufofs] = '\0';
if (!(val = json_loads(msg, 0, NULL))) {
char *buf = strdup("Invalid JSON, disconnecting\n"); char *buf = strdup("Invalid JSON, disconnecting\n");
LOGINFO("Client id %"PRId64" sent invalid json message %s", client->id, msg); LOGINFO("Client id %"PRId64" sent invalid json message %s", client->id, client->buf);
send_client(cdata, client->id, buf); send_client(cdata, client->id, buf);
invalidate_client(ckp, cdata, client); invalidate_client(ckp, cdata, client);
return; return;
@ -522,7 +516,7 @@ reparse:
passthrough_id = (client->id << 32) | passthrough_id; passthrough_id = (client->id << 32) | passthrough_id;
json_object_set_new_nocheck(val, "client_id", json_integer(passthrough_id)); json_object_set_new_nocheck(val, "client_id", json_integer(passthrough_id));
} else { } else {
if (ckp->redirector && !client->redirected && strstr(msg, "mining.submit")) if (ckp->redirector && !client->redirected && strstr(client->buf, "mining.submit"))
parse_redirector_share(client, val); parse_redirector_share(client, val);
json_object_set_new_nocheck(val, "client_id", json_integer(client->id)); json_object_set_new_nocheck(val, "client_id", json_integer(client->id));
json_object_set_new_nocheck(val, "address", json_string(client->address_name)); json_object_set_new_nocheck(val, "address", json_string(client->address_name));
@ -543,6 +537,10 @@ reparse:
free(s); free(s);
json_decref(val); json_decref(val);
} }
client->bufofs -= buflen;
if (client->bufofs)
memmove(client->buf, client->buf + buflen, client->bufofs);
client->buf[client->bufofs] = '\0';
if (client->bufofs) if (client->bufofs)
goto reparse; goto reparse;

Loading…
Cancel
Save