Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NativeSocket's read() doesn't work with jobs larger than 8192 bytes #4

Closed
ptrmcrthr opened this issue Jul 28, 2009 · 3 comments
Closed

Comments

@ptrmcrthr
Copy link

fread() only reads up to 8192 bytes, causing an exception to be thrown in Pheanstalk_Command->dispatchCommand() -- it's expecting a CRLF but there's remaining job data.

thanks for the client.

@pda
Copy link
Collaborator

pda commented Aug 5, 2009

Thanks for the report.
Feel free to unit test, fork & fix if you have time, otherwise I'll fix it when I get a chance.
I've just moved house and have nothing but iPhone 3G tethering for internet access :)

Cheers!
Paul

@anusoft
Copy link

anusoft commented Feb 11, 2010

This is a fix, sorry I've just notice this post -*-

/* (non-phpdoc)
 * @see Pheanstalk_Socket::write()
 */
public function write($data)
{
    //return fwrite($this->_socket, $data);

    $bytes_to_write = strlen($data);
    $bytes_written = 0;

    while ( $bytes_written < $bytes_to_write )
    {
        if ( $bytes_written == 0 ) {
            $rv = fwrite($this->_socket, $data);
        } else {
            $rv = fwrite($this->_socket, substr($data, $bytes_written));
        }

        if ( $rv === false || $rv == 0 )
        return( $bytes_written == 0 ? false : $bytes_written );

        $bytes_written += $rv;
    }

}

/* (non-phpdoc)
 * @see Pheanstalk_Socket::write()
 */
public function read($length)
{
    if ($length == 0) {
        return null;
    }
    if ($length <= 1024) {
        return fread($this->_socket, $length);
    }
    $length_to_read = 0;
    $data = '';
    for ($i = $length; $i > 0;$i = $i - 1024) {
        if ($i < 1024) {
            $data .= fread($this->_socket, $i);
        } else {
            $data .= fread($this->_socket, 1024);
        }
    }
    //echo $i;
    return $data;

}

@pda
Copy link
Collaborator

pda commented Mar 11, 2010

Thanks for the bug report, Peter, and the code anusoft. Sorry it's taken me a while to get to it.

I've actually found in PHP 5.2.10-2ubuntu6.4 nearly double 8192 is read on the first fread() when getting a job. I think this is probably because a prior call to fgets() buffers up to 8192 bytes and then only takes a short line from that buffer.

Anyway - I've written a test which demonstrates the bug, and updated NativeSocket based on anusoft's code.

I'd like to find time to write a nicer Pheanstalk_Socket implementation, perhaps using a select() loop.

Fixed in 436e3bb.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants