<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://wiki.staging.zoneminder.com/index.php?action=history&amp;feed=atom&amp;title=1.24.2_Patches</id>
	<title>1.24.2 Patches - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://wiki.staging.zoneminder.com/index.php?action=history&amp;feed=atom&amp;title=1.24.2_Patches"/>
	<link rel="alternate" type="text/html" href="http://wiki.staging.zoneminder.com/index.php?title=1.24.2_Patches&amp;action=history"/>
	<updated>2026-04-19T19:19:11Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.37.1</generator>
	<entry>
		<id>http://wiki.staging.zoneminder.com/index.php?title=1.24.2_Patches&amp;diff=3656&amp;oldid=prev</id>
		<title>Genanr: /* Patch to fix Incomplete frame errors in zmf.cpp */</title>
		<link rel="alternate" type="text/html" href="http://wiki.staging.zoneminder.com/index.php?title=1.24.2_Patches&amp;diff=3656&amp;oldid=prev"/>
		<updated>2010-02-18T22:37:09Z</updated>

		<summary type="html">&lt;p&gt;&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Patch to fix Incomplete frame errors in zmf.cpp&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Fix Incomplete Image errors in zmf ==&lt;br /&gt;
&lt;br /&gt;
This patch changes the socket reader in zmf from a single read, to a loop read.  Incomplete reads would be reported even though the image writer wrote the whole image to the socket.  The problem was when the read went to read the image frame from the socket, all the data had not yet been written to the socket by the writer, so the reader thought there was a problem.  The loop reads from the socket until a full image frame is read, or there is an error.&lt;br /&gt;
&lt;br /&gt;
NOTE: to patch from the top level ZM source directory type : patch -lp1 &amp;lt; patchname&lt;br /&gt;
Be sure to use the -l (ell flag, ignore whitespace), otherwise it may not patch cleanly due to tabs in the original source file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
--- zm/src/zmf.cpp      2010-01-04 10:52:00.000000000 -0600&lt;br /&gt;
+++ zm-andy/src/zmf.cpp 2010-02-18 16:11:18.000000000 -0600&lt;br /&gt;
@@ -231,16 +231,39 @@&lt;br /&gt;
                 }&lt;br /&gt;
                 Debug( 1, &amp;quot;Read frame header, expecting %ld bytes of image&amp;quot;, frame_header.image_length );&lt;br /&gt;
                 static unsigned char image_data[ZM_MAX_IMAGE_SIZE];&lt;br /&gt;
-                n_bytes = read( sd, image_data, frame_header.image_length );&lt;br /&gt;
-                if ( n_bytes != (ssize_t)frame_header.image_length )&lt;br /&gt;
+&lt;br /&gt;
+                // Read for pipe and loop until bytes expected have been read or an error occures&lt;br /&gt;
+                int bytes_read = 0;&lt;br /&gt;
+                do&lt;br /&gt;
                 {&lt;br /&gt;
-                        if ( n_bytes &amp;lt; 0 )&lt;br /&gt;
+                        n_bytes = read( sd, image_data+bytes_read, frame_header.image_length-bytes_read );&lt;br /&gt;
+                        if (n_bytes &amp;lt; 0) break; // break on error&lt;br /&gt;
+                        if (n_bytes &amp;lt; frame_header.image_length)&lt;br /&gt;
                         {&lt;br /&gt;
-                                Error( &amp;quot;Can&amp;#039;t read frame image data: %s&amp;quot;, strerror(errno) );&lt;br /&gt;
+                                // print some informational messages &lt;br /&gt;
+                                if (bytes_read == 0)&lt;br /&gt;
+                                {&lt;br /&gt;
+                                        Warning(&amp;quot;Image read : Short read %d bytes of %d expected bytes&amp;quot;,n_bytes,frame_header.image_length);&lt;br /&gt;
+                                }&lt;br /&gt;
+                                else if (bytes_read+n_bytes == frame_header.image_length)&lt;br /&gt;
+                                {&lt;br /&gt;
+                                        Warning(&amp;quot;Image read : Read rest of short read: %d bytes read total of %d bytes&amp;quot;,n_bytes,frame_header.image_length);&lt;br /&gt;
+                                }&lt;br /&gt;
+                                else&lt;br /&gt;
+                                {&lt;br /&gt;
+                                        Warning(&amp;quot;Image read : continuing, read %d bytes (%d so far)&amp;quot;, n_bytes, bytes_read+n_bytes);&lt;br /&gt;
+                                }&lt;br /&gt;
                         }&lt;br /&gt;
-                        else if ( n_bytes &amp;gt; 0 )&lt;br /&gt;
+                        bytes_read+= n_bytes;&lt;br /&gt;
+                } while (n_bytes&amp;gt;0 &amp;amp;&amp;amp; (bytes_read &amp;lt; (ssize_t)frame_header.image_length) );&lt;br /&gt;
+&lt;br /&gt;
+                // Print errors if there was a problem&lt;br /&gt;
+                if ( n_bytes &amp;lt; 1 )&lt;br /&gt;
+                {&lt;br /&gt;
+                        Error( &amp;quot;Only read %d bytes of %d\n&amp;quot;, bytes_read, frame_header.image_length);&lt;br /&gt;
+                        if ( n_bytes &amp;lt; 0 )&lt;br /&gt;
                         {&lt;br /&gt;
-                                Error( &amp;quot;Incomplete read of frame image data, %d bytes only&amp;quot;, n_bytes );&lt;br /&gt;
+                                Error( &amp;quot;Can&amp;#039;t read frame image data: %s&amp;quot;, strerror(errno) );&lt;br /&gt;
                         }&lt;br /&gt;
                         else&lt;br /&gt;
                         {&lt;br /&gt;
@@ -249,16 +272,18 @@&lt;br /&gt;
                         ReopenSocket( sd, monitor-&amp;gt;Id() );&lt;br /&gt;
                         continue;&lt;br /&gt;
                 }&lt;br /&gt;
+&lt;br /&gt;
                 static char subpath[PATH_MAX] = &amp;quot;&amp;quot;;&lt;br /&gt;
-        if ( config.use_deep_storage )&lt;br /&gt;
-        {&lt;br /&gt;
-            struct tm *time = localtime( &amp;amp;frame_header.event_time );&lt;br /&gt;
-            snprintf( subpath, sizeof(subpath), &amp;quot;%02d/%02d/%02d/%02d/%02d/%02d&amp;quot;, time-&amp;gt;tm_year-100, time-&amp;gt;tm_mon+1, time-&amp;gt;tm_mday, time-&amp;gt;tm_hour, time-&amp;gt;tm_min, time-&amp;gt;tm_sec );&lt;br /&gt;
-        }&lt;br /&gt;
-        else&lt;br /&gt;
-        {&lt;br /&gt;
-            snprintf( subpath, sizeof(subpath), &amp;quot;%ld&amp;quot;, frame_header.event_id );&lt;br /&gt;
-        }&lt;br /&gt;
+                if ( config.use_deep_storage )&lt;br /&gt;
+                {&lt;br /&gt;
+                    struct tm *time = localtime( &amp;amp;frame_header.event_time );&lt;br /&gt;
+                    snprintf( subpath, sizeof(subpath), &amp;quot;%02d/%02d/%02d/%02d/%02d/%02d&amp;quot;, time-&amp;gt;tm_year-100, time-&amp;gt;tm_mon+1, time-&amp;gt;tm_mday, time-&amp;gt;tm_hour, time-&amp;gt;tm_min, time-&amp;gt;tm_sec );&lt;br /&gt;
+                }&lt;br /&gt;
+                else&lt;br /&gt;
+                {&lt;br /&gt;
+                    snprintf( subpath, sizeof(subpath), &amp;quot;%ld&amp;quot;, frame_header.event_id );&lt;br /&gt;
+                }&lt;br /&gt;
+&lt;br /&gt;
                 static char path[PATH_MAX] = &amp;quot;&amp;quot;;&lt;br /&gt;
                 snprintf( path, sizeof(path), frame_header.alarm_frame?anal_path:capt_path, subpath, frame_header.frame_id );&lt;br /&gt;
                 Debug( 1, &amp;quot;Got image, writing to %s&amp;quot;, path );&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Andy (&amp;quot;genanr&amp;quot; forum member).&lt;/div&gt;</summary>
		<author><name>Genanr</name></author>
	</entry>
</feed>