Minicap數(shù)據(jù)解析(PHP-MiniStream)

<?php

namespace console\controllers;


/**
 * PostController implements the CRUD actions for Post model.
 */


class MiniStream{    

    private $readBannerBytes = 0;
    private $bannerLength = 2;
    private $readFrameBytes = 0;
    private $frameBodyLength = 0;
    private $frameBody = array();
    private $banner;
    private $js_client;

    const BINARY_TYPE_BLOB = "\x81";


    function __construct($client) {
        $this->js_client=$client;
    }




    public function socket(){
        set_time_limit(0); 
        $host = "127.0.0.1";
        $port = 1313;  
        $this->banner = new Banner();
        $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
        socket_connect($socket,$host,$port);    //  連接  


        while ($len=socket_recv($socket,$buff,4096,0)) {  
            for ($index = 0; $index < $len;) {
                $byte10 = ord($buff[$index]);    


                if ($this->readBannerBytes < $this->bannerLength) {                    
                        $index = $this->parserBanner($index, $byte10);        
                }else if ($this->readFrameBytes < 4) {
                        $this->frameBodyLength += ($byte10<< ($this->readFrameBytes * 8)) >> 0;
                        $index+= 1;
                        $this->readFrameBytes += 1;
//                        echo '<br />'. $this->readFrameBytes.'(val='. $this->frameBodyLength.')';    
                        $picLength = $this->frameBodyLength;
                }else{                                                            
                    if ($len - $index >= $this->frameBodyLength) {
//                            echo "<br />frameBodyLength = ".$this->frameBodyLength.'<br />';
                            $bytes = substr($buff, $index, $index+$this->frameBodyLength);
                            array_push($this->frameBody, $bytes);
                            if(bin2hex($this->frameBody[0][0]) !=='ff' || bin2hex($this->frameBody[0][1]) !=='d8'){
                                echo 'JPG header Error';
                                return;
                            }
//                                header("Content-type: image/JPEG",true);
                                $final_data = null;
                                foreach ($this->frameBody as $value) {
                                     $final_data .= $value;
                                }
                                $bas = base64_encode($final_data);

                                $this->send($this->js_client,$bas);
//                                $this->send($this->js_client,'end');
//                                return;


                            $index += $this->frameBodyLength;
                            $this->frameBodyLength = $this->readFrameBytes = 0;
                            $this->frameBody = array();

                    } else {
                            $bytes = substr($buff, $index, $len);
                            array_push($this->frameBody, $bytes);
                            $this->frameBodyLength -= ($len - $index);
                            $this->readFrameBytes += ($len - $index);
                            $index = $len;
                    }
                }
            }

        }  
        socket_close($socket);  
    }

    public function parserBanner($cursor,$byte10){
        $banner = $this->banner;
        switch ($this->readBannerBytes){
            case 0:
                $banner->setVersion($byte10);
                break;
            case 1:
                // length                
                $this->bannerLength = $byte10;
                $banner->setLength($byte10);
                break;
            case 2:
            case 3:
            case 4:
            case 5:
                // pid
                $pid = $banner->getPid();
                $pid += ($byte10 << (($this->readBannerBytes - 2) * 8)) >> 0;
                $banner->setPid($pid);
                break;
            case 6:
            case 7:
            case 8:
            case 9:
                // real width
                $realWidth = $banner->getReadWidth();
                $realWidth += ($byte10 << (($this->readBannerBytes - 6) * 8)) >> 0;
                $banner->setReadWidth($realWidth);
                break;
            case 10:
            case 11:
            case 12:
            case 13:
                // real height
                $realHeight = $banner->getReadHeight();
                $realHeight += ($byte10 << (($this->readBannerBytes - 10) * 8)) >> 0;
                $banner->setReadHeight($realHeight);
                break;
            case 14:
            case 15:
            case 16:
            case 17:
                // virtual width
                $virtualWidth = $banner->getVirtualWidth();
                $virtualWidth += ($byte10 << (($this->readBannerBytes - 14) * 8)) >> 0;
                $banner->setVirtualWidth($virtualWidth);

                break;
            case 18:
            case 19:
            case 20:
            case 21:
                // virtual height
                $virtualHeight = $banner->getVirtualHeight();
                $virtualHeight += ($byte10 << (($this->readBannerBytes - 18) * 8)) >> 0;
                $banner->setVirtualHeight($virtualHeight);
                break;
            case 22:
                // orientation
                $banner->setOrientation($byte10 * 90);
                break;
            case 23:
                // quirks
                $banner->setQuirks($byte10);
                break;
            }

        $cursor += 1;
        $this->readBannerBytes += 1;
        if ($this->readBannerBytes == $this->bannerLength) {
//            echo $banner->toString();
            null;
        }
        return $cursor;
    }

    public function frame($buffer)
      {
        $len = strlen($buffer);

          $first_byte = self::BINARY_TYPE_BLOB;

           if ($len <= 125) {
               $encode_buffer = $first_byte . chr($len) . $buffer;
           } else {
             if ($len <= 65535) {
                   $encode_buffer = $first_byte . chr(126) . pack("n", $len) . $buffer;
               } else {
                 //pack("xxxN", $len)pack函數(shù)只處理2的32次方大小的文件莽鸭,實(shí)際上2的32次方已經(jīng)4G了馆蠕。 
                  $encode_buffer = $first_byte . chr(127) . pack("xxxxN", $len) . $buffer;
              }
           }

          return $encode_buffer;
       }

