I am trying to write a large amount of data (~250K) to a stream in a non-blocking fashion.
Abstracting away the complexities and object structure, this is what I am running:
$fp = fsockopen('host', 80);
socket_set_blocking( $fp, false );
fwrite( $fp, $string, $length ); // Where $string is a 250K string
However the data doesn't all get written. Assuming this was PHP's write buffer coming into play, I set stream_set_write_buffer( $fp, 0 ) but that didn't solve the problem either.
I broke my fwrite into chunks of 4096B - and it looks like the client sends 3 complete batches (of 4096 bytes) and ~1500B of the fourth batch. Any and all successive calls to fwrite return 0 bytes written.
Does anyone have any idea how I can queue this data to all be sent out in a non-blocking fashion? If I remove socket_set_blocking( $fp, false ); - it all works fine. So clearly it's an issue with running it asynchronously.
What are your thoughts? Would the sockets extension help here at all? Does it handle buffers differently?
Note: I am intentionally writing this socket transport layer to avoid using curl for various reasons. Using curl_multi() is not an option.
socket_select()to find out whether the socket is ready for writing.