[ create a new paste ] login | about

Link: http://codepad.org/CEn2eXOp    [ raw code | output | fork ]

PHP, pasted on Jan 15:
function server($mud_clients)
{//set the time limit to indefinite execution
//From php documentation: Set the number of seconds a script is allowed to run. If this is reached, the script returns a fatal error. The default limit is 30 seconds or, if it exists, the max_execution_time value defined in the php.ini. 
set_time_limit (0);
//The error_reporting() function sets the error_reporting directive at runtime. PHP has many levels of errors, using this function sets that level for the duration (runtime) of your script. If the optional level is not set, error_reporting() will just return the current error reporting level. 
error_reporting(E_ERROR);
// We will listen on the localhost
$local = '127.0.0.1';
//need to take port from database
$listen_port = 9090;
$connections_max = 18;
// Creates and returns a socket resource, also referred to as an endpoint of communication. A typical network connection is made up of 2 sockets, one performing the role of the client, and another performing the role of the server.
//In this case it's the server socket 
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
//Binds the name given in address to the socket described by socket. This has to be done before a connection is be established using socket_connect() or socket_listen()
socket_bind($sock, $local, $listen_port) or emergency(LOG_ERR, 'Problem Binding Socket');
//After the socket socket has been created using socket_create() and bound to a name with socket_bind(), it may be told to listen for incoming connections on socket. 
socket_listen($sock);
// This is essentially the main game loop, which will run until we want to shut down
while (true)
        {
        $read[0]=$sock;
        //check to make sure that the array has a socket defined for each connection, plus essentially one spare.

        for ($counter = 0; $counter < $connections_max; $counter++)
                {
                if ($mud_client[$counter]['socket']  != null)
                                $read[$counter + 1]=$mud_client[$counter]['socket'];
                }


        //socket_select() accepts arrays of sockets and waits for them to change status. Those coming with BSD sockets background will recognize that those socket resource arrays are in fact the so-called file descriptor sets. Three independent arrays of socket resources are watched. 
        $should_add=socket_select($read, $write = NULL, $except = NULL, $tv_sec = NULL);
        //$should_add=socket_select($from_client,null,null,null);
        // if a new connection is being made add it to the client array 
        if (in_array($sock, $read))
                {
                for ($counter = 0; $counter < $connections_max; $counter++)
                        {
                        if ($client[$counter]['socket'] == null)
                                {
                                $client[$counter]['socket'] = socket_accept($sock);
                                break;
                                }
                        elseif ($counter == $connections_max - 1)
                                        print ("too many clients");
                        }
                if (--$should_add <= 0)
                                continue;
                } // end if in_array
        // If a client is trying to write - handle it now
        for ($counter = 0; $counter < $connections_max; $counter++) // for each client
                {
                if (in_array($client[$counter]['socket'] , $read))
                        {
                $input = socket_read($client[$counter]['socket'] , 512);
                if ($input == null)
                        {
                        // Zero length string meaning disconnected
                        unset($client[$counter]);
                        }
                $n = trim($input);
                if ($input == 'exit')
                        {
                        // requested disconnect
                        socket_close($client[$i]['socket']);
                        }
                elseif ($input)
                        {
                        // strip white spaces and write back to user
                        process_commands($input);
                        $output = ereg_replace("[ \t\n\r]","",$input).chr(0);
                        socket_write($client[$counter]['socket'],$output);
                        }
                        }
                else    {
                        // Close the socket
                        socket_close($client[$counter]['socket']);
                        unset($client[$counter]);
                        }
                }
        } // end while
// Close the master sockets
socket_close($sock);
}


Output:
function server($mud_clients)
{//set the time limit to indefinite execution
//From php documentation: Set the number of seconds a script is allowed to run. If this is reached, the script returns a fatal error. The default limit is 30 seconds or, if it exists, the max_execution_time value defined in the php.ini. 
set_time_limit (0);
//The error_reporting() function sets the error_reporting directive at runtime. PHP has many levels of errors, using this function sets that level for the duration (runtime) of your script. If the optional level is not set, error_reporting() will just return the current error reporting level. 
error_reporting(E_ERROR);
// We will listen on the localhost
$local = '127.0.0.1';
//need to take port from database
$listen_port = 9090;
$connections_max = 18;
// Creates and returns a socket resource, also referred to as an endpoint of communication. A typical network connection is made up of 2 sockets, one performing the role of the client, and another performing the role of the server.
//In this case it's the server socket 
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
//Binds the name given in address to the socket described by socket. This has to be done before a connection is be established using socket_connect() or socket_listen()
socket_bind($sock, $local, $listen_port) or emergency(LOG_ERR, 'Problem Binding Socket');
//After the socket socket has been created using socket_create() and bound to a name with socket_bind(), it may be told to listen for incoming connections on socket. 
socket_listen($sock);
// This is essentially the main game loop, which will run until we want to shut down
while (true)
        {
        $read[0]=$sock;
        //check to make sure that the array has a socket defined for each connection, plus essentially one spare.

        for ($counter = 0; $counter < $connections_max; $counter++)
                {
                if ($mud_client[$counter]['socket']  != null)
                                $read[$counter + 1]=$mud_client[$counter]['socket'];
                }


        //socket_select() accepts arrays of sockets and waits for them to change status. Those coming with BSD sockets background will recognize that those socket resource arrays are in fact the so-called file descriptor sets. Three independent arrays of socket resources are watched. 
        $should_add=socket_select($read, $write = NULL, $except = NULL, $tv_sec = NULL);
        //$should_add=socket_select($from_client,null,null,null);
        // if a new connection is being made add it to the client array 
        if (in_array($sock, $read))
                {
                for ($counter = 0; $counter < $connections_max; $counter++)
                        {
                        if ($client[$counter]['socket'] == null)
                                {
                                $client[$counter]['socket'] = socket_accept($sock);
                                break;
                                }
                        elseif ($counter == $connections_max - 1)
                                        print ("too many clients");
                        }
                if (--$should_add <= 0)
                                continue;
                } // end if in_array
        // If a client is trying to write - handle it now
        for ($counter = 0; $counter < $connections_max; $counter++) // for each client
                {
                if (in_array($client[$counter]['socket'] , $read))
                        {
                $input = socket_read($client[$counter]['socket'] , 512);
                if ($input == null)
                        {
                        // Zero length string meaning disconnected
                        unset($client[$counter]);
                        }
                $n = trim($input);
                if ($input == 'exit')
                        {
                        // requested disconnect
                        socket_close($client[$i]['socket']);
                        }
                elseif ($input)
                        {
                        // strip white spaces and write back to user
                        process_commands($input);
                        $output = ereg_replace("[ \t\n\r]","",$input).chr(0);
                        socket_write($client[$counter]['socket'],$output);
                        }
                        }
                else    {
                        // Close the socket
                        socket_close($client[$counter]['socket']);
                        unset($client[$counter]);
                        }
                }
        } // end while
// Close the master sockets
socket_close($sock);
}


Create a new paste based on this one


Comments: