Implement a GetData function and use it to simplify the code everywhere we want to get more data.

This commit is contained in:
Isaac Connor 2018-07-10 11:48:08 -04:00
parent d3034670a0
commit 315b1bc11e
2 changed files with 28 additions and 26 deletions

View File

@ -220,7 +220,7 @@ int RemoteCameraHttp::ReadData( Buffer &buffer, unsigned int bytes_expected ) {
} else { } else {
if ( ioctl( sd, FIONREAD, &total_bytes_to_read ) < 0 ) { if ( ioctl( sd, FIONREAD, &total_bytes_to_read ) < 0 ) {
Error( "Can't ioctl(): %s", strerror(errno) ); Error( "Can't ioctl(): %s", strerror(errno) );
return( -1 ); return -1;
} }
if ( total_bytes_to_read == 0 ) { if ( total_bytes_to_read == 0 ) {
@ -228,20 +228,20 @@ int RemoteCameraHttp::ReadData( Buffer &buffer, unsigned int bytes_expected ) {
int error = 0; int error = 0;
socklen_t len = sizeof (error); socklen_t len = sizeof (error);
int retval = getsockopt( sd, SOL_SOCKET, SO_ERROR, &error, &len ); 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) ); Debug( 1, "error getting socket error code %s", strerror(retval) );
} }
if (error != 0) { if ( error != 0 ) {
return -1; 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. // 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 // 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. // 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. //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. // 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 ); 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 RemoteCameraHttp::GetResponse()
{ {
int buffer_len; int buffer_len;
@ -310,9 +322,7 @@ int RemoteCameraHttp::GetResponse()
static RegExpr *content_length_expr = 0; static RegExpr *content_length_expr = 0;
static RegExpr *content_type_expr = 0; static RegExpr *content_type_expr = 0;
while ( !( buffer_len = ReadData(buffer) ) && !zm_terminate ) { buffer_len = GetData();
Debug(4, "Timeout waiting for REGEXP HEADER");
}
if ( buffer_len < 0 ) { if ( buffer_len < 0 ) {
Error( "Unable to read header data" ); Error( "Unable to read header data" );
return( -1 ); return( -1 );
@ -485,9 +495,7 @@ int RemoteCameraHttp::GetResponse()
else else
{ {
Debug( 3, "Unable to extract subheader from stream, retrying" ); Debug( 3, "Unable to extract subheader from stream, retrying" );
while ( !( buffer_len = ReadData(buffer) ) && !zm_terminate ) { buffer_len = GetData();
Debug(4, "Timeout waiting to extract subheader");
}
if ( buffer_len < 0 ) { if ( buffer_len < 0 ) {
Error( "Unable to extract subheader data" ); Error( "Unable to extract subheader data" );
return( -1 ); return( -1 );
@ -528,7 +536,7 @@ int RemoteCameraHttp::GetResponse()
while ( ((long)buffer.size() < content_length ) && ! zm_terminate ) while ( ((long)buffer.size() < content_length ) && ! zm_terminate )
{ {
Debug(3, "Need more data buffer %d < content length %d", buffer.size(), content_length ); 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 ) { if ( bytes_read < 0 ) {
Error( "Unable to read content" ); Error( "Unable to read content" );
@ -542,9 +550,7 @@ int RemoteCameraHttp::GetResponse()
{ {
while ( !content_length ) while ( !content_length )
{ {
while ( !( buffer_len = ReadData(buffer) ) && !zm_terminate ) { buffer_len = GetData();
Debug(4, "Timeout waiting for content");
}
if ( buffer_len < 0 ) { if ( buffer_len < 0 ) {
Error( "Unable to read content" ); Error( "Unable to read content" );
return( -1 ); return( -1 );
@ -663,9 +669,7 @@ int RemoteCameraHttp::GetResponse()
} }
case HEADERCONT : case HEADERCONT :
{ {
while ( !( buffer_len = ReadData(buffer) ) && !zm_terminate ) { buffer_len = GetData();
Debug(4, "Timeout waiting for HEADERCONT");
}
if ( buffer_len < 0 ) { if ( buffer_len < 0 ) {
Error( "Unable to read header" ); Error( "Unable to read header" );
return( -1 ); return( -1 );
@ -949,9 +953,7 @@ int RemoteCameraHttp::GetResponse()
state = CONTENT; state = CONTENT;
} else { } else {
Debug( 3, "Unable to extract subheader from stream, retrying" ); Debug( 3, "Unable to extract subheader from stream, retrying" );
while ( !( buffer_len = ReadData(buffer) ) &&!zm_terminate ) { buffer_len = GetData();
Debug(1, "Timeout waiting to extra subheader non regexp");
}
if ( buffer_len < 0 ) { if ( buffer_len < 0 ) {
Error( "Unable to read subheader" ); Error( "Unable to read subheader" );
return( -1 ); return( -1 );
@ -991,7 +993,7 @@ int RemoteCameraHttp::GetResponse()
if ( content_length ) { if ( content_length ) {
while ( ( (long)buffer.size() < content_length ) && ! zm_terminate ) { while ( ( (long)buffer.size() < content_length ) && ! zm_terminate ) {
Debug(4, "getting more data"); Debug(4, "getting more data");
int bytes_read = ReadData(buffer); int bytes_read = GetData();
if ( bytes_read < 0 ) { if ( bytes_read < 0 ) {
Error("Unable to read content"); Error("Unable to read content");
return -1; return -1;
@ -1004,8 +1006,7 @@ int RemoteCameraHttp::GetResponse()
while ( !content_length && !zm_terminate ) { while ( !content_length && !zm_terminate ) {
Debug(4, "!content_length, ReadData"); Debug(4, "!content_length, ReadData");
buffer_len = ReadData( buffer ); buffer_len = ReadData( buffer );
if ( buffer_len < 0 ) if ( buffer_len < 0 ) {
{
Error( "Unable to read content" ); Error( "Unable to read content" );
return( -1 ); return( -1 );
} }

View File

@ -53,12 +53,13 @@ public:
int Disconnect(); int Disconnect();
int SendRequest(); int SendRequest();
int ReadData( Buffer &buffer, unsigned int bytes_expected=0 ); int ReadData( Buffer &buffer, unsigned int bytes_expected=0 );
int GetData();
int GetResponse(); int GetResponse();
int PreCapture(); int PreCapture();
int Capture( Image &image ); int Capture( Image &image );
int PostCapture(); int PostCapture();
int CaptureAndRecord( Image &image, timeval recording, char* event_directory ) {return 0;}; 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 #endif // ZM_REMOTE_CAMERA_HTTP_H