Skip to content

Commit

Permalink
simulator: add DNS resolver for ss_ip
Browse files Browse the repository at this point in the history
  • Loading branch information
SwimmingTiger committed Apr 15, 2019
1 parent cc52f8d commit 4c121d0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
29 changes: 18 additions & 11 deletions src/StratumClient.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ StratumClient::StratumClient(
, isMining_(false) {
inBuf_ = evbuffer_new();

evdnsBase_ = evdns_base_new(base, 1);
if (evdnsBase_ == nullptr) {
LOG(FATAL) << "DNS init failed";
}

if (enableTLS_) {
LOG(INFO) << "<" << workerFullName_ << "> TLS enabled";

Expand Down Expand Up @@ -95,16 +100,20 @@ StratumClient::StratumClient(
StratumClient::~StratumClient() {
evbuffer_free(inBuf_);
bufferevent_free(bev_);
evdns_base_free(evdnsBase_, 0);
}

bool StratumClient::connect(struct sockaddr_in &sin) {
// bufferevent_socket_connect(): This function returns 0 if the connect
// was successfully launched, and -1 if an error occurred.
int res =
bufferevent_socket_connect(bev_, (struct sockaddr *)&sin, sizeof(sin));
bool StratumClient::connect(const string &host, uint16_t port) {
LOG(INFO) << "Connection request to " << host << ":" << port;

// bufferevent_socket_connect_hostname(): This function returns 0 if the
// connect was successfully launched, and -1 if an error occurred.
int res = bufferevent_socket_connect_hostname(
bev_, evdnsBase_, AF_INET, host.c_str(), (int)port);
if (res == 0) {
return true;
}

return false;
}

Expand Down Expand Up @@ -253,7 +262,7 @@ void StratumClient::sendData(const char *data, size_t len) {
////////////////////////////// StratumClientWrapper ////////////////////////////
StratumClientWrapper::StratumClientWrapper(
bool enableTLS,
const char *host,
const string &host,
const uint32_t port,
const uint32_t numConnections,
const string &userName,
Expand All @@ -263,17 +272,15 @@ StratumClientWrapper::StratumClientWrapper(
const libconfig::Config &config)
: running_(true)
, enableTLS_(enableTLS)
, host_(host)
, port_(port)
, base_(event_base_new())
, numConnections_(numConnections)
, userName_(userName)
, minerNamePrefix_(minerNamePrefix)
, passwd_(passwd)
, type_(type)
, config_(config) {
memset(&sin_, 0, sizeof(sin_));
sin_.sin_family = AF_INET;
inet_pton(AF_INET, host, &(sin_.sin_addr));
sin_.sin_port = htons(port);

if (minerNamePrefix_.empty())
minerNamePrefix_ = "simulator";
Expand Down Expand Up @@ -345,7 +352,7 @@ void StratumClientWrapper::run() {
Strings::Format("%s.%s-%05d", userName_, minerNamePrefix_, i);
auto client = createClient(enableTLS_, base_, workerFullName, passwd_);

if (!client->connect(sin_)) {
if (!client->connect(host_, port_)) {
LOG(ERROR) << "client connnect failure: " << workerFullName;
return;
}
Expand Down
9 changes: 6 additions & 3 deletions src/StratumClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <event2/event.h>
#include <event2/buffer.h>
#include <event2/bufferevent.h>
#include <event2/dns.h>

#include <glog/logging.h>
#include <arith_uint256.h>
Expand All @@ -52,6 +53,7 @@ class StratumClient {
bool enableTLS_;
struct bufferevent *bev_;
struct evbuffer *inBuf_;
struct evdns_base *evdnsBase_;

uint32_t sessionId_; // session ID
int32_t extraNonce2Size_;
Expand Down Expand Up @@ -118,7 +120,7 @@ class StratumClient {
const string &workerPasswd);
virtual ~StratumClient();

bool connect(struct sockaddr_in &sin);
bool connect(const string &host, uint16_t port);
virtual void sendHelloData();

void sendData(const char *data, size_t len);
Expand All @@ -133,8 +135,9 @@ class StratumClient {
class StratumClientWrapper {
bool running_;
bool enableTLS_;
string host_;
uint16_t port_;
struct event_base *base_;
struct sockaddr_in sin_;
struct event *timer_;
struct event *sigterm_;
struct event *sigint_;
Expand All @@ -151,7 +154,7 @@ class StratumClientWrapper {
public:
StratumClientWrapper(
bool enableTLS,
const char *host,
const string &host,
const uint32_t port,
const uint32_t numConnections,
const string &userName,
Expand Down
2 changes: 1 addition & 1 deletion src/simulator/simulator.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ simulator = {
number_clients = 1;

# stratum sever host & port
ss_ip = "127.0.0.1";
ss_ip = "localhost";
ss_port = 3333;
enable_tls = false;

Expand Down

0 comments on commit 4c121d0

Please sign in to comment.