forked from taganaka/SpeedTest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpeedTestClient.cpp
224 lines (193 loc) · 5.57 KB
/
SpeedTestClient.cpp
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//
// Created by Francesco Laurita on 5/30/16.
//
#include <arpa/inet.h>
#include <netdb.h>
#include "SpeedTestClient.h"
SpeedTestClient::SpeedTestClient(const ServerInfo &serverInfo):
mServerInfo(serverInfo),
mSocketFd(0),
mServerVersion(-1.0) {
}
SpeedTestClient::~SpeedTestClient() {
close();
}
// It connects and initiates client/server handshaking
bool SpeedTestClient::connect() {
if (mSocketFd)
return true;
auto ret = mkSocket();
if (!ret)
return ret;
if (!SpeedTestClient::writeLine(mSocketFd, "HI")) {
close();
return false;
}
std::string reply;
if (SpeedTestClient::readLine(mSocketFd, reply)) {
std::stringstream reply_stream(reply);
std::string hello;
reply_stream >> hello >> mServerVersion;
if (!reply_stream.fail() && "HELLO" == hello)
return true;
}
close();
return false;
}
// It closes a connection
void SpeedTestClient::close() {
if (mSocketFd) {
SpeedTestClient::writeLine(mSocketFd, "QUIT");
::close(mSocketFd);
}
}
// It executes PING command
bool SpeedTestClient::ping(long &millisec) {
millisec = LONG_MAX;
auto start = std::chrono::high_resolution_clock::now();
std::stringstream cmd;
cmd << "PING " << start.time_since_epoch().count() << "\n";
if (!SpeedTestClient::writeLine(mSocketFd, cmd.str()))
return false;
std::string reply;
//start = std::chrono::high_resolution_clock::now();
if (SpeedTestClient::readLine(mSocketFd, reply)) {
if (reply.substr(0, 5) == "PONG ") {
auto stop = std::chrono::high_resolution_clock::now();
millisec = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start).count();
return true;
}
}
return false;
}
// It executes DOWNLOAD command
bool SpeedTestClient::download(const long size, const long chunk_size, long &millisec) {
millisec = LONG_MAX;
std::stringstream cmd;
cmd << "DOWNLOAD " << size << "\n";
if (!SpeedTestClient::writeLine(mSocketFd, cmd.str()))
return false;
char *buff = new char[chunk_size];
for (size_t i = 0; i < static_cast<size_t>(chunk_size); i++)
buff[i] = '\0';
long missing = 0;
auto start = std::chrono::high_resolution_clock::now();
while (missing < size) {
auto current = read(mSocketFd, buff, static_cast<ssize_t>(chunk_size));
if (current < 1) {
delete[] buff;
return false;
}
missing += current;
}
auto stop = std::chrono::high_resolution_clock::now();
millisec = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start).count();
delete[] buff;
return missing == size;
}
// It executes UPLOAD command
bool SpeedTestClient::upload(const long size, const long chunk_size, long &millisec) {
millisec = LONG_MAX;
std::stringstream cmd;
cmd << "UPLOAD " << size << "\n";
if (!SpeedTestClient::writeLine(mSocketFd, cmd.str()))
return false;
char *buff = new char[chunk_size];
for (size_t i = 0; i < static_cast<size_t>(chunk_size); i++)
buff[i] = static_cast<char>(rand() % 256);
long missing = size - cmd.str().length();
ssize_t len;
auto start = std::chrono::high_resolution_clock::now();
while (missing > 0) {
if (missing - chunk_size > 0) {
len = static_cast<ssize_t>(chunk_size);
} else {
len = static_cast<ssize_t>(missing);
buff[missing - 1] = '\n';
}
auto n = write(mSocketFd, buff, len);
if (n != len) {
delete[] buff;
return false;
}
missing -= n;
}
auto stop = std::chrono::high_resolution_clock::now();
millisec = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start).count();
delete[] buff;
std::stringstream ss;
ss << "OK " << size << " ";
std::string reply;
if (!SpeedTestClient::readLine(mSocketFd, reply))
return false;
return reply.substr(0, ss.str().length()) == ss.str();
}
bool SpeedTestClient::mkSocket() {
mSocketFd = socket(AF_INET, SOCK_STREAM, 0);
if (!mSocketFd)
return false;
auto hostp = hostport();
#if __APPLE__
struct hostent *server = gethostbyname(hostp.first.c_str());
if (server == nullptr)
return false;
#else
struct hostent server;
char tmpbuf[BUFSIZ];
struct hostent *result;
int errnop;
if (gethostbyname_r(hostp.first.c_str(), &server, (char *)&tmpbuf, BUFSIZ, &result, &errnop))
return false;
#endif
int portno = hostp.second;
struct sockaddr_in serv_addr{};
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
#if __APPLE__
memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, (size_t)server->h_length);
#else
memcpy(&serv_addr.sin_addr.s_addr, server.h_addr, (size_t)server.h_length);
#endif
serv_addr.sin_port = htons(static_cast<uint16_t>(portno));
/* Dial */
return ::connect(mSocketFd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) >= 0;
}
float SpeedTestClient::version() {
return mServerVersion;
}
const std::pair<std::string, int> SpeedTestClient::hostport() {
std::string targetHost = mServerInfo.host;
std::size_t found = targetHost.find(':');
std::string host = targetHost.substr(0, found);
std::string port = targetHost.substr(found + 1, targetHost.length() - found);
return std::pair<std::string, int>(host, std::atoi(port.c_str()));
}
bool SpeedTestClient::readLine(int &fd, std::string &buffer) {
if (!fd)
return false;
buffer.clear();
char c;
while (true) {
auto n = read(fd, &c, 1);
if (n < 1)
return false;
if (c == '\n' || c == '\r')
break;
buffer += c;
}
return buffer.length() > 0;
}
bool SpeedTestClient::writeLine(int &fd, const std::string &buffer) {
if (!fd)
return false;
auto len = static_cast<ssize_t>(buffer.length());
if (len == 0)
return false;
std::string buff_copy = buffer;
if (buff_copy.find_first_of('\n') == std::string::npos) {
buff_copy += '\n';
len += 1;
}
auto n = write(fd, buff_copy.c_str(), len);
return n == len;
}