/**
* 发送数据
*
* @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;
}
}