From aeb400fc59349d7ef483d2883bb8421913c0c476 Mon Sep 17 00:00:00 2001 From: Mike Goslin Date: Fri, 17 Nov 2000 22:33:00 +0000 Subject: [PATCH] *** empty log message *** --- panda/src/downloader/downloader.I | 20 +++++++++++++++++ panda/src/downloader/downloader.cxx | 33 +++++++++++++++++------------ panda/src/downloader/downloader.h | 6 ++++-- 3 files changed, 43 insertions(+), 16 deletions(-) diff --git a/panda/src/downloader/downloader.I b/panda/src/downloader/downloader.I index 23159b9362..3d2daeee10 100644 --- a/panda/src/downloader/downloader.I +++ b/panda/src/downloader/downloader.I @@ -48,3 +48,23 @@ INLINE bool Downloader:: is_download_enabled(void) const { return _download_enabled; } + +//////////////////////////////////////////////////////////////////// +// Function: Downloader::change_buffer_size +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE bool Downloader:: +change_buffer_size(int size) { + if (size == _buffer_size) + return true; + nassertr(size > 0, false); +#ifdef HAVE_IPC + _buffer_lock.lock(); +#endif + _new_buffer_size = size; +#ifdef HAVE_IPC + _buffer_lock.unlock(); +#endif + return true; +} diff --git a/panda/src/downloader/downloader.cxx b/panda/src/downloader/downloader.cxx index 54d87d18fe..1d9e70333d 100644 --- a/panda/src/downloader/downloader.cxx +++ b/panda/src/downloader/downloader.cxx @@ -93,6 +93,8 @@ init(PT(Buffer) buffer) { _download_enabled = true; // We need to flush after every write in case we're interrupted _dest_stream.setf(ios::unitbuf, 0); + _buffer_size = _buffer->get_length(); + _new_buffer_size = 0; #if defined(WIN32) WSAData mydata; @@ -207,19 +209,6 @@ disconnect_from_server(void) { _connected = false; } -//////////////////////////////////////////////////////////////////// -// Function: Downloader::change_buffer_size -// Access: Public -// Description: -//////////////////////////////////////////////////////////////////// -void Downloader:: -change_buffer_size(int size) { - if (_buffer->get_length() == size) - return; - _buffer.clear(); - _buffer = new Buffer(size); -} - //////////////////////////////////////////////////////////////////// // Function: Downloader::request_download // Access: Public @@ -526,7 +515,7 @@ download(const string &file_name, Filename file_dest, // Ensure we have enough room in the buffer to download read_size // If we don't have enough room, write the buffer to disk - if (status._bytes_in_buffer + read_size > downloader_buffer_size) { + if (status._bytes_in_buffer + read_size > _buffer_size) { if (downloader_cat.is_debug()) downloader_cat.debug() << "Downloader::download() - Flushing buffer" << endl; @@ -758,6 +747,22 @@ write_to_disk(DownloadStatus &status) { status.reset(); + // Now see if we need to adjust the buffer size + if (_new_buffer_size > 0) { +#ifdef HAVE_IPC + _buffer_lock.lock(); +#endif + + _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; } diff --git a/panda/src/downloader/downloader.h b/panda/src/downloader/downloader.h index 37eea26b83..3a1d53195b 100644 --- a/panda/src/downloader/downloader.h +++ b/panda/src/downloader/downloader.h @@ -46,12 +46,11 @@ PUBLISHED: int last_byte, int total_bytes, bool partial_content = true); - void change_buffer_size(int size); - INLINE void set_bandwidth(float bytes); INLINE float get_bandwidth(void) const; INLINE void enable_download(bool val); INLINE bool is_download_enabled(void) const; + INLINE bool change_buffer_size(int size); private: class DownloadStatus { @@ -100,6 +99,7 @@ private: #ifdef HAVE_IPC mutex _bandwidth_frequency_lock; + mutex _buffer_lock; #endif int _socket; @@ -107,6 +107,8 @@ private: float _bandwidth; bool _download_enabled; ofstream _dest_stream; + int _new_buffer_size; + int _buffer_size; string _server_name; struct sockaddr_in _sin;