-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.cpp
More file actions
71 lines (56 loc) · 2.66 KB
/
functions.cpp
File metadata and controls
71 lines (56 loc) · 2.66 KB
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
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <fstream>
#include <chrono>
#include <thread>
#include <atomic>
#include <sys/socket.h>
#include "echelonheaders.h"
std::atomic<uint64_t> bytesCounter{0};
std::atomic<uint64_t> speedBps{0};
std::atomic<bool> running{true};
void BytesPerSecond(std::atomic<uint64_t>& bytesCounter, std::atomic<uint64_t>& speedBps, std::atomic<bool>& running) {
namespace ch = std::chrono;
while(running) {
auto start = ch::steady_clock::now();
std::this_thread::sleep_for(ch::milliseconds(100));
auto end = ch::steady_clock::now();
double dt = ch::duration<double>(end - start).count();
speedBps = static_cast<uint64_t>(bytesCounter.exchange(0) / dt); // changing global variable to display it in progressBar
}
}
void updateReceiveProgress(std::ofstream& file, char* buffer, int bytes_recieved, double& MB, double fileSizeMB, std::atomic<uint64_t>& bytesCounter, std::atomic<uint64_t>& speedBps) {
file.write(buffer, bytes_recieved);
MB += (double)bytes_recieved / 1000000; // progress counter(MB)
bytesCounter += bytes_recieved; // is used in BytesPerSecond function
double MBps = static_cast<double>(speedBps / 1024.0 / 1024.0);
int percent = static_cast<int>(( MB / fileSizeMB) * 100);
percent = std::min(percent, 100);
int filled = percent * getBarWidth() / 100;
std::string progressBar(getBarWidth(), ' ');
for(int i=0; i < filled; ++i) progressBar[i] = '=';
std::cout << "\r" << color::cyan << "Progress: "
<< percent << "% " << color::reset
<< color::green << std::fixed << std::setprecision(1) << MB << "/" << fileSizeMB << " MB "
<< " [" << color::green << progressBar << color::reset << "] "
<< color::yellow << MBps << " MB/s "
<< color::reset << std::flush;
}
void updateSendProgress(int clientSocket, char* buffer, int bytes_read, double& MB, double fileSizeMB, std::atomic<uint64_t>& bytesCounter, std::atomic<uint64_t>& speedBps) {
send(clientSocket, buffer, bytes_read, 0);
MB += (double)bytes_read / 1000000;
bytesCounter += bytes_read;
double MBps = static_cast<double>(speedBps / 1024.0 / 1024.0);
int percent = (int)(( MB / fileSizeMB) * 100);
percent = std::min(percent, 100);
int filled = percent * getBarWidth() / 100;
std::string progressBar(getBarWidth(), ' ');
for(int i=0; i < filled; ++i) progressBar[i] = '=';
std::cout << "\r" << color::cyan << "Progress: "
<< percent << "% " << color::reset
<< color::green << std::fixed << std::setprecision(1) << MB << "/" << fileSizeMB << " MB "
<< " [" << color::green << progressBar << color::reset << "] "
<< color::yellow << MBps << " MB/s "
<< color::reset << std::flush;
}