2021-08-22 04:20:59 -06:00
|
|
|
/*
|
|
|
|
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
2022-03-30 06:42:26 -06:00
|
|
|
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
2021-08-22 04:20:59 -06: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, version 3.
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
namespace p2pool {
|
|
|
|
|
|
|
|
class JSONRPCRequest
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
template<typename T>
|
|
|
|
static FORCEINLINE void call(const char* address, int port, const char* req, T&& cb)
|
|
|
|
{
|
|
|
|
// It will be deleted in one of the tcp callbacks eventually
|
2021-11-20 03:51:22 -07:00
|
|
|
JSONRPCRequest* r = new JSONRPCRequest(address, port, req, new Callback<T>(std::move(cb)), nullptr, nullptr);
|
2021-09-10 08:18:16 -06:00
|
|
|
if (!r->m_valid) {
|
|
|
|
delete r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename U>
|
2021-11-20 03:51:22 -07:00
|
|
|
static FORCEINLINE void call(const char* address, int port, const char* req, T&& cb, U&& close_cb, uv_loop_t* loop = nullptr)
|
2021-09-10 08:18:16 -06:00
|
|
|
{
|
|
|
|
// It will be deleted in one of the tcp callbacks eventually
|
2021-11-10 08:26:01 -07:00
|
|
|
CallbackBase* close_callback = new Callback<U>(std::move(close_cb));
|
2021-11-20 03:51:22 -07:00
|
|
|
JSONRPCRequest* r = new JSONRPCRequest(address, port, req, new Callback<T>(std::move(cb)), close_callback, loop);
|
2021-09-10 08:18:16 -06:00
|
|
|
if (!r->m_valid) {
|
|
|
|
constexpr char err[] = "internal error";
|
2021-11-10 08:26:01 -07:00
|
|
|
(*close_callback)(err, sizeof(err) - 1);
|
2021-09-10 08:18:16 -06:00
|
|
|
delete r;
|
|
|
|
}
|
2021-08-22 04:20:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct CallbackBase
|
|
|
|
{
|
|
|
|
virtual ~CallbackBase() {}
|
|
|
|
virtual void operator()(const char* data, size_t size) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct Callback : public CallbackBase
|
|
|
|
{
|
|
|
|
explicit FORCEINLINE Callback(T&& cb) : m_cb(std::move(cb)) {}
|
|
|
|
void operator()(const char* data, size_t size) override { m_cb(data, size); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
Callback& operator=(Callback&&) = delete;
|
|
|
|
T m_cb;
|
|
|
|
};
|
|
|
|
|
2021-11-20 03:51:22 -07:00
|
|
|
JSONRPCRequest(const char* address, int port, const char* req, CallbackBase* cb, CallbackBase* close_cb, uv_loop_t* loop);
|
2021-08-22 04:20:59 -06:00
|
|
|
~JSONRPCRequest();
|
|
|
|
|
|
|
|
static void on_connect(uv_connect_t* req, int status);
|
|
|
|
static void on_write(uv_write_t* handle, int status);
|
|
|
|
static void on_alloc(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf);
|
|
|
|
static void on_read(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf);
|
|
|
|
void on_read(const char* data, size_t size);
|
2022-05-26 10:20:29 -06:00
|
|
|
// cppcheck-suppress functionConst
|
2021-08-22 04:20:59 -06:00
|
|
|
void close();
|
|
|
|
static void on_close(uv_handle_t* handle);
|
|
|
|
|
|
|
|
uv_tcp_t m_socket;
|
|
|
|
uv_connect_t m_connect;
|
|
|
|
uv_write_t m_write;
|
|
|
|
|
|
|
|
CallbackBase* m_callback;
|
2021-09-10 08:18:16 -06:00
|
|
|
CallbackBase* m_closeCallback;
|
2021-08-22 04:20:59 -06:00
|
|
|
uint32_t m_contentLength;
|
|
|
|
bool m_contentLengthHeader;
|
|
|
|
|
|
|
|
std::vector<char> m_request;
|
|
|
|
std::string m_response;
|
|
|
|
char m_readBuf[65536];
|
|
|
|
bool m_readBufInUse;
|
2021-09-10 08:18:16 -06:00
|
|
|
bool m_valid;
|
|
|
|
std::string m_error;
|
2021-08-22 04:20:59 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace p2pool
|