public bool SendChecker() { // returns true if we're done sending if (file_stream == null ){ return true; } byte[] b; bool received_error = false; b = network.PopReceiveBuffer(); while (b != null){ // we have data in b! Deal with it. FilePiece piece = FilePiece.parse_packet(b); if (piece != null){ if (piece.number > 0){ // do nothing. We are not a receiver! }else if (piece.number == MESSAGE_RESEND){ // we are server! We must re-send this packet. received_error = true; packets_error++; Int64 actual_number = BitConverter.ToInt64(piece.get_data(), 0); if (actual_number != 0){ FilePiece specific_piece = new FilePiece(actual_number, file_stream.GetSpecificChunk(actual_number)); network.send(specific_piece.get_packet()); bytes_sent += specific_piece.get_packet_size(); }else{ Console.WriteLine("Hmm, something's wrong with that packet"); } } } b = network.PopReceiveBuffer(); } if (file_stream.GetFileStatus()){ // done sending, just sending corrections if (last_error == DateTime.MinValue){ Console.WriteLine("last_error is null"); last_error = new DateTime(); }else if (received_error){ last_error = DateTime.Now; }else if ((DateTime.Now.Subtract(last_error) ).TotalMilliseconds > CORRECTIONS_TIMEOUT){ Console.WriteLine("No error! Time out :)"); return true; } } // now that we're caught up, adjust the speed based on errors. if (received_error){ sending_speed *= change_with_error_decrease; }else{ sending_speed *= change_without_error_increase; } // returns true if all pieces have been sent. //Does some sending if there are things to send double time_spent = (DateTime.Now.AddSeconds(1) - start_time).TotalSeconds; while (sending_speed > (double)bytes_sent/(double)time_spent){ FilePiece fp = file_stream.GetNextPiece(); if (fp != null){ byte[] bytes = fp.get_packet(); if (bytes != null){ network.send(bytes); bytes_sent += bytes.Length; }else{ // exit loop if we're done sending file return true; } }else{ return true; } } return false; }
public bool SendChecker() { // returns true if we're done sending if (file_stream == null ){ return true; } byte[] b; bool received_error = false; b = network.PopReceiveBuffer(); while (b != null){ // we have data in b! Deal with it. FilePiece piece = FilePiece.parse_packet(b); if (piece != null){ if (piece.number > 0){ // do nothing. We are not a receiver! }else if (piece.number == MESSAGE_RESEND){ // we are server! We must re-send this packet. received_error = true; packets_error++; FilePiece specific_piece = new FilePiece(piece.number, file_stream.GetSpecificChunk(piece.number)); network.send(specific_piece.get_packet()); bytes_sent += specific_piece.get_packet_size(); } } b = network.PopReceiveBuffer(); } if (file_stream.GetFileStatus()){ return true; // done sending, just sending corrections } // now that we're caught up, adjust the speed based on errors. if (received_error){ sending_speed *= change_with_error_decrease; }else{ sending_speed *= change_without_error_increase; } // returns true if all pieces have been sent. //Does some sending if there are things to send if (!file_stream.GetFileStatus()){ double time_spent = (DateTime.Now.AddSeconds(1) - start_time).TotalSeconds; while (sending_speed > (double)bytes_sent/(double)time_spent){ FilePiece fp = file_stream.GetNextPiece(); if (fp != null){ byte[] bytes = fp.get_packet(); if (bytes != null){ network.send(bytes); bytes_sent += bytes.Length; }else{ // exit loop if we're done sending file return true; } }else{ return true; } } return false; }else{ // file stream is done, so we're done sending! return true; } }