PHP Flash Policy Server

This is a simple Flash Policy Server written in PHP which provides for multiplexing multiple simultanious connections.

To run this program as a daemon use nohup, like this.

nohup /usr/bin/php flash-policy-server.php 1>/var/log/flash-policy-server.log 2>&1 &&

If stuck with .NET you can try this one in C#.

#!/usr/bin/php
<?php
/**
    PHP Based Flash Policy Server
    
    @author Edoceo, http://edoceo.com/
    @copyright 2010 Edoceo
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see .

*/

// Set time limit to indefinite execution
error_reporting(E_ALL | E_STRICT);
set_time_limit(0);

$addr = 0;
$port = 843;

// Create a TCP Stream socket
$svr = socket_create(AF_INET, SOCK_STREAM, 0);
// socket_set_option($svr, SOL_SOCKET, SO_KEEPALIVE, 1);
socket_set_option($svr, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($svr, $addr, $port) or die('Could not bind to address');
socket_listen($svr);

// Array that will hold client information

$client_max = 10;
$client_set = array();

// Loop continuously
while (true) {

    $recv = array($svr); // Server
    $send = array();
    $excp = null; // array();

    foreach ($client_set as $x) {
        $recv[] = $x['conn'];
        $send[] = $x['conn'];
    }
    echo "Reading: " . (count($recv) - 1) . " clients\n";
    // Setup clients listen socket for reading
    // for ($i = 0; $i < $max_clients; $i++) {
    //     if (!empty($client[$i]['sock'] != null)
    //         $read[$i + 1] = $client[$i]['sock'] ;
    // }
    
    // Set up a blocking call to socket_select()
    socket_select($recv,$send,$excp,null);
    echo count($recv) . " readers, " . count($send) . " senders, " . count($excp) . " exceptions\n";
    
    // If a new connection is being made add it to the client array
    if (in_array($svr,$recv)) { 
        $cli = socket_accept($svr);
        if (count($client_set) > $client_max) {
            echo "Too many dicks on the dance floor\n";
            socket_close($cli);
            continue;
        }
        $addr = $port = null;
        if (!socket_getpeername($cli,$addr,$port)) {
            echo "Cannot getpeername()\n";
            socket_close($cli);
            continue;
        }
        $client_set[ "$addr:$port" ] = array(
            'conn' => $cli,
            'addr' => $addr,
            'port' => $port,
            'recv' => trim(socket_read($cli,32,PHP_BINARY_READ)),
            'send' => '',
        );
    }

    // Process Clients Here
    foreach ($client_set as $k=>$cli) {
        // Readers
        if (in_array($cli['conn'],$recv)) {
            // Add to the $cli recv Buffer
            $buf = trim(socket_read($cli['conn'],32,PHP_BINARY_READ));
            // Zero length string meaning disconnected
            if ($buf == null) {
                unset($client_set[ $k ]);
            } else {
                $client_set[$k]['recv'].= $buf;
            }
        }
        // Writers
        if (in_array($cli['conn'],$send)) {
            if ($cli['recv'] = '') {
                echo "Writing Response\n";
                socket_write($cli['conn'],$cli['send']);
                socket_write($cli['conn'],"\0");
                socket_close($cli['conn']);
                unset($client_set[$k]);
            }
        }
    }
}
?>