} } elseif ($n == "TERM") { // Server termination requested socket_close($sock); rLog("Terminated server (requested by client #" . $i . ")"); exit; } elseif ($input) { // Strip whitespaces and write back to user // Respond to commands /*$output = ereg_replace("[ \t\n\r]","",$input).chr(0); socket_write($client[$i]['sock'],$output);*/ if ($n == "PING") { socket_write($client[$i]['sock'], "PONG" . chr(0)); } if ($n == "<policy-file-request/>") { rLog("Client #" . $i . " requested a policy file..."); $cdmp = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><cross-domain-policy xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://www.adobe.com/xml/schemas/PolicyFileSocket.xsd\"><allow-access-from domain=\"*\" to-ports=\"*\" secure=\"false\" /><site-control permitted-cross-domain-policies=\"master-only\" /></cross-domain-policy>"; socket_write($client[$i]['sock'], $cdmp . chr(0)); socket_close($client[$i]['sock']); unset($client[$i]); $cdmp = ""; } } } else { //if($client[$i]['sock']!=null){ // Close the socket //socket_close($client[$i]['sock']); //unset($client[$i]); //rLog("Disconnected(1) client #".$i); //} }
public final function start() { // Create socket $sock = socket_create(AF_INET, SOCK_STREAM, 0) or die("[" . date('Y-m-d H:i:s') . "] Could not create socket\n"); socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1); // Bind to socket socket_bind($sock, $this->host, $this->port) or die("[" . date('Y-m-d H:i:s') . "] Could not bind to socket\n"); // Start listening socket_listen($sock) or die("[" . date('Y-m-d H:i:s') . "] Could not set up socket listener\n"); rLog("Server started at " . $host . ":" . $port); // Server loop while (true) { socket_set_nonblock($sock); // Setup clients listen socket for reading $read[0] = $sock; for ($i = 0; $i < $this->max_clients; $i++) { if ($this->clients[$i]->sock != null) { $read[$i + 1] = $this->clients[$i]->sock; $this->onTick($this->clients[$i]); } } // Set up a blocking call to socket_select() $ready = socket_select($read, $write = NULL, $except = NULL, $tv_sec = 0, $tv_usec = 10000); // If a new connection is being made add it to the clients array if (in_array($sock, $read)) { for ($i = 0; $i < $this->max_clients; $i++) { if ($this->clients[$i]->sock == null) { $this->clients[$i] = new $this->client_object_name(); if (($this->clients[$i]->sock = socket_accept($sock)) < 0) { rLog("socket_accept() failed: " . socket_strerror($this->clients[$i]->sock)); } else { rLog("Client #" . $i . " connected"); $this->clients[$i]->id = $i; $this->clients[$i]->server =& $this; $this->clients[$i]->host = socket_getpeername($this->clients[$i]->sock); } break; } elseif ($i == $this->max_clients - 1) { rLog("Too many clients"); } } if (--$ready <= 0) { continue; } } for ($i = 0; $i < $this->max_clients; $i++) { if (in_array($this->clients[$i]->sock, $read)) { $input = socket_read($this->clients[$i]->sock, 1024); if ($input == null) { unset($this->clients[$i]); continue; } $n = trim($input); if ($n == '<policy-file-request/>') { socket_write($this->clients[$i]->sock, $this->FLASH_POLICY_FILE); } else { if (!$this->clients[$i]->upgraded && substr($n, 0, 4) == 'GET ') { $this->doHandshake($this->clients[$i], $n); } else { $this->processFrame($this->clients[$i], $n); } } } } } // Close the master sockets socket_close($sock); }
// ** Place message logic ******************************* // // ****************************************************** // $ack = call_user_func(array($cls, $method), $data); // ****************************************************** // // ** End message logic ********************************* // // ****************************************************** // @socket_write($client, $ack, strlen($ack)); // @socket_close($clients[$key]); // unset($clients[$key]); $request++; rLog('socket_write() client #' . $key . ' Request:' . $request . ' Error:' . socket_strerror(socket_last_error($sock))); unset($ack); gc_collect_cycles(); } } catch (Exception $e) { rLog($e->getMessage()); } } // unset($clients); } socket_close($sock); //exit; // //// PHP SOCKET SERVER //error_reporting(E_ALL); //// Configuration variables // //$host = $argv[1]; //$port = $argv[2]; //$path = $argv[3]; //$class = $argv[4];