Files
xmrig/src/common/net/Client.h

144 lines
4.7 KiB
C
Raw Normal View History

2017-06-04 20:52:21 +03:00
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2016-2018 XMRig <https://github.com/xmrig>, <support@xmrig.com>
2017-06-04 20:52:21 +03:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __CLIENT_H__
#define __CLIENT_H__
#include <map>
2017-06-04 20:52:21 +03:00
#include <uv.h>
2018-03-07 16:38:58 +07:00
#include <vector>
2017-06-04 20:52:21 +03:00
#include "common/net/Id.h"
2018-04-20 23:44:32 +07:00
#include "common/net/Job.h"
#include "common/net/Pool.h"
#include "common/net/Storage.h"
#include "common/net/SubmitResult.h"
#include "rapidjson/fwd.h"
2017-06-07 01:19:59 +03:00
2017-06-04 20:52:21 +03:00
class IClientListener;
2017-06-11 15:32:15 +03:00
class JobResult;
2017-06-04 20:52:21 +03:00
class Client
{
public:
enum SocketState {
UnconnectedState,
HostLookupState,
ConnectingState,
ConnectedState,
ClosingState
};
constexpr static int kResponseTimeout = 20 * 1000;
2017-06-07 06:48:00 +03:00
Client(int id, const char *agent, IClientListener *listener);
2018-03-31 18:12:52 +07:00
~Client();
2017-06-04 20:52:21 +03:00
2018-03-07 16:38:58 +07:00
bool disconnect();
int64_t submit(const JobResult &result);
2017-06-07 02:46:52 +03:00
void connect();
2018-04-11 06:09:34 +07:00
void connect(const Pool &pool);
2018-03-24 00:10:39 +07:00
void deleteLater();
2018-04-11 08:29:02 +07:00
void setPool(const Pool &pool);
void tick(uint64_t now);
2017-06-10 13:32:27 +03:00
inline bool isReady() const { return m_state == ConnectedState && m_failures == 0; }
2018-04-11 06:09:34 +07:00
inline const char *host() const { return m_pool.host(); }
2017-07-02 01:36:33 +03:00
inline const char *ip() const { return m_ip; }
2017-06-10 13:32:27 +03:00
inline const Job &job() const { return m_job; }
2017-06-07 06:48:00 +03:00
inline int id() const { return m_id; }
inline SocketState state() const { return m_state; }
2018-04-11 06:09:34 +07:00
inline uint16_t port() const { return m_pool.port(); }
2017-06-30 03:20:50 +03:00
inline void setQuiet(bool quiet) { m_quiet = quiet; }
inline void setRetries(int retries) { m_retries = retries; }
2017-06-07 06:48:00 +03:00
inline void setRetryPause(int ms) { m_retryPause = ms; }
2017-06-04 20:52:21 +03:00
private:
2018-03-07 16:38:58 +07:00
bool close();
bool isCriticalError(const char *message);
bool parseJob(const rapidjson::Value &params, int *code);
bool parseLogin(const rapidjson::Value &result, int *code);
2017-06-04 20:52:21 +03:00
int resolve(const char *host);
int64_t send(size_t size);
2018-03-07 16:38:58 +07:00
void connect(const std::vector<addrinfo*> &ipv4, const std::vector<addrinfo*> &ipv6);
void connect(sockaddr *addr);
void login();
2018-03-26 11:47:01 +07:00
void onClose();
2017-06-04 20:52:21 +03:00
void parse(char *line, size_t len);
void parseExtensions(const rapidjson::Value &value);
void parseNotification(const char *method, const rapidjson::Value &params, const rapidjson::Value &error);
void parseResponse(int64_t id, const rapidjson::Value &result, const rapidjson::Value &error);
2017-06-07 06:48:00 +03:00
void ping();
2017-06-07 04:19:32 +03:00
void reconnect();
2017-06-04 20:52:21 +03:00
void setState(SocketState state);
2017-06-07 06:48:00 +03:00
void startTimeout();
2017-06-04 20:52:21 +03:00
inline bool isQuiet() const { return m_quiet || m_failures >= m_retries; }
2017-06-04 20:52:21 +03:00
static void onAllocBuffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf);
static void onClose(uv_handle_t *handle);
static void onConnect(uv_connect_t *req, int status);
static void onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf);
static void onResolved(uv_getaddrinfo_t *req, int status, struct addrinfo *res);
2018-03-31 18:12:52 +07:00
static inline Client *getClient(void *data) { return m_storage.get(data); }
2017-06-04 20:52:21 +03:00
addrinfo m_hints;
2018-03-07 16:38:58 +07:00
bool m_ipv6;
bool m_nicehash;
2017-06-30 03:20:50 +03:00
bool m_quiet;
char m_buf[2048];
2018-03-07 16:38:58 +07:00
char m_ip[46];
char m_sendBuf[768];
const char *m_agent;
2017-06-04 20:52:21 +03:00
IClientListener *m_listener;
2017-06-07 02:46:52 +03:00
int m_id;
int m_retries;
2017-06-07 04:19:32 +03:00
int m_retryPause;
int64_t m_failures;
2017-06-07 01:19:59 +03:00
Job m_job;
2018-04-11 06:09:34 +07:00
Pool m_pool;
2017-06-04 20:52:21 +03:00
size_t m_recvBufPos;
SocketState m_state;
std::map<int64_t, SubmitResult> m_results;
uint64_t m_expire;
2018-03-17 16:30:41 +07:00
uint64_t m_jobs;
2018-03-31 18:12:52 +07:00
uint64_t m_keepAlive;
uintptr_t m_key;
2017-06-04 20:52:21 +03:00
uv_buf_t m_recvBuf;
uv_getaddrinfo_t m_resolver;
uv_stream_t *m_stream;
uv_tcp_t *m_socket;
2018-03-07 16:38:58 +07:00
xmrig::Id m_rpcId;
2018-03-31 18:12:52 +07:00
static int64_t m_sequence;
static xmrig::Storage<Client> m_storage;
2017-06-04 20:52:21 +03:00
};
#endif /* __CLIENT_H__ */