I'm trying to make my PHP server a bit more efficient. I've built an object named Client
which contains the connected client (which has an open socket connection with the server) information such as name
, id
etc.
For now I have one array of socket connections, and one array of Client objects. When I'm referring a connection, I'm searching inside my Client array to find the right client who matches this connection. It works great, but it's a bit inefficient.. For small amount of clients in the server you don't feel it, but I'm afraid that if I'll have thousands of connection it will slow down the server.
As a solution I thought about 2 dimensional array, but I have a logic problem designing it.
Can I do something like this:
$clients = array();
$temp = array($newsock, new Client());
$clients[] = $temp;
I want my $clients[]
to be the socket and the $clients[][]
to be the client object. In each row of $client
I will have only $client[$index][0]
which will be my client object for that connection. Will I be able to send this to the socket_select()
function?
You say that you have within your client object an id
attribute. Why not use that id
as the key for both arrays?
You might even be able to hold the connection and the client object in one array, each in one object under the same key I talked about before - the clients id
.
In any case, wherever you decide to store your clients connection object, you will be able to pass it to all the relevant socket functions -
socket_select();
socket_accept();
socket_write();
With regard to the efficiency of your server, I implemented some forking for broadcasting data to large amounts of clients (all of them in the example of a chat server).
This is the implementation that I used for forking the broadcasts -
function broadcastData($socketArray, $data){
global $db;
$pid = pcntl_fork();
if($pid == -1) {
// Something went wrong (handle errors here)
// Log error, email the admin, pull emergency stop, etc...
echo "Could not fork()!!";
} elseif($pid == 0) {
// This part is only executed in the child
foreach($socketArray AS $socket) {
// There's more happening here but the essence is this
socket_write($socket,$msg,strlen($msg));
// TODO : Consider additional forking here for each client.
}
// This is where the signal is fired
exit(0);
}
// The child process is now occupying the same database
// connection as its parent (in my case mysql). We have to
// reinitialize the parent's DB connection in order to continue using it.
$db = dbEngine::factory(_dbEngine);
}
The code above was lifted from a previous question of mine (that was self answered).
Terminating zombie child processes forked from socket server
Perhaps it might assist you if you chose to start forking processes.