我使用的violetminer连接服务器,发现一个问题:经常我自己的哈希率看着正常的,服务端却显示当前hashrate为0;此时只能通过重启连接才能解决,
如果定时间隔重启效率比较低,于是我想到了定时去网页获取当前状态,比较速率,如果效率太低就重新连接:
步骤1)下载库文件,我使用vcpkg,官网没有找到合适的包,
vcpkg install curl
vcpkg install json-c
// 备注,需要在bin目录同时找到zlib1.dll,
2)测试代码:
#include <iostream>
#include "StatInfo.h"
#include <thread>
#include <chrono>
using namespace std;
int main()
{
StatInfo info;
double hashrate;
bool ret = false;
for (int i = 0; i < 1000; i++)
{
std::chrono::seconds secs = std::chrono::seconds(10);
std::this_thread::sleep_for(secs);
ret = info.GetHashRate(hashrate);
cout << "ok--> ";
std::cout << "当前速率:" << hashrate << std::endl;
}
//getchar();
return 0;
}
执行效果
3)功能类:
StatInfo.h
#pragma once
#include <curl/curl.h>
#include <json-c/json.h>
//不带SSL
//带SSL
//#pragma comment(lib, "libcurl_imp.lib")
#include <string>
#include <iostream>
#include <assert.h>
#pragma comment(lib, "libcurl.lib")
#pragma comment(lib, "json-c.lib")
/*
CURLE_OK 任务完成一切都好
CURLE_UNSUPPORTED_PROTOCOL 不支持的协议,由URL的头部指定
CURLE_COULDNT_CONNECT 不能连接到remote 主机或者代理
CURLE_REMOTE_ACCESS_DENIED 访问被拒绝
CURLE_HTTP_RETURNED_ERROR Http返回错误
CURLE_READ_ERROR 读本地文件错误
CURLE_SSL_CACERT 访问HTTPS时需要CA证书路径
*/
class StatInfo
{
public:
StatInfo();
virtual ~StatInfo();
void Init();
bool GetHashRate(double & rate);
protected:
CURL *curl = nullptr;
CURLcode code = CURLE_OK;
std::string szbuffer;
std::string szheader_buffer;
char errorBuffer[CURL_ERROR_SIZE];
std::string url = "https://trtl.mine2gether.com/api/stats_address?address=TRT...钱包地址";
//std::string url = "https://vip.icbc.com.cn/icbc/perbank/index.jsp";
std::string useragent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20100101 Firefox/13.0.1";
};
StatIno.cpp
#include "StatInfo.h"
#define SKIP_PEER_VERIFICATION 1
//#define SKIP_HOSTNAME_VERFICATION 1
/*
ptr是指向存储数据的指针,
size是每个块的大小,
nmemb是指块的数目,
stream是用户参数。
所以根据以上这些参数的信息可以知道,ptr中的数据的总长度是size*nmemb
*/
size_t call_wirte_func(const char *ptr, size_t size, size_t nmemb, std::string *stream)
{
assert(stream != NULL);
size_t len = size * nmemb;
stream->append(ptr, len);
return len;
}
// 返回http header回调函数
size_t header_callback(const char *ptr, size_t size, size_t nmemb, std::string *stream)
{
assert(stream != NULL);
size_t len = size * nmemb;
stream->append(ptr, len);
return len;
}
double parseJson(const std::string & szbuffer)
{
struct json_object *root_obj = nullptr;
int pageCount;
root_obj = json_tokener_parse(szbuffer.c_str());
struct json_object * stat = json_object_object_get(root_obj, "stats");
struct json_object * speed = json_object_object_get(stat, "hashrate");
const char * strSpeed = json_object_get_string(speed);
double ret = atof(strSpeed);
return ret;
}
StatInfo::StatInfo()
{
curl_global_init(CURL_GLOBAL_ALL);
}
StatInfo::~StatInfo()
{
if (curl != nullptr)
{
/* 释放内存 */
curl_easy_cleanup(curl);
curl = nullptr;
}
curl_global_cleanup();
}
void StatInfo::Init()
{
/*
CURL_GLOBAL_ALL //初始化所有的可能的调用。
CURL_GLOBAL_SSL //初始化支持 安全套接字层。
CURL_GLOBAL_WIN32 //初始化win32套接字库。
CURL_GLOBAL_NOTHING //没有额外的初始化。
*/
curl = curl_easy_init();
}
bool StatInfo::GetHashRate(double & rate)
{
if (curl == nullptr)
Init();
if (curl == nullptr)
return false;
// 远程URL,支持 http, https, ftp
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_USERAGENT, useragent.c_str());
// 官方下载的DLL并不支持GZIP,Accept-Encoding:deflate, gzip
curl_easy_setopt(curl, CURLOPT_ENCODING, "gzip, deflate");
//curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);//调试信息打开
//https 访问专用:start
#ifdef SKIP_PEER_VERIFICATION
//跳过服务器SSL验证,不使用CA证书
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
//如果不跳过SSL验证,则可指定一个CA证书目录
//curl_easy_setopt(curl, CURLOPT_CAPATH, "this is ca ceat");
#endif
#ifdef SKIP_HOSTNAME_VERFICATION
//验证服务器端发送的证书,默认是 2(高),1(中),0(禁用)
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
#endif
//https 访问专用:end
//发送cookie值给服务器
//curl_easy_setopt(curl, CURLOPT_COOKIE, "name1=var1; name2=var2;");
/* 与服务器通信交互cookie,默认在内存中,可以是不存在磁盘中的文件或留空 */
//curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "./cookie.txt");
/* 与多个CURL或浏览器交互cookie,会在释放内存后写入磁盘文件 */
//curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "./cookie.txt");
/* POST 数据 */
// curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "name=daniel&project=curl");
//设置重定向的最大次数
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 5);
//设置301、302跳转跟随location
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
//抓取内容后,回调函数
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, call_wirte_func);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &szbuffer);
//抓取头信息,回调函数
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, header_callback);
curl_easy_setopt(curl, CURLOPT_HEADERDATA, &szheader_buffer);
/*
CURLE_OK 任务完成一切都好
CURLE_UNSUPPORTED_PROTOCOL 不支持的协议,由URL的头部指定
CURLE_COULDNT_CONNECT 不能连接到remote 主机或者代理
CURLE_REMOTE_ACCESS_DENIED 访问被拒绝
CURLE_HTTP_RETURNED_ERROR Http返回错误
CURLE_READ_ERROR 读本地文件错误
CURLE_SSL_CACERT 访问HTTPS时需要CA证书路径
*/
szbuffer = "";
code = curl_easy_perform(curl);
if (CURLE_OK == code)
{
double val;
/* check for bytes downloaded */
/*code = curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD, &val);
if ((CURLE_OK == code) && (val > 0))
printf("Data downloaded: %0.0f bytes.\n", val);*/
/* check for total download time */
/*code = curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &val);
if ((CURLE_OK == code) && (val > 0))
printf("Total download time: %0.3f sec.\n", val);*/
/* check for average download speed */
/*code = curl_easy_getinfo(curl, CURLINFO_SPEED_DOWNLOAD, &val);
if ((CURLE_OK == code) && (val > 0))
printf("Average download speed: %0.3f kbyte/sec.\n", val / 1024);
printf("%s\n", szbuffer.c_str());*/
rate = parseJson(szbuffer);
//std::cout << "当前速率:" << rate << std::endl;
}
else {
printf("Failed to get '%s' [%s]\n", url.c_str(), errorBuffer);
return false;
}
return true;
}
解析的JSON类似:
{"stats":
{"hashes":"47996852063",
"lastShare":"1622856265",
"paymentId":"",
"balance":"83946",
"minPayment":"50000",
"blocks":"9",
"lastBlockFound":"1622775360100",
"hashrate":"18.35 KH"
}
}