MyQEE\Server\RPC\Server::encrypt PHP Метод

encrypt() публичный статический Метод

加密数据
public static encrypt ( mixed $data, $key = null, $rpc = null ) : string
$data mixed
Результат string
    public static function encrypt($data, $key = null, $rpc = null)
    {
        $rs = new \stdClass();
        if ($rpc && $rpc !== static::$defaultRPC) {
            $rs->rpc = $rpc;
        }
        $string = msgpack_pack($data);
        $rs->type = is_object($data) ? $data instanceof \stdClass || isset($data->type) ? $data->type : get_class($data) : 'e';
        $rs->data = self::rc4($string, false, $key);
        $rs->hash = md5($string . $key . $rs->rpc);
        return msgpack_pack($rs);
    }

Usage Example

Пример #1
0
 /**
  * 发送数据
  *
  * @param string $type 类型: task | msg
  * @param mixed  $data
  * @param string $workerName 当前进程对应的名称
  * @param \Closure $callback 需要回调的信息, $type = task 时支持
  * @return bool
  */
 public function sendData($type, $data, $workerName, $callback = null)
 {
     $resource = $this->resource();
     if (!$resource) {
         return false;
     }
     $id = Host::$taskIdAtomic->add();
     $obj = new \stdClass();
     $obj->type = $type;
     $obj->id = $id;
     $obj->sid = $this->serverId;
     $obj->wid = $this->workerId;
     $obj->wname = $workerName;
     $obj->data = $data;
     if ($obj->id > 4000000000) {
         # 重置序号
         Host::$taskIdAtomic->set(0);
         $obj->id = 0;
     }
     if ($callback && $type === 'task') {
         # 设置一个回调
         $this->taskCallbackList[$id] = $callback;
     }
     $str = ($this->key ? \MyQEE\Server\RPC\Server::encrypt($obj, $this->key) : msgpack_pack($obj)) . \MyQEE\Server\RPC\Server::$EOF;
     $len = strlen($str);
     $wLen = @fwrite($resource, $str);
     if ($len !== $wLen) {
         # 发送失败
         $this->close();
         return false;
     } else {
         $this->lastTaskId = $obj->id;
         return true;
     }
 }
All Usage Examples Of MyQEE\Server\RPC\Server::encrypt