diff --git a/panda/src/downloader/decompressor.I b/panda/src/downloader/decompressor.I index fce4d3b8f7..351d0cb0d6 100644 --- a/panda/src/downloader/decompressor.I +++ b/panda/src/downloader/decompressor.I @@ -3,6 +3,8 @@ // //////////////////////////////////////////////////////////////////// +#include "config_downloader.h" + //////////////////////////////////////////////////////////////////// // Function: Decompressor::get_progress // Access: Public @@ -10,6 +12,12 @@ //////////////////////////////////////////////////////////////////// INLINE float Decompressor:: get_progress(void) const { + if (_initiated == false) { + downloader_cat.warning() + << "Decompressor::get_progress() - Decompression has not been " + << "initiated" << endl; + return 0.0; + } nassertr(_source_file_length > 0, 0.0); return ((float)_total_bytes_read / (float)_source_file_length); } diff --git a/panda/src/downloader/decompressor.cxx b/panda/src/downloader/decompressor.cxx index 4b0582aa51..5e1eb8011d 100644 --- a/panda/src/downloader/decompressor.cxx +++ b/panda/src/downloader/decompressor.cxx @@ -50,6 +50,7 @@ Decompressor(PT(Buffer) buffer) { //////////////////////////////////////////////////////////////////// void Decompressor:: init(PT(Buffer) buffer) { + _initiated = false; nassertv(!buffer.is_null()); _half_buffer_length = buffer->get_length()/2; _buffer = buffer; @@ -68,6 +69,8 @@ init(PT(Buffer) buffer) { Decompressor:: ~Decompressor(void) { _temp_file_name.unlink(); + if (_initiated == true) + cleanup(); } //////////////////////////////////////////////////////////////////// @@ -98,6 +101,13 @@ initiate(Filename &source_file) { int Decompressor:: initiate(Filename &source_file, Filename &dest_file) { + if (_initiated == true) { + downloader_cat.error() + << "Decompressor::run() - Decompression has already been initiated" + << endl; + return DS_error; + } + // Open source file _source_file = source_file; _source_file.set_binary(); @@ -134,7 +144,31 @@ initiate(Filename &source_file, Filename &dest_file) { _total_bytes_read = 0; _read_all_input = false; _source_buffer_length; - return 1; + _initiated = true; + _decompressor = new ZDecompressor(); + return DS_success; +} + +//////////////////////////////////////////////////////////////////// +// Function: Decompressor::cleanup +// Access: Private +// Description: +//////////////////////////////////////////////////////////////////// +void Decompressor:: +cleanup(void) { + if (_initiated == false) { + downloader_cat.error() + << "Decompressor::cleanup() - Decompression has not been " + << "initiated" << endl; + return; + } + + _initiated = false; + delete _decompressor; + _decompressor = NULL; + _read_stream.close(); + _write_stream.close(); + _source_file.unlink(); } //////////////////////////////////////////////////////////////////// @@ -144,8 +178,11 @@ initiate(Filename &source_file, Filename &dest_file) { //////////////////////////////////////////////////////////////////// int Decompressor:: run(void) { - if (_decompressor == NULL) { - _decompressor = new ZDecompressor(); + if (_initiated == false) { + downloader_cat.error() + << "Decompressor::run() - Decompression has not been initiated" + << endl; + return DS_error; } // See if there is anything left in the source file @@ -180,11 +217,7 @@ run(void) { return DS_error_zlib; if ((int)_decompressor->get_total_in() == _source_file_length && avail_out == dest_buffer_length) { - _read_stream.close(); - _write_stream.close(); - _source_file.unlink(); - delete _decompressor; - _decompressor = NULL; + cleanup(); return DS_success; } } diff --git a/panda/src/downloader/decompressor.h b/panda/src/downloader/decompressor.h index 165011f116..2cd83eb239 100644 --- a/panda/src/downloader/decompressor.h +++ b/panda/src/downloader/decompressor.h @@ -43,7 +43,9 @@ PUBLISHED: private: void init(PT(Buffer) buffer); + void cleanup(void); + bool _initiated; PT(Buffer) _buffer; int _half_buffer_length; Filename _temp_file_name; diff --git a/panda/src/downloader/downloader.I b/panda/src/downloader/downloader.I index 0f4d4ee829..ffe3044333 100644 --- a/panda/src/downloader/downloader.I +++ b/panda/src/downloader/downloader.I @@ -85,6 +85,12 @@ get_disk_write_frequency(void) const { //////////////////////////////////////////////////////////////////// INLINE int Downloader:: get_bytes_written(void) const { + if (_initiated == false) { + downloader_cat.warning() + << "Downloader::get_bytes_per_second() - Download has not been " + << "initiated" << endl; + return 0.0; + } nassertr(_current_status != NULL, 0); return _current_status->_total_bytes_written; } @@ -96,6 +102,12 @@ get_bytes_written(void) const { //////////////////////////////////////////////////////////////////// INLINE float Downloader:: get_bytes_per_second(void) const { + if (_initiated == false) { + downloader_cat.warning() + << "Downloader::get_bytes_per_second() - Download has not been " + << "initiated" << endl; + return 0.0; + } nassertr(_tlast - _tfirst > 0.0, 0.0); nassertr(_current_status != NULL, 0.0); return (float)((double)_current_status->_total_bytes / (_tlast - _tfirst)); diff --git a/panda/src/downloader/downloader.cxx b/panda/src/downloader/downloader.cxx index 6f5e344663..7b506a104b 100644 --- a/panda/src/downloader/downloader.cxx +++ b/panda/src/downloader/downloader.cxx @@ -63,6 +63,7 @@ Downloader(void) { _tfirst = 0.0; _tlast = 0.0; _got_any_data = false; + _initiated = false; #if defined(WIN32) WSAData mydata; @@ -85,8 +86,8 @@ Downloader:: if (_connected) disconnect_from_server(); _buffer.clear(); - if (_current_status != NULL) - delete _current_status; + if (_initiated == true) + cleanup(); } //////////////////////////////////////////////////////////////////// @@ -287,6 +288,13 @@ initiate(const string &file_name, Filename file_dest, int first_byte, int last_byte, int total_bytes, bool partial_content) { + if (_initiated == true) { + downloader_cat.error() + << "Downloader::initiate() - Download has already been initiated" + << endl; + return DS_error; + } + // Connect to the server if (connect_to_server() == false) return DS_error_connect; @@ -340,8 +348,6 @@ initiate(const string &file_name, Filename file_dest, return DS_error_connect; // Create a download status to maintain download progress information - if (_current_status != NULL) - delete _current_status; _current_status = new DownloadStatus(_buffer->_buffer, first_byte, last_byte, total_bytes, partial_content); @@ -349,10 +355,32 @@ initiate(const string &file_name, Filename file_dest, _tfirst = 0.0; _tlast = 0.0; _got_any_data = false; - + _initiated = true; return DS_success; } +//////////////////////////////////////////////////////////////////// +// Function: Downloader::cleanup +// Access: Private +// Description: +//////////////////////////////////////////////////////////////////// +void Downloader:: +cleanup(void) { + if (_initiated == false) { + downloader_cat.error() + << "Downloader::cleanup() - Download has not been initiated" + << endl; + return; + } + + // The "Connection: close" line tells the server to close the + // connection when the download is complete + _connected = false; + _dest_stream.close(); + delete _current_status; + _initiated = false; +} + //////////////////////////////////////////////////////////////////// // Function: Downloader::run // Access: Published @@ -360,9 +388,10 @@ initiate(const string &file_name, Filename file_dest, //////////////////////////////////////////////////////////////////// int Downloader:: run(void) { - if (_current_status == NULL) { + if (_initiated == false) { downloader_cat.error() - << "Downloader::run() - Did not call initiate() first" << endl; + << "Downloader::run() - Download has not been initiated" + << endl; return DS_error; } @@ -426,6 +455,8 @@ run(void) { fret = fast_receive(_socket, _current_status, _receive_size); if (fret == FR_eof || fret < 0) break; + else if (fret == FR_success) + _got_any_data = true; } _tlast = _clock.get_real_time(); @@ -436,10 +467,7 @@ run(void) { if (write_to_disk(_current_status) == false) return DS_error_write; } - _dest_stream.close(); - // The "Connection: close" line tells the server to close the - // connection when the download is complete - _connected = false; + cleanup(); return DS_success; } else { if (downloader_cat.is_debug()) diff --git a/panda/src/downloader/downloader.h b/panda/src/downloader/downloader.h index d15df81ee2..8a0c15b6df 100644 --- a/panda/src/downloader/downloader.h +++ b/panda/src/downloader/downloader.h @@ -91,12 +91,15 @@ private: bool parse_header(DownloadStatus *status); bool write_to_disk(DownloadStatus *status); + void cleanup(void); + private: bool _connected; int _socket; string _server_name; struct sockaddr_in _sin; + bool _initiated; PT(Buffer) _buffer; int _disk_write_frequency; float _frequency; diff --git a/panda/src/downloader/extractor.I b/panda/src/downloader/extractor.I index 9ef04beb6c..9dd52a3d57 100644 --- a/panda/src/downloader/extractor.I +++ b/panda/src/downloader/extractor.I @@ -3,6 +3,8 @@ // //////////////////////////////////////////////////////////////////// +#include "config_downloader.h" + //////////////////////////////////////////////////////////////////// // Function: Extractor::get_progress // Access: Public @@ -10,6 +12,12 @@ //////////////////////////////////////////////////////////////////// INLINE float Extractor:: get_progress(void) const { + if (_initiated == false) { + downloader_cat.warning() + << "Extractor::get_progress() - Extraction has not been initiated" + << endl; + return 0.0; + } nassertr(_source_file_length > 0, 0.0); return ((float)_total_bytes_read / (float)_source_file_length); } diff --git a/panda/src/downloader/extractor.cxx b/panda/src/downloader/extractor.cxx index 4564580cb4..541f411410 100644 --- a/panda/src/downloader/extractor.cxx +++ b/panda/src/downloader/extractor.cxx @@ -43,6 +43,7 @@ Extractor(PT(Buffer) buffer) { //////////////////////////////////////////////////////////////////// void Extractor:: init(PT(Buffer) buffer) { + _initiated = false; nassertv(!buffer.is_null()); _buffer = buffer; _mfile = NULL; @@ -55,8 +56,8 @@ init(PT(Buffer) buffer) { //////////////////////////////////////////////////////////////////// Extractor:: ~Extractor(void) { - if (_mfile != NULL) - delete _mfile; + if (_initiated == true) + cleanup(); } //////////////////////////////////////////////////////////////////// @@ -67,6 +68,13 @@ Extractor:: int Extractor:: initiate(Filename &source_file, const Filename &rel_path) { + if (_initiated == true) { + downloader_cat.error() + << "Extractor::initiate() - Extraction has already been initiated" + << endl; + return ES_error; + } + // Open source file _source_file = source_file; _source_file.set_binary(); @@ -87,9 +95,32 @@ initiate(Filename &source_file, const Filename &rel_path) { _total_bytes_read = 0; _read_all_input = false; _handled_all_input = false; + _mfile = new Multifile(); + _initiated = true; return ES_success; } +//////////////////////////////////////////////////////////////////// +// Function: Extractor::cleanup +// Access: Private +// Description: +//////////////////////////////////////////////////////////////////// +void Extractor:: +cleanup(void) { + if (_initiated == false) { + downloader_cat.error() + << "Extractor::cleanup() - Extraction has not been initiated" + << endl; + return; + } + + delete _mfile; + _mfile = NULL; + _read_stream.close(); + _source_file.unlink(); + _initiated = false; +} + //////////////////////////////////////////////////////////////////// // Function: Extractor::run // Access: Public @@ -97,8 +128,12 @@ initiate(Filename &source_file, const Filename &rel_path) { //////////////////////////////////////////////////////////////////// int Extractor:: run(void) { - if (_mfile == NULL) - _mfile = new Multifile; + if (_initiated == false) { + downloader_cat.error() + << "Extractor::run() - Extraction has not been initiated" + << endl; + return ES_error; + } // See if there is anything left in the source file if (_read_all_input == false) { @@ -116,10 +151,7 @@ run(void) { // Write to the out file if (_mfile->write(buffer_start, buffer_size, _rel_path) == true) { - _read_stream.close(); - _source_file.unlink(); - delete _mfile; - _mfile = NULL; + cleanup(); return ES_success; } return ES_ok; diff --git a/panda/src/downloader/extractor.h b/panda/src/downloader/extractor.h index 1d96671c43..35ea7b261b 100644 --- a/panda/src/downloader/extractor.h +++ b/panda/src/downloader/extractor.h @@ -41,7 +41,9 @@ PUBLISHED: private: void init(PT(Buffer) buffer); + void cleanup(void); + bool _initiated; PT(Buffer) _buffer; ifstream _read_stream;