forked from taganaka/SpeedTest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpeedTest.h
61 lines (58 loc) · 2.5 KB
/
SpeedTest.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//
// Created by Francesco Laurita on 5/29/16.
//
#ifndef SPEEDTEST_SPEEDTEST_H
#define SPEEDTEST_SPEEDTEST_H
#include "SpeedTestConfig.h"
#include "SpeedTestClient.h"
#include <libxml/xmlreader.h>
#include <functional>
#include <cmath>
#include <curl/curl.h>
#include <fstream>
#include <sstream>
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <thread>
#include <mutex>
#include "DataTypes.h"
class SpeedTestClient;
typedef bool (SpeedTestClient::*opFn)(const long size, const long chunk_size, long &millisec);
typedef void (*progressFn)(bool success);
class SpeedTest {
public:
explicit SpeedTest(float minServerVersion);
~SpeedTest();
CURLcode httpRequest(const std::string &url, const std::string &postdata, std::stringstream &ss, CURL *handler = nullptr, long timeout = 30);
static std::map<std::string, std::string> parseQueryString(const std::string &query);
static std::vector<std::string> splitString(const std::string &instr, const char separator);
bool ipInfo(IPInfo &info);
const std::vector<ServerInfo> &serverList();
const ServerInfo bestServer(const int sample_size = 5, std::function<void(bool)> cb = nullptr);
bool setServer(ServerInfo &server);
const long &latency();
bool downloadSpeed(const ServerInfo &server, const TestConfig &config, double &result, std::function<void(bool)> cb = nullptr);
bool uploadSpeed(const ServerInfo &server, const TestConfig &config, double &result, std::function<void(bool)> cb = nullptr);
bool jitter(const ServerInfo &server, long &result, const int sample = 40);
bool share(const ServerInfo &server, std::string &image_url);
private:
bool fetchServers(const std::string &url, std::vector<ServerInfo> &target, int &http_code);
bool testLatency(SpeedTestClient &client, int sample_size, long &latency);
const ServerInfo findBestServerWithin(const std::vector<ServerInfo> &serverList, long &latency, const int sample_size = 5, std::function<void(bool)> cb = nullptr);
static size_t writeFunc(void *buf, size_t size, size_t nmemb, void *userp);
static ServerInfo processServerXMLNode(xmlTextReaderPtr reader);
double execute(const ServerInfo &server, const TestConfig &config, const opFn &pfunc, std::function<void(bool)> cb = nullptr);
template <typename T>
static T deg2rad(T n);
template <typename T>
static T harversine(std::pair<T, T> n1, std::pair<T, T> n2);
IPInfo mIpInfo;
std::vector<ServerInfo> mServerList;
float mMinSupportedServer;
long mLatency;
double mUploadSpeed;
double mDownloadSpeed;
};
#endif // SPEEDTEST_SPEEDTEST_H