Note that there are some explanatory texts on larger screens.

plurals
  1. POImplementing packet timeout c++
    text
    copied!<p>For a computer networks project, we are writing an ftp server in c++ with different window protocols. We are having issues implementing a working timeout function. The gist of it is to set a timestamp to when a packet is transmitted, and if the "ack" isn't revived in a certain amount of time (say 2.5 milliseconds), retransmit the packet. We currently use the <code>clock_gettime()</code> function to get the current time and timestamp the packet. </p> <p>Here is the while loop we are having issues with:</p> <pre><code>while(packet_sync_array[prev].recieved ==0 &amp;&amp; t==0) { clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&amp;timeout2); currtime = timeout2.tv_nsec; cout &lt;&lt; "Is currtime: "&lt;&lt;currtime&lt;&lt; " - ptimeout " &lt;&lt; ptimeout &lt;&lt; " = " &lt;&lt; (currtime - ptimeout) &lt;&lt; " &gt; " &lt;&lt; (connection_parameters-&gt;timeoutInterval)*1000 &lt;&lt; "?" &lt;&lt; endl; if((currtime - ptimeout)&gt;((connection_parameters-&gt;timeoutInterval)*1000)) { t = 1; cout &lt;&lt; "Prev PACKET TIMEDOUT" &lt;&lt; endl; cout &lt;&lt; "Is currtime: "&lt;&lt;currtime&lt;&lt; " - ptimeout " &lt;&lt; ptimeout &lt;&lt; " = " &lt;&lt; (currtime - ptimeout) &lt;&lt; " &gt; " &lt;&lt; (connection_parameters-&gt;timeoutInterval)*1000 &lt;&lt; "?" &lt;&lt; endl; } } </code></pre> <p>Where <code>ptimeout</code> is the time that the packet was sent and <code>connection_parameters-&gt;timeoutInterval</code> is the timeout interval. The problem is that since <code>ptimeout</code> is a long integer representation of the time, sometimes it is a very large value (999715992 for example). This means that it will never be able to tell if the timeout has occurred, because the current time in nano seconds will be rest to 0 before the value gets large enough. </p> <p>Has anyone else dealt with these timing issues in c++ and have a possible solution?</p> <p>Thanks!</p> <p>EDIT:</p> <p>Thanks for the quick responses! I was able to get something figured out. Modifying to the while loop to check and see if the timeout+the timestamp would be larger than the allowed long intereger size let me see if the clock_gettime would get setback to zero before the comparison. Knowing this I checked if the current time > (timeout interval - (maximum long int val - time stamp)). This allows for a timeout of up to 1 second which should be plenty for the purposes of this problem. If anyone thinks they have a better solution, let me know! Thanks! Here is the code for those interested: </p> <pre><code>if(((999999998-ptimeout)&lt; (connection_parameters-&gt;timeoutInterval)*1000)&amp;&amp;(currtime - ptimeout)&lt;0){ if(currtime &gt; (((connection_parameters-&gt;timeoutInterval)*1000)-(999999998-ptimeout))){ t = 1; cout &lt;&lt; "this wrapped since " &lt;&lt; currtime &lt;&lt; " + " &lt;&lt; (connection_parameters-&gt;timeoutInterval)*1000 &lt;&lt; "is bigger than 999999999 and then timed out" &lt;&lt; endl; cout &lt;&lt; "curr time " &lt;&lt; currtime &lt;&lt; "is bigger than" &lt;&lt; endl; cout &lt;&lt; (connection_parameters-&gt;timeoutInterval)*1000 &lt;&lt; endl; cout &lt;&lt; "-" &lt;&lt; endl; cout &lt;&lt; (999999998-ptimeout) &lt;&lt; endl; cout &lt;&lt; "---------------------------" &lt;&lt; endl; cout &lt;&lt; (((connection_parameters-&gt;timeoutInterval)*1000)-(999999998-ptimeout)) &lt;&lt; endl; cout &lt;&lt; "Prev PACKET TIMEDOUT" &lt;&lt; endl; cout &lt;&lt; "Is currtime: "&lt;&lt;currtime&lt;&lt; " - ptimeout " &lt;&lt; ptimeout &lt;&lt; " = " &lt;&lt; (currtime - ptimeout) &lt;&lt; " &gt; " &lt;&lt; (connection_parameters-&gt;timeoutInterval)*1000 &lt;&lt; "?" &lt;&lt; endl; } } else if((currtime - ptimeout)&gt;((connection_parameters-&gt;timeoutInterval)*1000)){ t = 1; cout &lt;&lt; "Prev PACKET TIMEDOUT" &lt;&lt; endl; cout &lt;&lt; "Is currtime: "&lt;&lt;currtime&lt;&lt; " - ptimeout " &lt;&lt; ptimeout &lt;&lt; " = " &lt;&lt; (currtime - ptimeout) &lt;&lt; " &gt; " &lt;&lt; (connection_parameters-&gt;timeoutInterval)*1000 &lt;&lt; "?" &lt;&lt; endl; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload