public static function bytes2int($str, $l = false) { if ($l) { $str = strrev($str); } $dec = 0; $len = mb_orig_strlen($str); for ($i = 0; $i < $len; ++$i) { $dec += ord(mb_orig_substr($str, $i, 1)) * pow(0x100, $len - $i - 1); } return $dec; }
/** * Called when new data received * @return void */ public function onRead() { start: if ($this->type === 'udp') { $this->onUdpPacket($this->read($this->getInputLength())); return; } if ($this->state === self::STATE_ROOT) { if (false === ($hdr = $this->readExact(2))) { return; // not enough data } $this->pctSize = Binary::bytes2int($hdr); $this->setWatermark($this->pctSize); $this->state = self::STATE_PACKET; } if ($this->state === self::STATE_PACKET) { if (false === ($pct = $this->readExact($this->pctSize))) { return; // not enough data } $this->state = self::STATE_ROOT; $this->setWatermark(2); $this->onUdpPacket($pct); } goto start; }