    // 返回?cái)?shù)據(jù)
    function send($client, $msg)
    {
        $msg = $this->frame($msg);
        socket_write($client, $msg, strlen($msg));     
    }

}

class Banner{
    private $version;
    private $length;
    private $pid;
    private $readWidth;
    private $readHeight;
    private $virtualWidth;
    private $virtualHeight;
    private $orientation;
    private $quirks;

    public function toString() {
//            echo $this->getLength();
            return "Banner [version=".$this->version.", length=".$this->length.", pid="
                           .$this->pid.", readWidth=".$this->readWidth.", readHeight="
                           .$this->readHeight.", virtualWidth=".$this->virtualWidth
                           .", virtualHeight=".$this->virtualHeight.", orientation="
                           .$this->orientation.", quirks=".$this->quirks."]";
    }
    public function getVersion() {
            return $this->version;
    }
    public function setVersion($version) {
            $this->version = $version;            
    }
    public function getLength() {
            return $this->length;
    }
    public function setLength($length) {
            $this->length = $length;
    }
    public function getPid() {
            return $this->pid;
    }
    public function setPid($pid) {
            $this->pid = $pid;
    }
    public function getReadWidth() {
            return $this->readWidth;
    }
    public function setReadWidth($readWidth) {
            $this->readWidth = $readWidth;
    }
    public function getReadHeight() {
            return $this->readHeight;
    }
    public function setReadHeight($readHeight) {
            $this->readHeight = $readHeight;
    }
    public function getVirtualWidth() {
            return $this->virtualWidth;
    }
    public function setVirtualWidth($virtualWidth) {
            $this->virtualWidth = $virtualWidth;
    }
    public function getVirtualHeight() {
            return $this->virtualHeight;
    }
    public function setVirtualHeight($virtualHeight) {
            $this->virtualHeight = $virtualHeight;
    }
    public function getOrientation() {
            return $this->orientation;
    }
    public function setOrientation($orientation) {
            $this->orientation = $orientation;
    }
    public function getQuirks() {
            return $this->quirks;
    }
    public function setQuirks($quirks) {
            $this->quirks = $quirks;
    }

    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市朽砰,隨后出現(xiàn)的幾起案子朦佩,更是在濱河造成了極大的恐慌并思,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,039評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件语稠,死亡現(xiàn)場(chǎng)離奇詭異宋彼,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)仙畦,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,426評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)输涕,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人慨畸,你說(shuō)我怎么就攤上這事莱坎。” “怎么了寸士?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,417評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵檐什,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我碉京,道長(zhǎng)厢汹,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,868評(píng)論 1 295
  • 正文 為了忘掉前任谐宙,我火速辦了婚禮烫葬,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘凡蜻。我一直安慰自己搭综,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,892評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布划栓。 她就那樣靜靜地躺著兑巾,像睡著了一般。 火紅的嫁衣襯著肌膚如雪忠荞。 梳的紋絲不亂的頭發(fā)上蒋歌,一...
    開(kāi)封第一講書(shū)人閱讀 51,692評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音委煤,去河邊找鬼堂油。 笑死,一個(gè)胖子當(dāng)著我的面吹牛碧绞,可吹牛的內(nèi)容都是我干的府框。 我是一名探鬼主播,決...
    沈念sama閱讀 40,416評(píng)論 3 419
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼讥邻,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼迫靖!你這毒婦竟也來(lái)了院峡?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,326評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤系宜,失蹤者是張志新(化名)和其女友劉穎照激,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體蜈首,經(jīng)...
    沈念sama閱讀 45,782評(píng)論 1 316
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡实抡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,957評(píng)論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了欢策。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片吆寨。...
    茶點(diǎn)故事閱讀 40,102評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖踩寇,靈堂內(nèi)的尸體忽然破棺而出啄清,到底是詐尸還是另有隱情,我是刑警寧澤俺孙,帶...
    沈念sama閱讀 35,790評(píng)論 5 346
  • 正文 年R本政府宣布辣卒,位于F島的核電站,受9級(jí)特大地震影響睛榄,放射性物質(zhì)發(fā)生泄漏荣茫。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,442評(píng)論 3 331
  • 文/蒙蒙 一场靴、第九天 我趴在偏房一處隱蔽的房頂上張望啡莉。 院中可真熱鬧,春花似錦旨剥、人聲如沸咧欣。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,996評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)魄咕。三九已至,卻和暖如春蚌父,著一層夾襖步出監(jiān)牢的瞬間哮兰,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,113評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工苟弛, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留喝滞,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,332評(píng)論 3 373
  • 正文 我出身青樓嗡午,卻偏偏與公主長(zhǎng)得像囤躁,于是被迫代替她去往敵國(guó)和親冀痕。 傳聞我的和親對(duì)象是個(gè)殘疾皇子荔睹,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,044評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容