PHPDaemon\Utils\Binary::getStrWord PHP Method

getStrWord() public static method

Get word (2 bytes)
public static getStrWord ( &$p, boolean $l = false ) : string
$l boolean Little endian?
return string
    public static function getStrWord(&$p, $l = false)
    {
        $r = mb_orig_substr($p, 0, 2);
        $p = mb_orig_substr($p, 2);
        if ($l) {
            $r = strrev($r);
        }
        return $r;
    }

Usage Example

Beispiel #1
0
 /**
  * Called when new data received
  * @return void
  */
 public function onRead()
 {
     $packet = $this->read(1024);
     $orig = $packet;
     $type = Binary::getByte($packet);
     $code = Binary::getByte($packet);
     $checksum = Binary::getStrWord($packet);
     $id = Binary::getWord($packet);
     $seq = Binary::getWord($packet);
     if ($checksum !== self::checksum(substr_replace($orig, "", 2, 2))) {
         $status = 'badChecksum';
     } elseif ($type === 0x3) {
         $status = isset(static::$unreachableCodes[$code]) ? static::$unreachableCodes[$code] : 'unk' . $code . 'unreachable';
     } else {
         $status = 'unknownType0x' . dechex($type);
     }
     while (!$this->onResponse->isEmpty()) {
         $el = $this->onResponse->shift();
         if ($el instanceof CallbackWrapper) {
             $el = $el->unwrap();
         }
         list($cb, $st) = $el;
         call_user_func($cb, microtime(true) - $st, $status);
     }
     $this->finish();
 }