|
|
@ -1,14 +1,17 @@ |
|
|
|
#include "EthStratumClient.h" |
|
|
|
|
|
|
|
using boost::asio::ip::tcp; |
|
|
|
|
|
|
|
EthStratumClient::EthStratumClient(string const & host, string const & port, string const & user, string const & pass) : m_socket(m_io_service) |
|
|
|
EthStratumClient::EthStratumClient(string const & host, string const & port, string const & user, string const & pass) |
|
|
|
: m_socket(m_io_service) |
|
|
|
{ |
|
|
|
m_host = host; |
|
|
|
m_port = port; |
|
|
|
m_user = user; |
|
|
|
m_pass = pass; |
|
|
|
|
|
|
|
|
|
|
|
connect(); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
@ -19,14 +22,16 @@ EthStratumClient::~EthStratumClient() |
|
|
|
|
|
|
|
void EthStratumClient::connect() |
|
|
|
{ |
|
|
|
|
|
|
|
tcp::resolver r(m_io_service); |
|
|
|
tcp::resolver::query q(m_host, m_port); |
|
|
|
|
|
|
|
|
|
|
|
r.async_resolve(q, boost::bind(&EthStratumClient::resolve_handler, |
|
|
|
this, boost::asio::placeholders::error, |
|
|
|
boost::asio::placeholders::iterator)); |
|
|
|
|
|
|
|
m_io_service.run(); |
|
|
|
boost::thread t(boost::bind(&boost::asio::io_service::run, &m_io_service)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
void EthStratumClient::resolve_handler(const boost::system::error_code& ec, tcp::resolver::iterator i) |
|
|
@ -52,8 +57,9 @@ void EthStratumClient::connect_handler(const boost::system::error_code& ec, tcp: |
|
|
|
std::ostream os(&m_requestBuffer); |
|
|
|
os << "{\"id\": 1, \"method\": \"mining.subscribe\", \"params\": []}\n"; |
|
|
|
|
|
|
|
|
|
|
|
async_write(m_socket, m_requestBuffer, |
|
|
|
boost::bind(&EthStratumClient::subscribe_handler, this, |
|
|
|
boost::bind(&EthStratumClient::handleResponse, this, |
|
|
|
boost::asio::placeholders::error)); |
|
|
|
} |
|
|
|
else |
|
|
@ -63,6 +69,77 @@ void EthStratumClient::connect_handler(const boost::system::error_code& ec, tcp: |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
void EthStratumClient::handleResponse(const boost::system::error_code& ec) { |
|
|
|
if (!ec) |
|
|
|
{ |
|
|
|
async_read_until(m_socket, m_responseBuffer, "\n", |
|
|
|
boost::bind(&EthStratumClient::readResponse, this, |
|
|
|
boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); |
|
|
|
|
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
cerr << "Handle response failed: " << ec.message(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void EthStratumClient::readResponse(const boost::system::error_code& ec, std::size_t bytes_transferred) |
|
|
|
{ |
|
|
|
if (!ec) |
|
|
|
{ |
|
|
|
std::istream is(&m_responseBuffer); |
|
|
|
std::string response; |
|
|
|
getline(is, response); |
|
|
|
cnote << response; |
|
|
|
|
|
|
|
Json::Value responseObject; |
|
|
|
Json::Reader reader; |
|
|
|
if (reader.parse(response.c_str(), responseObject)) |
|
|
|
{ |
|
|
|
int error = responseObject.get("error", NULL).asInt(); |
|
|
|
if (error != NULL) |
|
|
|
{ |
|
|
|
cerr << "Error"; |
|
|
|
} |
|
|
|
std::ostream os(&m_requestBuffer); |
|
|
|
int id = responseObject.get("id", NULL).asInt(); |
|
|
|
switch (id) |
|
|
|
{ |
|
|
|
case 1: |
|
|
|
cnote << "Subscribed"; |
|
|
|
|
|
|
|
os << "{\"id\": 2, \"method\": \"mining.authorize\", \"params\": [\"" << m_user << "\",\"" << m_pass << "\"]}\n"; |
|
|
|
|
|
|
|
//async_write(m_socket, m_requestBuffer,
|
|
|
|
// boost::bind(&EthStratumClient::handleResponse, this,
|
|
|
|
// boost::asio::placeholders::error));
|
|
|
|
break; |
|
|
|
case 2: |
|
|
|
cnote << "Authorized"; |
|
|
|
|
|
|
|
break; |
|
|
|
default: |
|
|
|
string method = responseObject.get("method", "").asString(); |
|
|
|
cnote << method; |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
async_read_until(m_socket, m_responseBuffer, "\n", |
|
|
|
boost::bind(&EthStratumClient::readResponse, this, |
|
|
|
boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); |
|
|
|
|
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
cerr << "Parse response failed: " << reader.getFormattedErrorMessages(); |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
cerr << "Read response failed: " << ec.message(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void EthStratumClient::subscribe_handler(const boost::system::error_code& ec) { |
|
|
|
if (!ec) |
|
|
|
{ |
|
|
@ -79,7 +156,7 @@ void EthStratumClient::subscribe_handler(const boost::system::error_code& ec) { |
|
|
|
|
|
|
|
void EthStratumClient::read_subscribe_handler(const boost::system::error_code& ec, std::size_t bytes_transferred) |
|
|
|
{ |
|
|
|
if (!ec || ec == error::eof) |
|
|
|
if (!ec) |
|
|
|
{ |
|
|
|
std::istream is(&m_responseBuffer); |
|
|
|
std::string response; |
|
|
@ -89,10 +166,13 @@ void EthStratumClient::read_subscribe_handler(const boost::system::error_code& e |
|
|
|
std::ostream os(&m_requestBuffer); |
|
|
|
os << "{\"id\": 2, \"method\": \"mining.authorize\", \"params\": [\"" << m_user << "\",\"" << m_pass << "\"]}\n"; |
|
|
|
|
|
|
|
//async_write(m_socket, m_requestBuffer,
|
|
|
|
// boost::bind(&EthStratumClient::authorize_handler, this,
|
|
|
|
// boost::asio::placeholders::error));
|
|
|
|
|
|
|
|
async_write(m_socket, m_requestBuffer, |
|
|
|
boost::bind(&EthStratumClient::authorize_handler, this, |
|
|
|
boost::asio::placeholders::error)); |
|
|
|
async_read_until(m_socket, m_responseBuffer, "\n", |
|
|
|
boost::bind(&EthStratumClient::read_work_handler, this, |
|
|
|
boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|