/// <summary> /// </summary> /// <param name="line">formato: // Invio: [Percorso completo]\r\n[Ultima modifica (16 char)][Hash (32 char)]\r\n </param> public void addRawRecord(string line, int backupVersion) { //separo campi di line string[] stringSeparators = new string[] { "\r\n" }; string[] part = line.Split(stringSeparators, StringSplitOptions.None); //estraggo lastModify string long lmLong = Convert.ToInt64(part[1].Substring(0, 16), 16); string hashStr = part[1].Substring(16, 32); addRecoverRecord( new RecordFile(part[0], hashStr, -1, MyConverter.UnixTimestampToDateTime(lmLong)), backupVersion); }
/// <summary> /// return true se ricezione corretta, false altrimenti /// </summary> /// <param name="fout">already opened output fileStream</param> /// <returns>last modify date of the file</returns> private System.DateTime RecFileContent(string FileNameAndPath, FileStream fout) { //ricezione hash byte[] hashReceived = new byte[32]; var count = serverStream.Read(hashReceived, 0, 32); string strHash = System.Text.Encoding.UTF8.GetString(hashReceived); //remove \r\n socketReadline(); //legge dimensione del file long sizeFile = Convert.ToInt64(socketReadline(), 16); //legge data_ultima_modifica file var lmfile = MyConverter.UnixTimestampToDateTime(Convert.ToInt64(socketReadline(), 16)); //data_rec sendToServer(commDataRec); int attempt = 0; do { //ricezione e salvataggio su disco. var buffer = new byte[1024]; int bytesRead; long totalBytesRead = 0; fout.Seek(0, SeekOrigin.Begin); //step della progress bar long stepSize = (long)Math.Ceiling((double)sizeFile / 10); long nextStep = 0; do { bytesRead = serverStream.Read(buffer, 0, buffer.Length); fout.Write(buffer, 0, bytesRead); totalBytesRead += bytesRead; //aggiorna progress bar if (totalBytesRead > nextStep) { mainWindow.recoverW.Dispatcher.BeginInvoke(mainWindow.recoverW.DelSetRecProgressValues, sizeFile, 0, totalBytesRead); nextStep += stepSize; } }while (totalBytesRead < sizeFile); mainWindow.recoverW.Dispatcher.BeginInvoke(mainWindow.recoverW.DelSetRecProgressValues, sizeFile, 0, sizeFile); if (totalBytesRead != sizeFile) { sendToServer(commSndAgain); attempt++; if (attempt < 5) { continue; } else { break; } } //calcolo e confronto hash var computedHash = RecordFile.calcHash(FileNameAndPath, fout); if (computedHash == strHash) { sendToServer(commDataAck); return(lmfile); } else { sendToServer(commSndAgain); attempt++; } }while (attempt < 5); MyLogger.print("errore nel download del file. impossibile effettuare il ripristino.\n"); //todo: catchare questa eccezione nelle funzioni chiamanti throw new CancelFileRequestException(); }