Browse Source

grr

cl-refactor
Jan Willem Penterman 9 years ago
parent
commit
11c4bf384d
  1. 3
      ethminer/MinerAux.h
  2. 14
      libstratum/CMakeLists.txt
  3. 98
      libstratum/EthStratumClient.cpp
  4. 10
      libstratum/EthStratumClient.h

3
ethminer/MinerAux.h

@ -883,8 +883,9 @@ private:
sealers["cuda"] = GenericFarm<EthashProofOfWork>::SealerDescriptor{ &EthashCUDAMiner::instances, [](GenericMiner<EthashProofOfWork>::ConstructionInfo ci){ return new EthashCUDAMiner(ci); } };
#endif
EthStratumClient client(host, port, user, pass);
client.connect();
//client.connect();
while (true)
this_thread::sleep_for(chrono::milliseconds(1000));

14
libstratum/CMakeLists.txt

@ -0,0 +1,14 @@
set(EXECUTABLE ethstratum)
aux_source_directory(. SRC_LIST)
include_directories(..)
include_directories(${Boost_INCLUDE_DIRS})
file(GLOB HEADERS "*.h")
add_library(${EXECUTABLE} ${SRC_LIST} ${HEADERS})
target_link_libraries(${EXECUTABLE} ${Boost_SYSTEM_LIBRARY} ${Boost_THREAD_LIBRARY} ${Boost_REGEX_LIBRARY})
install( TARGETS ${EXECUTABLE} RUNTIME DESTINATION bin ARCHIVE DESTINATION lib LIBRARY DESTINATION lib )
install( FILES ${HEADERS} DESTINATION include/${EXECUTABLE} )

98
libstratum/EthStratumClient.cpp

@ -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
{

10
libstratum/EthStratumClient.h

@ -15,12 +15,17 @@ public:
EthStratumClient(string const & host, string const & port, string const & user, string const & pass);
~EthStratumClient();
void connect();
boost::asio::io_service m_io_service;
private:
void connect();
void resolve_handler(const boost::system::error_code& ec, tcp::resolver::iterator i);
void connect_handler(const boost::system::error_code& ec, tcp::resolver::iterator i);
void handleResponse(const boost::system::error_code& ec);
void readResponse(const boost::system::error_code& ec, std::size_t bytes_transferred);
void subscribe_handler(const boost::system::error_code& ec);
void authorize_handler(const boost::system::error_code& ec);
void read_subscribe_handler(const boost::system::error_code& ec, std::size_t bytes_transferred);
@ -32,8 +37,9 @@ private:
string m_user;
string m_pass;
io_service m_io_service;
tcp::socket m_socket;
boost::asio::streambuf m_requestBuffer;
boost::asio::streambuf m_responseBuffer;
};
Loading…
Cancel
Save