From 7deee3240b09e88a26f82c6318ffce2b5c8627fe Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 6 Jun 2017 06:05:17 +0300 Subject: [PATCH] Handle jsonrpc errors. --- src/net/Client.cpp | 40 ++++++++++++++++++++++++++++++++++++---- src/net/Client.h | 5 +++++ 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/net/Client.cpp b/src/net/Client.cpp index cb77ebadd..8b80cd25a 100644 --- a/src/net/Client.cpp +++ b/src/net/Client.cpp @@ -22,9 +22,6 @@ */ -#include - - #include "Console.h" #include "interfaces/IClientListener.h" #include "net/Client.h" @@ -34,6 +31,8 @@ Client::Client(IClientListener *listener) : m_host(nullptr), m_listener(listener), + m_retries(0), + m_sequence(1), m_recvBufPos(0), m_state(UnconnectedState), m_port(0), @@ -96,9 +95,11 @@ void Client::disconnect() void Client::login(const char *user, const char *pass, const char *agent) { + m_sequence = 1; + const size_t size = 96 + strlen(user) + strlen(pass) + strlen(agent); char *req = static_cast(malloc(size)); - snprintf(req, size, "{\"method\":\"login\",\"params\":{\"login\":\"%s\",\"pass\":\"%s\",\"agent\":\"%s\"},\"id\":1}\n", user, pass, agent); + snprintf(req, size, "{\"id\":%llu,\"jsonrpc\":\"2.0\",\"method\":\"login\",\"params\":{\"login\":\"%s\",\"pass\":\"%s\",\"agent\":\"%s\"}}\n", m_sequence, user, pass, agent); send(req); } @@ -117,6 +118,7 @@ void Client::send(char *data) return; } + m_sequence++; uv_buf_t buf = uv_buf_init(data, strlen(data)); uv_write_t *req = static_cast(malloc(sizeof(uv_write_t))); @@ -195,6 +197,36 @@ void Client::parse(char *line, size_t len) LOG_ERR("[%s:%u] JSON decode failed: \"%s\"", m_host, m_port, err.text); return; } + + const json_t *id = json_object_get(val, "id"); + if (json_is_integer(id)) { + parseResponse(json_integer_value(id), json_object_get(val, "result"), json_object_get(val, "error")); + } + else { + parseNotification(json_string_value(json_object_get(val, "method")), json_object_get(val, "params")); + } + + json_decref(val); +} + + +void Client::parseNotification(const char *method, const json_t *params) +{ + +} + + +void Client::parseResponse(int64_t id, const json_t *result, const json_t *error) +{ + if (json_is_object(error)) { + LOG_ERR("[%s:%u] error: \"%s\", code: %lld", m_host, m_port, json_string_value(json_object_get(error, "message")), json_integer_value(json_object_get(error, "code"))); + + if (id == 1) { + close(); + } + + return; + } } diff --git a/src/net/Client.h b/src/net/Client.h index 181ea6419..89167affd 100644 --- a/src/net/Client.h +++ b/src/net/Client.h @@ -25,6 +25,7 @@ #define __CLIENT_H__ +#include #include @@ -61,6 +62,8 @@ private: void close(); void connect(struct sockaddr *addr); void parse(char *line, size_t len); + void parseNotification(const char *method, const json_t *params); + void parseResponse(int64_t id, const json_t *result, const json_t *error); void setState(SocketState state); static void onAllocBuffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf); @@ -73,6 +76,8 @@ private: char *m_host; IClientListener *m_listener; + int64_t m_retries; + int64_t m_sequence; size_t m_recvBufPos; SocketState m_state; struct addrinfo m_hints;