Browse Source

Take advantage of Deserializer::IsEof() being virtual to clean up HttpRequest code. Now position_ is no longer manipulated to fake the EOF behavior.

Lasse Öörni 9 years ago
parent
commit
e7e0cb51a6

+ 1 - 1
Source/Urho3D/IO/NamedPipe.h

@@ -47,7 +47,7 @@ public:
     /// Destruct and close.
     virtual ~NamedPipe();
 
-    /// Read bytes from the pipe. Return number of bytes actually read.
+    /// Read bytes from the pipe without blocking if there is less data available. Return number of bytes actually read. 
     virtual unsigned Read(void* dest, unsigned size);
     /// Set position. No-op for pipes.
     virtual unsigned Seek(unsigned position);

+ 18 - 14
Source/Urho3D/Network/HttpRequest.cpp

@@ -207,12 +207,12 @@ unsigned HttpRequest::Read(void* dest, unsigned size)
 
     for (;;)
     {
-        unsigned bytesAvailable;
+        Pair<unsigned, bool> status;
 
         for (;;)
         {
-            bytesAvailable = CheckEofAndAvailableSize();
-            if (bytesAvailable || IsEof())
+            status = CheckAvailableSizeAndEof();
+            if (status.first_ || status.second_)
                 break;
             // While no bytes and connection is still open, block until has some data
             mutex_.Release();
@@ -220,6 +220,8 @@ unsigned HttpRequest::Read(void* dest, unsigned size)
             mutex_.Acquire();
         }
 
+        unsigned bytesAvailable = status.first_;
+
         if (bytesAvailable)
         {
             if (bytesAvailable > sizeLeft)
@@ -247,8 +249,6 @@ unsigned HttpRequest::Read(void* dest, unsigned size)
             break;
     }
 
-    // Check for end-of-file once more after reading the bytes
-    CheckEofAndAvailableSize();
     mutex_.Release();
     return totalRead;
 #else
@@ -259,35 +259,39 @@ unsigned HttpRequest::Read(void* dest, unsigned size)
 
 unsigned HttpRequest::Seek(unsigned position)
 {
-    return position_;
+    return 0;
+}
+
+bool HttpRequest::IsEof() const
+{
+    MutexLock lock(mutex_);
+    return CheckAvailableSizeAndEof().second_;
 }
 
 String HttpRequest::GetError() const
 {
     MutexLock lock(mutex_);
-    const_cast<HttpRequest*>(this)->CheckEofAndAvailableSize();
     return error_;
 }
 
 HttpRequestState HttpRequest::GetState() const
 {
     MutexLock lock(mutex_);
-    const_cast<HttpRequest*>(this)->CheckEofAndAvailableSize();
     return state_;
 }
 
 unsigned HttpRequest::GetAvailableSize() const
 {
     MutexLock lock(mutex_);
-    return const_cast<HttpRequest*>(this)->CheckEofAndAvailableSize();
+    return CheckAvailableSizeAndEof().first_;
 }
 
-unsigned HttpRequest::CheckEofAndAvailableSize()
+Pair<unsigned, bool> HttpRequest::CheckAvailableSizeAndEof() const
 {
-    unsigned bytesAvailable = (writePosition_ - readPosition_) & (READ_BUFFER_SIZE - 1);
-    if (state_ == HTTP_ERROR || (state_ == HTTP_CLOSED && !bytesAvailable))
-        position_ = M_MAX_UNSIGNED;
-    return bytesAvailable;
+    Pair<unsigned, bool> ret;
+    ret.first_ = (writePosition_ - readPosition_) & (READ_BUFFER_SIZE - 1);
+    ret.second_ = (state_ == HTTP_ERROR || (state_ == HTTP_CLOSED && !ret.first_));
+    return ret;
 }
 
 }

+ 4 - 2
Source/Urho3D/Network/HttpRequest.h

@@ -56,6 +56,8 @@ public:
     virtual unsigned Read(void* dest, unsigned size);
     /// Set position from the beginning of the stream. Not supported.
     virtual unsigned Seek(unsigned position);
+    /// Return whether all response data has been read.
+    virtual bool IsEof() const;
 
     /// Return URL used in the request.
     const String& GetURL() const { return url_; }
@@ -74,8 +76,8 @@ public:
     bool IsOpen() const { return GetState() == HTTP_OPEN; }
 
 private:
-    /// Check for end of the data stream and return available size in buffer. Must only be called when the mutex is held by the main thread.
-    unsigned CheckEofAndAvailableSize();
+    /// Check for available read data in buffer and whether end has been reached. Must only be called when the mutex is held by the main thread.
+    Pair<unsigned, bool> CheckAvailableSizeAndEof() const;
 
     /// URL.
     String url_;