/** * [initReqData 初始化发包信息] * @return [type] [description] */ public function initReqData() { $cmd = 0x1; $seq = 0; $reqhead = new \Tencent\Crm\Test\Com\Head\WebReqHead(); //设置command对象 $command = new \Tencent\Crm\Test\Com\Head\Command(); //设置主命令 $command->setUint32Cmd($cmd); //设置子命令 $command->setUint32SubCmd($cmd); $reqhead->setCommand($command); $reqhead->setTimestamp(time()); $reqhead->setUint32Seq($seq); var_dump(__MEHTOD__ . print_r($reqhead, true)); //转buf $reqhead_buf = $reqhead->serializeToString(); //设置body $pskey_reqbody = new \Tencent\Crm\Spkey\ReqBody(); $pskey_reqbody->setStrUname('938060809'); var_dump(__MEHTOD__ . print_r($pskey_reqbody, true)); //转Buf $pskey_reqbody_buf = $pskey_reqbody->serializeToString(); $data = PBAssistant::packPbData($cmd, $seq, $reqhead_buf, $pskey_reqbody_buf); return $data; }
/** * [unpackPbData 解析pb,验证pb完整性] * @param [type] $requestBuf [description] * @return [type] [description] */ public static function unpackPbData($requestBuf) { // $webStx = substr($requestBuf, 0, 1); $cmdArr = unpack('N', substr($requestBuf, 1, 4)); error_log(__METHOD__ . ' cmdArr : ' . print_r($cmdArr, true) . PHP_EOL, 3, '/tmp/winters.log'); $cmd = $cmdArr[1]; $seqArr = unpack('N', substr($requestBuf, 5, 4)); error_log(__METHOD__ . ' seqArr : ' . print_r($seqArr, true) . PHP_EOL, 3, '/tmp/winters.log'); $seq = $seqArr[1]; $headLen = substr($requestBuf, 9, 4); $headLen = unpack('Nlen', $headLen); $headLen = $headLen['len']; $bodyLen = substr($requestBuf, 13, 4); $bodyLen = unpack('Nlen', $bodyLen); $bodyLen = $bodyLen['len']; $headBuf = substr($requestBuf, 17, $headLen); $bodyBuf = substr($requestBuf, 17 + $headLen, $bodyLen); $webEtx = substr($requestBuf, -1); //验证包头是否是pb数据 if ($webStx != pack('C', self::WEB_STX)) { //pb数据 return array('r' => 1001); } //验证包尾是不是Pb数据 if ($webEtx != pack('C', self::WEB_ETX)) { //pb数据 return array('r' => 1002); } $webReqHead = new \Tencent\Crm\Test\Com\Head\WebReqHead(); try { $webReqHead->parseFromString($headBuf); } catch (Exception $e) { //解包头失败 error_log(__METHOD__ . ' get headObj failed ' . PHP_EOL, 3, '/tmp/winters.log'); return array('r' => 1003); } //验证cmd失败 if ($webReqHead->getCommand()->getUint32Cmd() !== $cmd) { error_log(__METHOD__ . ' check cmd failed ' . PHP_EOL, 3, '/tmp/winters.log'); return array('r' => 1004); } return array('r' => 0, 'cmd' => $cmd, 'seq' => $seq, 'headObj' => $webReqHead, 'bodyBuf' => $bodyBuf); }