So I have a socket server that is interacting with Flash. I'm having this problem where all of my messages are arriving in clumps rather than being distributed evenly over time. It results in very jerky movements in a multiplayer game I've been writing. I put a trace function in my socket-writing code to report the number of bytes written by socket_write:
51 bytes sent at 1242146737.69
43 bytes sent at 1242146737.72
43 bytes sent at 1242146737.76
43 bytes sent at 1242146737.79
43 bytes sent at 1242146737.82
43 bytes sent at 1242146737.86
43 bytes sent at 1242146737.89
43 bytes sent at 1242146737.92
52 bytes sent at 1242146737.96
51 bytes sent at 1242146739.46
43 bytes sent at 1242146739.49
44 bytes sent at 1242146739.52
44 bytes sent at 1242146739.56
44 bytes sent at 1242146739.59
44 bytes sent at 1242146739.62
44 bytes sent at 1242146739.66
53 bytes sent at 1242146739.69
This looks great -- 40-50 bytes written every 30 or 40 milliseconds would correspond almost exactly to a complete 'movePlayer' RPC being sent 25 or 30 times per second.
HOWEVER in Flash, the function that fires to indicate that data has arrived is firing only once every 150-200 milliseconds -- about 5-7 frames per second and when it does, the data on the socket buffer contains several 'movePlayer' instructions which all fire at basically the same time:
* socketDataHandler running at 1242153515.703
client.movePlayer running at 1242153515.703
client.movePlayer running at 1242153515.703
client.movePlayer running at 1242153515.703
client.movePlayer running at 1242153515.703
client.movePlayer running at 1242153515.703
* socketDataHandler running at 1242153515.89
client.movePlayer running at 1242153515.89
client.movePlayer running at 1242153515.906
client.movePlayer running at 1242153515.906
client.movePlayer running at 1242153515.906
client.movePlayer running at 1242153515.906
client.movePlayer running at 1242153515.906
* socketDataHandler running at 1242153516.093
client.movePlayer running at 1242153516.109
client.movePlayer running at 1242153516.109
client.movePlayer running at 1242153516.109
client.movePlayer running at 1242153516.109
client.movePlayer running at 1242153516.109
Is there some PHP or OS-level functionality that is grouping my tiny packets for the sake of network efficiency? I'm concerned that either *nix or PHP is implementing Nagle's algorithm or something. Can I turn this off? I need my packets to arrive ASAP after they are written to the socket.
Is there any way to detect whether this is happening on my server or happening in Flash? The server happens to be a Mac in this case buy my code should also run on Linux. I'm wondering if a packet-sniffing program accurately timestamps the packets?