From 4a8295ec6482f90be2c18289c41ea7fb4e4d71b0 Mon Sep 17 00:00:00 2001 From: Nico <nigr21@inf.ufpr.br> Date: Sun, 26 Feb 2023 01:47:30 -0300 Subject: [PATCH] aaaaaaa --- headers/client.h | 12 ++++++++---- headers/server.h | 44 +++++++++++++++++++++++++++----------------- 2 files changed, 35 insertions(+), 21 deletions(-) diff --git a/headers/client.h b/headers/client.h index b92ab45..0c81bf3 100644 --- a/headers/client.h +++ b/headers/client.h @@ -208,18 +208,16 @@ string client::calc_file_size(string fileName) { vector<char> client::read_file(string fileName) { fstream file; - file.open(fileName, ios::in); + file.open(fileName, ios::binary); string teste; vector<char> fileData; char c; while ((file.get(c), file.eof() == false)) { fileData.push_back(c); - teste.push_back(c); } file.close(); - cout << "vetor criado: " << teste << "\n"; return fileData; } @@ -259,7 +257,13 @@ void client::send_file() { cout << "Enviando arquivo\n"; vector<char> file = read_file(fileName); - if (file.empty() || !send_message(file, DADOS)) + if ( file.empty() ) + { + cout << "Falha ao abrir o arquivo para leitura. Abortado\n"; + return; + } + + if (!send_message(file, DADOS)) { cout << "Limite de timout, arquivo nao foi enviado\n"; return; diff --git a/headers/server.h b/headers/server.h index e602fea..691cf5f 100644 --- a/headers/server.h +++ b/headers/server.h @@ -115,7 +115,7 @@ void server::receive_text(frame *f) { int lastSeq = f->get_seq(); do { - if (!receive_valid_frame(&f)) { continue; } + if (!receive_valid_frame(&f)) { return; } if (f->get_tipo() != TEXTO) { continue; } if (f->get_seq() == lastSeq) { continue; } @@ -183,7 +183,7 @@ string server::receive_file_name() { // Aguarda receber um frame do tipo midia com o nome do arquivo do { - if (!receive_valid_frame(&fReceive)) { continue; } + if (!receive_valid_frame(&fReceive)) { return string {}; } if (fReceive->get_tipo() != MIDIA) { continue; } if (strncmp(fReceive->get_dado(), "NAME", 4)) { continue; } @@ -200,24 +200,34 @@ string server::receive_file_name() { return string(fReceive->get_dado()+4); } -// void server::receive_file_data(){ -// vector<frame *> framesMidia; -// receive +int server::receive_file_data(string fileName) { -// }; + // Abre o arquivo para escrita + ofstream file; + file.open(FILE_DESTINATION+"/"+fileName); + if (!file.is_open()) { + cout << "Falha ao criar o arquivo. Abortado\n"; + return 0; + } -int server::receive_file_data(string fileName) { - frame *fReceive; - int lastSeq = -1; - int retries = 0; + cout << "Arquivo criado com sucesso\n"; + + int lastSeq = -1; + frame *f; - // Aguarda receber um frame do tipo midia com o nome do arquivo do { - if (!receive_valid_frame(&fReceive)) { continue; } - if (fReceive->get_tipo() != MIDIA) { continue; } - if (strncmp(fReceive->get_dado(), "NAME", 4)) { continue; } - } while( fReceive->get_tipo() != FIMT); -}; + if (!receive_valid_frame(&f)) { return 0; } + if (f->get_tipo() != DADOS) { continue; } + if (f->get_seq() == lastSeq) { continue; } + + lastSeq = f->get_seq(); + file.write(f->get_dado(), f->get_tam()); + } while (f->get_tipo() != FIMT); + + cout << "Arquivo recebido com sucesso\n"; + file.close(); + return 1; +} void server::receive_midia(frame *f) { if (!create_received_dir()) { return; } @@ -227,7 +237,7 @@ void server::receive_midia(frame *f) { if (fileName.size() == 0) { return; } - if (!receive_file_data(fileName)) { return; } + receive_file_data(fileName); } // Recebe um frame do cliente -- GitLab