diff options
author | Alexander Tkachev | 2016-07-07 17:21:40 +0600 |
---|---|---|
committer | Alexander Tkachev | 2016-08-24 16:07:55 +0600 |
commit | bb67b81d04101d5f8457cf2e1fc1e67d09ad29c3 (patch) | |
tree | 2e9424f5c3f747c1b1a541a41c0a5b08d3c2eaa8 /backends/networking/sdl_net/reader.cpp | |
parent | 79b39bf0d080d43dc1c5569272b8091017dfa3e3 (diff) | |
download | scummvm-rg350-bb67b81d04101d5f8457cf2e1fc1e67d09ad29c3.tar.gz scummvm-rg350-bb67b81d04101d5f8457cf2e1fc1e67d09ad29c3.tar.bz2 scummvm-rg350-bb67b81d04101d5f8457cf2e1fc1e67d09ad29c3.zip |
CLOUD: Update Reader to support pausing
That means that if current buffer is over, reader will stop reading and
it's safe to call readResponse() again, so it would continue from the
place it left.
Diffstat (limited to 'backends/networking/sdl_net/reader.cpp')
-rw-r--r-- | backends/networking/sdl_net/reader.cpp | 74 |
1 files changed, 59 insertions, 15 deletions
diff --git a/backends/networking/sdl_net/reader.cpp b/backends/networking/sdl_net/reader.cpp index edfaf81874..b6bf9e52ba 100644 --- a/backends/networking/sdl_net/reader.cpp +++ b/backends/networking/sdl_net/reader.cpp @@ -28,6 +28,9 @@ namespace Networking { Reader::Reader() { + _state = RS_NONE; + _bytesLeft = 0; + _contentLength = 0; _availableBytes = 0; @@ -35,6 +38,9 @@ Reader::Reader() { _windowUsed = 0; _windowSize = 0; + _headers = ""; + _buffer = ""; + _content = "POST /upload HTTP/1.1\r\n" \ "Host: 127.0.0.1:12345\r\n" \ @@ -61,10 +67,20 @@ Reader::Reader() { Reader::~Reader() {} -void Reader::readResponse() { +bool Reader::readResponse() { + if (_state == RS_NONE) _state = RS_READING_HEADERS; + while (true) { - readHeaders(); //til "\r\n\r\n" - readContent(); //til "--" + _boundary + if (!bytesLeft()) return false; + + if (_state == RS_READING_HEADERS) + if (!readHeaders()) + return false; + + if (_state == RS_READING_CONTENT) + if (!readContent()) + return false; + if (_availableBytes >= 2) { Common::String bts; bts += readOne(); @@ -79,15 +95,25 @@ void Reader::readResponse() { } if (_availableBytes > 0) debug("STRANGE END: %llu bytes left", _availableBytes); else debug("END"); + + return true; } -void Reader::readHeaders() { +bool Reader::readHeaders() { Common::String boundary = "\r\n\r\n"; - makeWindow(boundary.size()); + if (_window == nullptr) { + makeWindow(boundary.size()); + _headers = ""; + } - Common::String headers = ""; - while (readOneByteInString(headers, boundary)); - handleHeaders(headers); + while (readOneByteInString(_headers, boundary)) { + if (!bytesLeft()) return false; + } + handleHeaders(_headers); + + freeWindow(); + _state = RS_READING_CONTENT; + return true; } namespace { @@ -136,9 +162,12 @@ void Reader::handleHeaders(Common::String headers) { } } -void Reader::readContent() { +bool Reader::readContent() { Common::String boundary = "--" + _boundary; - makeWindow(boundary.size()); + if (_window == nullptr) { + makeWindow(boundary.size()); + _buffer = ""; + } /* if (_fieldIsFile) { @@ -150,10 +179,15 @@ void Reader::readContent() { handleFileContent(tempFileName); } else { */ - Common::String buffer = ""; - while (readOneByteInString(buffer, boundary)) ; - handleValueContent(buffer); + while (readOneByteInString(_buffer, boundary)) { + if (!bytesLeft()) return false; + } + handleValueContent(_buffer); //} + + freeWindow(); + _state = RS_READING_HEADERS; + return true; } void Reader::handleFileContent(Common::String filename) { @@ -166,14 +200,19 @@ void Reader::handleValueContent(Common::String value) { } void Reader::makeWindow(uint32 size) { - delete[] _window; - _window = nullptr; + freeWindow(); _window = new byte[size]; _windowUsed = 0; _windowSize = size; } +void Reader::freeWindow() { + delete[] _window; + _window = nullptr; + _windowUsed = _windowSize = 0; +} + /* bool Reader::readOneByteInStream(stream) { b = read(1); @@ -214,7 +253,12 @@ byte Reader::readOne() { byte b = _content[0]; _content.deleteChar(0); --_availableBytes; + --_bytesLeft; return b; } +uint32 Reader::bytesLeft() { return _bytesLeft; } + +void Reader::setBytesLeft(uint32 b) { _bytesLeft = b; } + } // End of namespace Networking |