/* * This file is part of the Monero P2Pool * Copyright (c) 2021-2022 SChernykh * * 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 . */ #pragma once namespace p2pool { class JSONRPCRequest { public: template 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 JSONRPCRequest* r = new JSONRPCRequest(address, port, req, new Callback(std::move(cb)), nullptr, nullptr); if (!r->m_valid) { delete r; } } template static FORCEINLINE void call(const char* address, int port, const char* req, T&& cb, U&& close_cb, uv_loop_t* loop = nullptr) { // It will be deleted in one of the tcp callbacks eventually CallbackBase* close_callback = new Callback(std::move(close_cb)); JSONRPCRequest* r = new JSONRPCRequest(address, port, req, new Callback(std::move(cb)), close_callback, loop); if (!r->m_valid) { constexpr char err[] = "internal error"; (*close_callback)(err, sizeof(err) - 1); delete r; } } private: struct CallbackBase { virtual ~CallbackBase() {} virtual void operator()(const char* data, size_t size) = 0; }; template 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; }; JSONRPCRequest(const char* address, int port, const char* req, CallbackBase* cb, CallbackBase* close_cb, uv_loop_t* loop); ~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); // cppcheck-suppress functionConst 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; CallbackBase* m_closeCallback; uint32_t m_contentLength; bool m_contentLengthHeader; std::vector m_request; std::string m_response; char m_readBuf[65536]; bool m_readBufInUse; bool m_valid; std::string m_error; }; } // namespace p2pool