1

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?

Lix
  • 47,311
  • 12
  • 103
  • 131
Asaf Nevo
  • 11,338
  • 23
  • 79
  • 154

1 Answers1

1

You say that you have within your client object an id attribute. Why not use that id as the key for both arrays?

  • Socket connections array
  • Client object array

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();
  • etc...

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.

Community
  • 1
  • 1
Lix
  • 47,311
  • 12
  • 103
  • 131
  • in general it could be a great solution to use the id as the index, the only problem that the id property of the Client object is sent by the client after the socket connection has been made, and it is not generated by the server. can you explain a bit more about the pcntl_fork() function? i'm not that familiar with unix and i didn't quite understand the explanation i've read in php.net – Asaf Nevo May 06 '12 at 03:19
  • `pcntl_fork()` will **only work on unix like systems**. It is the process of creating a child process separate from the parent process to improve performance - the child process can handle a specific request leaving the parent server to continue processing incoming data. – Lix May 06 '12 at 07:15
  • that's a good idea to give it a try... back to my question, what is you suggestion ? can i keep an array like this: array(socket, new Client()) and send it back to socket_select() function ? or maybe i should wait for the unique id (which will be received from client after the connection is made) and just then to connect the socket to the object ? – Asaf Nevo May 06 '12 at 12:40
  • You won't be able to pass that entire object to ` socket_select()`; Only the actual socket connection. If you can, then yes - use the unique ID after the connection is made and retrieve the connection by that ID. – Lix May 06 '12 at 12:42
  • it is far more efficient then to have a lookup every time right ? – Asaf Nevo May 06 '12 at 12:50
  • Oh yes - I use a lookup with the `array_search()` method only when a client has disconnected unexpectedly and I want to find out who it was. Other than that - where ever possible, I'll use it's ID. – Lix May 06 '12 at 12:52
  • and one more question, in the matter of saving space in the memory of the server, when i'm haveing an array like this: array( 6666 => "something"), the 6666 only represent the index number right ? not the number of space saved in the memory for that array ? – Asaf Nevo May 06 '12 at 12:53
  • Yep - that's just the index. If you want to see how much memory you are using for your script you can use the [`memory_get_usage()`](http://php.net/manual/en/function.memory-get-usage.php) function... – Lix May 06 '12 at 12:55