first working prototype
This commit is contained in:
+15
-16
@@ -17,7 +17,7 @@
|
||||
#include <switch.h>
|
||||
#endif
|
||||
#define PORT 8080
|
||||
#define BUFFER_SIZE 1024
|
||||
#define BUFFER_SIZE 65536
|
||||
#define MULTICAST_PORT 8081
|
||||
#define MULTICAST_GROUP "239.0.0.1" // Multicast group IP
|
||||
|
||||
@@ -37,11 +37,14 @@ struct ThreadArgs {
|
||||
fs::path directory;
|
||||
};
|
||||
|
||||
bool receiveAck(int sock) {
|
||||
char ack[4] = {0};
|
||||
int bytes_received = read(sock, ack, 3);
|
||||
std::cout << "receiveAck bytes_received: " << bytes_received << std::endl;
|
||||
return bytes_received > 0 && std::string(ack) == "ACK";
|
||||
static bool send_all(int sock, const void* buf, size_t len) {
|
||||
size_t sent = 0;
|
||||
while (sent < len) {
|
||||
ssize_t n = send(sock, static_cast<const char*>(buf) + sent, len - sent, 0);
|
||||
if (n <= 0) return false;
|
||||
sent += n;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Отправка строки с учетом её длины
|
||||
@@ -76,7 +79,7 @@ void sendFile(int sock, const fs::path &base_path, const fs::path &filepath) {
|
||||
// Get the size of the file
|
||||
std::streamsize file_size = infile.tellg();
|
||||
infile.seekg(0, std::ios::beg);
|
||||
char buffer[BUFFER_SIZE];
|
||||
char* buffer = new char[BUFFER_SIZE];
|
||||
|
||||
std::cout << "send filesize: " << file_size << std::endl;
|
||||
|
||||
@@ -87,22 +90,18 @@ void sendFile(int sock, const fs::path &base_path, const fs::path &filepath) {
|
||||
|
||||
while (file_size > 0) {
|
||||
infile.read(buffer, BUFFER_SIZE);
|
||||
ssize_t bytes_sent = send(sock, buffer, infile.gcount(), 0);
|
||||
if (bytes_sent == -1) {
|
||||
std::streamsize count = infile.gcount();
|
||||
if (!send_all(sock, buffer, count)) {
|
||||
std::cerr << "Failed to send file data" << std::endl;
|
||||
delete[] buffer;
|
||||
pthread_exit(nullptr);
|
||||
}
|
||||
file_size -= bytes_sent;
|
||||
|
||||
// Wait for acknowledgment after each chunk
|
||||
if (!receiveAck(sock)) {
|
||||
std::cerr << "Failed to receive acknowledgment" << std::endl;
|
||||
pthread_exit(nullptr);
|
||||
}
|
||||
file_size -= count;
|
||||
}
|
||||
|
||||
std::cout << "File sent successfully: " << filepath << std::endl;
|
||||
|
||||
delete[] buffer;
|
||||
infile.close();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user