From 315b1bc11e79cc888b6d92d26bd83969a87557a2 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Tue, 10 Jul 2018 11:48:08 -0400 Subject: [PATCH] Implement a GetData function and use it to simplify the code everywhere we want to get more data. --- src/zm_remote_camera_http.cpp | 51 ++++++++++++++++++----------------- src/zm_remote_camera_http.h | 3 ++- 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/src/zm_remote_camera_http.cpp b/src/zm_remote_camera_http.cpp index 9c91d235b..a3b31029c 100644 --- a/src/zm_remote_camera_http.cpp +++ b/src/zm_remote_camera_http.cpp @@ -220,7 +220,7 @@ int RemoteCameraHttp::ReadData( Buffer &buffer, unsigned int bytes_expected ) { } else { if ( ioctl( sd, FIONREAD, &total_bytes_to_read ) < 0 ) { Error( "Can't ioctl(): %s", strerror(errno) ); - return( -1 ); + return -1; } if ( total_bytes_to_read == 0 ) { @@ -228,20 +228,20 @@ int RemoteCameraHttp::ReadData( Buffer &buffer, unsigned int bytes_expected ) { int error = 0; socklen_t len = sizeof (error); int retval = getsockopt( sd, SOL_SOCKET, SO_ERROR, &error, &len ); - if(retval != 0 ) { + if ( retval != 0 ) { Debug( 1, "error getting socket error code %s", strerror(retval) ); } - if (error != 0) { + if ( error != 0 ) { return -1; } // Case where we are grabbing a single jpg, but no content-length was given, so the expectation is that we read until close. - return( 0 ); + return 0; } // If socket is closed locally, then select will fail, but if it is closed remotely // then we have an exception on our socket.. but no data. - Debug( 3, "Socket closed remotely" ); + Debug(3, "Socket closed remotely"); //Disconnect(); // Disconnect is done outside of ReadData now. - return( -1 ); + return -1; } // There can be lots of bytes available. I've seen 4MB or more. This will vastly inflate our buffer size unnecessarily. @@ -278,6 +278,18 @@ int RemoteCameraHttp::ReadData( Buffer &buffer, unsigned int bytes_expected ) { return( total_bytes_read ); } +int RemoteCameraHttp::GetData() { + time_t start_time = time(NULL); + int buffer_len = 0; + while ( !( buffer_len = ReadData(buffer) ) ) { + if ( zm_terminate || ( start_time - time(NULL) < ZM_WATCH_MAX_DELAY )) + return -1; + Debug(4, "Timeout waiting for REGEXP HEADER"); + usleep(100000); + } + return buffer_len; +} + int RemoteCameraHttp::GetResponse() { int buffer_len; @@ -310,9 +322,7 @@ int RemoteCameraHttp::GetResponse() static RegExpr *content_length_expr = 0; static RegExpr *content_type_expr = 0; - while ( !( buffer_len = ReadData(buffer) ) && !zm_terminate ) { - Debug(4, "Timeout waiting for REGEXP HEADER"); - } + buffer_len = GetData(); if ( buffer_len < 0 ) { Error( "Unable to read header data" ); return( -1 ); @@ -485,9 +495,7 @@ int RemoteCameraHttp::GetResponse() else { Debug( 3, "Unable to extract subheader from stream, retrying" ); - while ( !( buffer_len = ReadData(buffer) ) && !zm_terminate ) { - Debug(4, "Timeout waiting to extract subheader"); - } + buffer_len = GetData(); if ( buffer_len < 0 ) { Error( "Unable to extract subheader data" ); return( -1 ); @@ -528,7 +536,7 @@ int RemoteCameraHttp::GetResponse() while ( ((long)buffer.size() < content_length ) && ! zm_terminate ) { Debug(3, "Need more data buffer %d < content length %d", buffer.size(), content_length ); - int bytes_read = ReadData( buffer ); + int bytes_read = GetData(); if ( bytes_read < 0 ) { Error( "Unable to read content" ); @@ -542,9 +550,7 @@ int RemoteCameraHttp::GetResponse() { while ( !content_length ) { - while ( !( buffer_len = ReadData(buffer) ) && !zm_terminate ) { - Debug(4, "Timeout waiting for content"); - } + buffer_len = GetData(); if ( buffer_len < 0 ) { Error( "Unable to read content" ); return( -1 ); @@ -663,9 +669,7 @@ int RemoteCameraHttp::GetResponse() } case HEADERCONT : { - while ( !( buffer_len = ReadData(buffer) ) && !zm_terminate ) { - Debug(4, "Timeout waiting for HEADERCONT"); - } + buffer_len = GetData(); if ( buffer_len < 0 ) { Error( "Unable to read header" ); return( -1 ); @@ -949,9 +953,7 @@ int RemoteCameraHttp::GetResponse() state = CONTENT; } else { Debug( 3, "Unable to extract subheader from stream, retrying" ); - while ( !( buffer_len = ReadData(buffer) ) &&!zm_terminate ) { - Debug(1, "Timeout waiting to extra subheader non regexp"); - } + buffer_len = GetData(); if ( buffer_len < 0 ) { Error( "Unable to read subheader" ); return( -1 ); @@ -991,7 +993,7 @@ int RemoteCameraHttp::GetResponse() if ( content_length ) { while ( ( (long)buffer.size() < content_length ) && ! zm_terminate ) { Debug(4, "getting more data"); - int bytes_read = ReadData(buffer); + int bytes_read = GetData(); if ( bytes_read < 0 ) { Error("Unable to read content"); return -1; @@ -1004,8 +1006,7 @@ int RemoteCameraHttp::GetResponse() while ( !content_length && !zm_terminate ) { Debug(4, "!content_length, ReadData"); buffer_len = ReadData( buffer ); - if ( buffer_len < 0 ) - { + if ( buffer_len < 0 ) { Error( "Unable to read content" ); return( -1 ); } diff --git a/src/zm_remote_camera_http.h b/src/zm_remote_camera_http.h index a1ed4a267..d2bc96848 100644 --- a/src/zm_remote_camera_http.h +++ b/src/zm_remote_camera_http.h @@ -53,12 +53,13 @@ public: int Disconnect(); int SendRequest(); int ReadData( Buffer &buffer, unsigned int bytes_expected=0 ); + int GetData(); int GetResponse(); int PreCapture(); int Capture( Image &image ); int PostCapture(); int CaptureAndRecord( Image &image, timeval recording, char* event_directory ) {return 0;}; - int Close() { return 0; }; + int Close() { Disconnect(); return 0; }; }; #endif // ZM_REMOTE_CAMERA_HTTP_H