-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSSend.cpp
More file actions
142 lines (104 loc) · 3.55 KB
/
SSend.cpp
File metadata and controls
142 lines (104 loc) · 3.55 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
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
#include "echelonheaders.h"
#include <atomic>
#include <cstring>
#include <fstream>
#include <thread>
#include <chrono>
#include <iostream>
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
int serverSend(Config cfg) {
namespace ch = std::chrono;
double speed = cfg.speed;
const char* passkey = cfg.passkey.c_str();
const char* filename = cfg.file.c_str(); // argv[3] is a file //im tired to repeat this fucking comments
int serverSocket = socket(AF_INET, SOCK_STREAM, 0);
sockaddr_in serverAddress;
serverAddress.sin_family = AF_INET; // IPV4
serverAddress.sin_port = htons(PORT); // set in the echelonheaders.h file
serverAddress.sin_addr.s_addr = INADDR_ANY; // bind on 0.0.0.0
int i;
for(i=0; i < cfg.attemptAmount; i++) {
if(bind(serverSocket, (struct sockaddr*)&serverAddress, sizeof(serverAddress)) == 0) {
break;
}
std::cerr << "Failed binding (attempt " << (i+1) << "/" << cfg.attemptAmount << ").\n";
std::this_thread::sleep_for(ch::milliseconds(cfg.attemptInterval));
}
if(i == cfg.attemptAmount) {
std::cout << "Failed to bind.\n" << std::strerror(errno) << "\n";
return 1;
}
listen(serverSocket, 5);
int clientSocket = accept(serverSocket, nullptr, nullptr);
std::ifstream file(filename, std::ios::binary);
if(!file.is_open()) {
std::cerr << "error: can't open file " << filename << std::endl;
return 1;
}
if(passkey != nullptr && strlen(passkey) > 0) {
bool logged = false;
while(logged != true) {
int userPasskeyLength;
recv(clientSocket, &userPasskeyLength, sizeof(int), 0);
if(userPasskeyLength <= 0 || userPasskeyLength > 1024) {
std::cerr << "Invalid passkey lenght\n";
close(clientSocket);
clientSocket = accept(serverSocket, nullptr, nullptr);
continue;
}
std::string userPasskey(userPasskeyLength, '\0');
recv(clientSocket, userPasskey.data(), userPasskeyLength, 0);
if(strcmp(userPasskey.c_str(), passkey) != 0) {
std::cout << "Wrong passkey." << std::endl;
close(clientSocket);
clientSocket = accept(serverSocket, nullptr, nullptr);
continue;
}
else {
std::cout << "Passkey accepted." << std::endl;
logged = true;
}
}
}
int filenameSize = strlen(filename);
if(filenameSize <= 0 || filenameSize > 512) {
std::cerr << "Invalid filename lenght\n";
close(clientSocket);
return 1;
}
send(clientSocket, &filenameSize, sizeof(int), 0);
send(clientSocket, filename, filenameSize, 0);
std::streampos fileSize = file.tellg();
file.seekg(0, std::ios::end);
fileSize = file.tellg() - fileSize;
file.seekg(0, std::ios::beg);
send(clientSocket, &fileSize, sizeof(int), 0);
char buffer[BUFFER_SIZE];
int bytes_read;
int sleepDuration;
double MB = 0;
std::string progressBar(50, ' ');
double fileSizeMB = (double)fileSize / 1000000;
if(speed > 0) {
sleepDuration = calculateSpeed(speed);
}
std::thread speedometer(BytesPerSecond, std::ref(bytesCounter), std::ref(speedBps), std::ref(running));
while((bytes_read = file.readsome(buffer, sizeof(buffer))) > 0) {
updateSendProgress(clientSocket, buffer, bytes_read, MB, fileSizeMB, bytesCounter, speedBps);
if(speed > 0) {
std::this_thread::sleep_for(std::chrono::microseconds(sleepDuration));
}
}
running = false;
speedometer.join();
std::cout << std::endl;
file.close();
close(clientSocket);
close(serverSocket);
return 0;
}