socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); echo "IP: " . ($ip = "0.0.0.0") . "\n"; echo "PORT: " . ($port = 27015) . "\n"; echo socket_bind($this->socket, $ip, $port) ? "Socket bound to $ip:$port\n" : "ERROR: Could not bind the socket to $ip:$port\n"; echo socket_listen($this->socket) ? "Listening...\n" : "ERROR: Could not listen...\n"; echo "Entering the main loop\n"; while (true) { $r = array($this->socket); $w = NULL; $e = NULL; if (socket_select($r, $w, $e, 0) > 0) { $client = new client(socket_accept($this->socket)); if ($client->auth()) { $this->clients[] = clone $client; echo "### A client was added! ###\n"; } else { $client = false; echo "ERROR: A new client failed to auth...\n"; } } foreach ($this->clients as $client) { if (($input = $client->get_input()) != false) { echo "<--" . $input . "\n"; } } usleep(1000); } } private function process_clients_pending() { } } $server = new server(); class client { var $socket; var $buffer; public function client($socket) { $this->socket = $socket; } public function auth() { return true; } public function get_input() { $r = array($this->socket); $w = NULL; $e = NULL; if (socket_select($r, $w, $e, 0) > 0) { @socket_recv($this->socket, $data, 65029, 0); $length = decoder::read_ushort($data); $opcode = decoder::read_ushort($data, 2); $message = substr($data, 4, $length); return "[$length][$opcode][$message]"; } else { return false; } } } class decoder { public function read_ushort($data, $offset = 0) { $b1 = ord(substr($data, $offset, 1)); $b2 = ord(substr($data, $offset+1, 1)); return ($b2*256) + $b1; } } ?>