nodrock / redtamarin

Automatically exported from code.google.com/p/redtamarin
Other
0 stars 1 forks source link

bug in recvall #104

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
in

the utility function recvall() has a bug

here the fix

var recvall2:Function = function( socket:int, bytes:ByteArray, len:int = 8192, 
flags:int = 0 ):int
{
    var total:uint = 0; // how many bytes we received
    var n:int;
    var b:ByteArray = new ByteArray();

    var run:Boolean = true;
    while( run )
    {
        b.clear();
        n = recv( socket, b, len, flags );
        if( n == -1 ) { run = false; break; }
        bytes.writeBytes( b );
        total += n;
        if( n == 0 ) { run = false; break; }
    }

    b.clear();

    if( n < 0 )
    {
        return -1; //failure
    }

    return total; // number of bytes actually received
}

the loop need to check on "bytes recv zero"
and not "bytes recv smaller than buffer"

eg.
//if( n < len ) { run = false; break; }  // bad
if( n == 0 ) { run = false; break; }     // good

if you trace( "n = " + n );
on getting an HTTML page you get something like

n = 1448
n = 1448
n = 2896
n = 1448
n = 1448
n = 1448
n = 2896
n = 1448
n = 1448
n = 1448
n = 1448
n = 1448
n = 1448
n = 2896
n = 1534
n = 0

even if the buffer is max=8192 bytes
depending on network settings and other MTU window
we can recevie s,aller chunk than the buffer
which is what create the bug

the real indicator of "all is received" is when we receive zero bytes

Original issue reported on code.google.com by zwetan on 26 Mar 2015 at 8:38