cvs: pear /Net_GameServerQuery/GameServerQuery/Protocol HalfLife.php

From: Date: Tue, 18 Jan 2005 04:59:52 +0000
Subject: cvs: pear /Net_GameServerQuery/GameServerQuery/Protocol HalfLife.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-28653@lists.php.net to get a copy of this message
aidan Mon Jan 17 23:59:52 2005 EDT Modified files: /pear/Net_GameServerQuery/GameServerQuery/Protocol HalfLife.php Log: Updated with new protocol procedures. Added multipacketjoin method. http://cvs.php.net/diff.php/pear/Net_GameServerQuery/GameServerQuery/Protocol/HalfLife.php?r1=1.22&r2=1.23&ty=u Index: pear/Net_GameServerQuery/GameServerQuery/Protocol/HalfLife.php diff -u pear/Net_GameServerQuery/GameServerQuery/Protocol/HalfLife.php:1.22 pear/Net_GameServerQuery/GameServerQuery/Protocol/HalfLife.php:1.23 --- pear/Net_GameServerQuery/GameServerQuery/Protocol/HalfLife.php:1.22 Sat Jan 15 09:27:27 2005 +++ pear/Net_GameServerQuery/GameServerQuery/Protocol/HalfLife.php Mon Jan 17 23:59:51 2005 @@ -16,7 +16,7 @@ // | Tom Buskens <ortega@php.net> | // +----------------------------------------------------------------------+ // -// $Id: HalfLife.php,v 1.22 2005/01/15 14:27:27 aidan Exp $ +// $Id: HalfLife.php,v 1.23 2005/01/18 04:59:51 aidan Exp $ require_once NET_GAMESERVERQUERY_BASE . 'Protocol.php'; @@ -29,33 +29,33 @@ * @package Net_GameServerQuery * @author Aidan Lister <aidan@php.net> * @author Tom Buskens <ortega@php.net> - * @version $Revision: 1.22 $ + * @version $Revision: 1.23 $ */ class Net_GameServerQuery_Protocol_HalfLife extends Net_GameServerQuery_Protocol { /** * Status */ - protected function infostring(&$response, &$result) + protected function infostring(&$buffer, &$result) { - if ($response->readInt32() !== -1) { + if ($buffer->readInt32() !== -1) { return false; } - if ($response->readString() !== 'infostringresponse') { + if ($buffer->readString() !== 'infostringresponse') { return false; } - if ($response->read() !== '\\') { + if ($buffer->read() !== '\\') { return false; } - if ($response->readLast() !== "\x0") { + if ($buffer->readLast() !== "\x0") { return false; } - while ($response->buffer()) { - $result->add($response->readString('\\'), $response->readString('\\')); + while (!$buffer->is_empty()) { + $result->add($buffer->readString('\\'), $buffer->readString('\\')); } return $result->fetch(); @@ -65,23 +65,23 @@ /** * Players */ - protected function players(&$response, &$result) + protected function players(&$buffer, &$result) { - if ($response->readInt32() !== -1) { + if ($buffer->readInt32() !== -1) { return false; } - if ($response->read() !== 'D') { + if ($buffer->read() !== 'D') { return false; } - $result->addMeta('count', $response->readInt8()); + $result->addMeta('count', $buffer->readInt8()); - while ($response->buffer()) { - $result->addPlayer('id', $response->readInt8()); - $result->addPlayer('name', $response->readString()); - $result->addPlayer('score', $response->readInt32()); - $result->addPlayer('time', $response->readFloat32()); + while (!$buffer->is_empty()) { + $result->addPlayer('id', $buffer->readInt8()); + $result->addPlayer('name', $buffer->readString()); + $result->addPlayer('score', $buffer->readInt32()); + $result->addPlayer('time', $buffer->readFloat32()); } return $result->fetch(); @@ -91,40 +91,45 @@ /** * Rules */ - protected function rules(&$response, &$result) + protected function rules(&$buffer, &$result) { - // Convert multiple packets into a single response - // Extract the packet order from each packet and order by that - if (is_array($response->getResponse())) { - $r = $response->getResponse(); - $packets = array(); - foreach ($r as $packet) { - $key = substr(bin2hex($packet{8}), 0, 1); - $packet = substr($packet, 9); - $packets[$key] = $packet; - } - - $r = implode('', $packets); - $response->setResponse($r); - } - - // Standard parsing - if ($response->readInt32() !== -1) { + if ($buffer->readInt32() !== -1) { return false; } - if ($response->read() !== 'E') { + if ($buffer->read() !== 'E') { return false; } - $result->addMeta('count', $response->readInt16()); + $result->addMeta('count', $buffer->readInt16()); - while ($response->buffer()) { - $result->add($response->readString(), $response->readString()); + while (!$buffer->is_empty()) { + $result->add($buffer->readString(), $buffer->readString()); } return $result->fetch(); } + + + /** + * Join multiple packets + */ + protected function multipacketjoin($packets) + { + $result = array(); + foreach ($packets as $packet) { + // Get the low nibble of the 9th bit + $key = substr(bin2hex($packet{8}), 0, 1); + + // Strip whole header + $packet = substr($packet, 9); + + // Order by low nibble + $result[$key] = $packet; + } + + return implode('', $result); + } }

« previous php.pear.cvs (#28653) next »