/** * 接收消息体 * * @param package $package * @return string or boolean */ public function receiveMsg() { $socket = $this->sock; if ($socket == null) { $this->errno = ERR_REQUEST_NULL_SOCKET; $this->error = '连接不存在,请检查连接是否正常'; return false; } $rdata = socket_read($socket, 14, PHP_BINARY_READ); $n = strlen($rdata); if ($n == 14) { $header = new CwxMsgHead(); $ret = $header->fromNet($rdata); if ($ret == true) { $dataLen = $header->getDataLen(); $rdata = null; $n = 0; while ($n < $dataLen) { $rt = socket_read($socket, $dataLen - $n, PHP_BINARY_READ); $rdata .= $rt; $n = $n + strlen($rt); } if ($n == $dataLen) { //处理压缩的消息,进行解压缩操作 if (($header->getAttr() & 2) == true) { $rdata = gzuncompress($rdata); } return $rdata; } else { $this->errno = ERR_REQUEST_RECEIVE_BAD_MSG; $this->error = '获取消息体失败 返回数据长度错误'; return false; } } else { $this->errno = $header->getLastErrno(); $this->error = $header->getLastError(); return false; } } else { $this->errno = ERR_REQUEST_RECEIVE_BAD_MSG_HEADR; $this->error = '获取消息头失败 返回数据长度错误'; return false; } }