*** empty log message ***

This commit is contained in:
Mike Goslin 2000-12-12 00:22:14 +00:00
parent 33ff76b960
commit 9895baba0a
3 changed files with 70 additions and 52 deletions

View File

@ -13,10 +13,18 @@
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
INLINE void Downloader:: INLINE void Downloader::
set_byte_rate(float bytes) { set_byte_rate(float bytes) {
nassertv(bytes > 0.0);
if (bytes == _byte_rate)
return;
#ifdef HAVE_IPC #ifdef HAVE_IPC
mutex_lock lock(_bandwidth_frequency_lock); _buffer_lock.lock();
#endif
_new_byte_rate = bytes;
#ifdef HAVE_IPC
_buffer_lock.unlock();
#endif #endif
_byte_rate = bytes;
} }
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
@ -54,22 +62,18 @@ is_download_enabled(void) const {
// Access: Public // Access: Public
// Description: // Description:
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
INLINE bool Downloader:: INLINE void Downloader::
set_disk_write_frequency(int frequency) { set_disk_write_frequency(int frequency) {
nassertr(frequency > 0, false); nassertv(frequency > 0);
_disk_write_frequency = frequency;
int size = _disk_write_frequency * (_byte_rate * _frequency);
if (size == _buffer_size)
return false;
nassertr(size > 0, false);
#ifdef HAVE_IPC #ifdef HAVE_IPC
_buffer_lock.lock(); _buffer_lock.lock();
#endif #endif
_new_buffer_size = size;
_new_disk_write_frequency = frequency;
#ifdef HAVE_IPC #ifdef HAVE_IPC
_buffer_lock.unlock(); _buffer_lock.unlock();
#endif #endif
return true;
} }
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////

View File

@ -106,15 +106,14 @@ Downloader(PT(Buffer) buffer) : AsyncUtility() {
void Downloader:: void Downloader::
init(void) { init(void) {
_disk_write_frequency = downloader_disk_write_frequency; _disk_write_frequency = downloader_disk_write_frequency;
_new_disk_write_frequency = 0;
_byte_rate = downloader_byte_rate; _byte_rate = downloader_byte_rate;
_new_byte_rate = 0;
_frequency = downloader_frequency; _frequency = downloader_frequency;
nassertv(_frequency > 0); nassertv(_frequency > 0 && _byte_rate > 0);
if (_frequency == 0) _read_size = _byte_rate * _frequency;
_buffer_size = _disk_write_frequency * _byte_rate; _disk_buffer_size = _disk_write_frequency * _read_size;
else _buffer = new Buffer(_disk_buffer_size);
_buffer_size = _disk_write_frequency * (_byte_rate * _frequency);
_buffer = new Buffer(_buffer_size);
_new_buffer_size = 0;
_connected = false; _connected = false;
_token_board = new DownloaderTokenBoard; _token_board = new DownloaderTokenBoard;
_download_enabled = true; _download_enabled = true;
@ -518,7 +517,7 @@ attempt_read(int length, DownloadStatus &status, int &bytes_read,
// Ensure we have enough room in the buffer to download length bytes // Ensure we have enough room in the buffer to download length bytes
// If we don't have enough room, write the buffer to disk // If we don't have enough room, write the buffer to disk
if (status._bytes_in_buffer + length > _buffer_size) { if (status._bytes_in_buffer + length > _disk_buffer_size) {
if (downloader_cat.is_debug()) if (downloader_cat.is_debug())
downloader_cat.debug() downloader_cat.debug()
<< "Downloader::attempt_read() - Flushing buffer" << endl; << "Downloader::attempt_read() - Flushing buffer" << endl;
@ -642,25 +641,59 @@ download(const string &file_name, Filename file_dest,
// Loop at the requested frequency until the download completes // Loop at the requested frequency until the download completes
for (;;) { for (;;) {
bool resize_buffer = false;
// Ensure that these don't change while we're computing read_size // Ensure that these don't change while we're computing read_size
#ifdef HAVE_IPC #ifdef HAVE_IPC
_bandwidth_frequency_lock.lock(); _buffer_lock.lock();
#endif #endif
// read_size is the length of the buffer requested via safe_receive()
int read_size;
nassertr(_frequency > 0, D_error); nassertr(_frequency > 0, D_error);
if (sync == true || _frequency == 0) // If byte rate has changed, recompute read size and write buffer size
read_size = (int)_byte_rate; if (_new_byte_rate > 0) {
else _read_size = (int)(_new_byte_rate * _frequency);
read_size = (int)(_byte_rate * _frequency); _byte_rate = _new_byte_rate;
_new_byte_rate = 0;
resize_buffer = true;
}
// If the disk write frequency has changed, compute a new buffer size
if (_new_disk_write_frequency > 0) {
_disk_write_frequency = _new_disk_write_frequency;
_new_disk_write_frequency = 0;
resize_buffer = true;
}
if (resize_buffer == true) {
// Flush the write buffer before resizing it
if (status._bytes_in_buffer > 0) {
if (write_to_disk(status) == false) {
downloader_cat.error()
<< "Downloader::download() - failed to flush buffer during "
<< "resize" << endl;
return D_error;
}
}
// Resize the buffer
_disk_buffer_size = (int)(_disk_write_frequency * _read_size);
if (downloader_cat.is_debug())
downloader_cat.debug()
<< "Downloader::download() - resizing disk buffer to: "
<< _disk_buffer_size << endl;
_buffer.clear();
_buffer = new Buffer(_disk_buffer_size);
}
#ifdef HAVE_IPC #ifdef HAVE_IPC
_bandwidth_frequency_lock.unlock(); _buffer_lock.unlock();
#endif #endif
// Attempt to read // Attempt to read
int bytes_read; int bytes_read;
bool stalled; bool stalled;
int ret = attempt_read(read_size, status, bytes_read, stalled); int ret = attempt_read(_read_size, status, bytes_read, stalled);
_last_attempt_stalled = stalled; _last_attempt_stalled = stalled;
if (bytes_read > 0) if (bytes_read > 0)
got_any_data = true; got_any_data = true;
@ -942,26 +975,6 @@ write_to_disk(DownloadStatus &status) {
status.reset(); status.reset();
// Now see if we need to adjust the buffer size
if (_new_buffer_size > 0) {
#ifdef HAVE_IPC
_buffer_lock.lock();
#endif
if (downloader_cat.is_debug())
downloader_cat.debug()
<< "Downloader::write_to_buffer() - resizing buffer to: "
<< _new_buffer_size << endl;
_buffer.clear();
_buffer = new Buffer(_new_buffer_size);
_buffer_size = _new_buffer_size;
_new_buffer_size = 0;
#ifdef HAVE_IPC
_buffer_lock.unlock();
#endif
}
return true; return true;
} }

View File

@ -54,7 +54,7 @@ PUBLISHED:
INLINE void set_byte_rate(float bytes); INLINE void set_byte_rate(float bytes);
INLINE float get_byte_rate(void) const; INLINE float get_byte_rate(void) const;
INLINE bool set_disk_write_frequency(int frequency); INLINE void set_disk_write_frequency(int frequency);
INLINE int get_disk_write_frequency(void) const; INLINE int get_disk_write_frequency(void) const;
INLINE void enable_download(bool val); INLINE void enable_download(bool val);
INLINE bool is_download_enabled(void) const; INLINE bool is_download_enabled(void) const;
@ -109,18 +109,19 @@ private:
bool _connected; bool _connected;
#ifdef HAVE_IPC #ifdef HAVE_IPC
mutex _bandwidth_frequency_lock;
mutex _buffer_lock; mutex _buffer_lock;
#endif #endif
int _socket; int _socket;
PT(Buffer) _buffer; PT(Buffer) _buffer;
int _disk_write_frequency; int _disk_write_frequency;
int _new_disk_write_frequency;
float _byte_rate; float _byte_rate;
float _new_byte_rate;
int _read_size;
bool _download_enabled; bool _download_enabled;
ofstream _dest_stream; ofstream _dest_stream;
int _new_buffer_size; int _disk_buffer_size;
int _buffer_size;
bool _last_attempt_stalled; bool _last_attempt_stalled;
string _server_name; string _server_name;