Anyone here have experience with php and sockets?
I'm trying to fix a bug in my project, flashmog. More on that bug here.
I'm thinking that I should try to read ALL the available data on a socket each time data becomes available. I want to avoid buffer overflow risks so I have a MAX_BUFFER_SIZE constant that I check.
What's puzzling me is that the socket_read returns an empty string when people disconnect. socket_read also returns an empty string when there is no more data to read on a socket. I'm wondering if I might somehow confuse the two situations.
Also, I was wondering if someone might let me know if anything looks dangerous about this loop. This little loop will live inside the main socket server loop that calls [man]socket_select[/man] on a list of sockets.
// socket_select has indicated that $socket is one of the sockets with data available
$data = socket_read($socket, SOCKET_READ_LENGTH, PHP_BINARY_READ);
if ($data === '') {
// handle user disconnect
}
if ((strlen($data) + strlen($buffer)) > MAX_BUFFER_SIZE) {
// dump the data, send error
}
// LOOP...want to make sure i read all the data available
while($data = socket_read($socket, SOCKET_READ_LENGTH, PHP_BINARY_READ)) {
if ((strlen($data) + strlen($buffer)) > MAX_BUFFER_SIZE) {
// dump the data, send error
}
}
Any insights are welcome.