1: <?php
2: namespace aliuly\common;
3: use aliuly\common\PluginAsyncTask;
4: use pocketmine\plugin\Plugin;
5: use pocketmine\Server;
6: use aliuly\common\Rcon;
7:
8:
9:
10:
11: 12: 13:
14: class RconTask extends PluginAsyncTask {
15: protected $server;
16: protected $cmd;
17: protected $sock;
18:
19: 20: 21: 22: 23: 24: 25:
26: public function __construct(Plugin $owner, $callable, array $remote, $cmd, array $args = []){
27: parent::__construct($owner,$callable,$args);
28: $this->server = $remote;
29: $this->cmd = $cmd;
30: $this->sock = false;
31: }
32: private function close($msg = "") {
33: $this->setResult($msg);
34: if ($this->sock) {
35: fclose($this->sock);
36: $this->sock = false;
37: }
38: }
39: public function onRun() {
40: list($host,$port,$auth) = $this->server;
41: $ret = Rcon::connect($host,$port,$auth);
42: if (!is_array($ret)) {
43: $this->close($ret);
44: return ;
45: }
46: list($this->sock,$id) = $ret;
47:
48: $ret = Rcon::cmd($this->cmd,$this->sock,$id);
49: $this->close($ret);
50: }
51: }
52: