GenisysPro  for Minecraft PE/Windows 10 v1.1.x
Feature-rich server software for Minecraft PE and Windows 10 Edition
RCONInstance Class Reference

Public Member Functions

 isWaiting ()
 
 __construct ($logger, $socket, $password, $maxClients=50)
 
 close ()
 
 run ()
 
 getThreadName ()
 
- Public Member Functions inherited from Thread
 getClassLoader ()
 
 setClassLoader (\ClassLoader $loader=null)
 
 registerClassLoader ()
 
 start (int $options=PTHREADS_INHERIT_ALL)
 
 quit ()
 
 getThreadName ()
 
- Public Member Functions inherited from Thread
 getCreatorId ()
 
 getThreadId ()
 
 isJoined ()
 
 isStarted ()
 
 join ()
 
 start (int $options=PTHREADS_INHERIT_ALL)
 
- Public Member Functions inherited from Threaded
 chunk ($size, bool $preserve=false)
 
 count ()
 
 isRunning ()
 
 isTerminated ()
 
 merge ($from, $overwrite=true)
 
 notify ()
 
 notifyOne ()
 
 offsetGet ($offset)
 
 offsetSet ($offset, $value)
 
 offsetExists ($offset)
 
 offsetUnset ($offset)
 
 pop ()
 
 run ()
 
 shift ()
 
 synchronized (\Closure $function, $args=null)
 
 wait ($timeout)
 
 getRefCount ()
 
 addRef ()
 
 delRef ()
 
 isGarbage ()
 

Data Fields

 $stop
 
 $cmd
 
 $response
 
 $serverStatus
 

Additional Inherited Members

- Static Public Member Functions inherited from Thread
static getCurrentThread ()
 
static getCurrentThreadId ()
 
- Static Public Member Functions inherited from Threaded
static extend ($obj)
 
- Protected Attributes inherited from Thread
 $classLoader
 
 $isKilled = false
 

Constructor & Destructor Documentation

◆ __construct()

__construct (   $logger,
  $socket,
  $password,
  $maxClients = 50 
)

RCONInstance constructor.

Parameters
$logger
$socket
$password
int$maxClients
58  {
59  $this->logger = $logger;
60  $this->stop = false;
61  $this->cmd = "";
62  $this->response = "";
63  $this->socket = $socket;
64  $this->password = $password;
65  $this->maxClients = (int) $maxClients;
66  for($n = 0; $n < $this->maxClients; ++$n){
67  $this->{"client" . $n} = null;
68  $this->{"status" . $n} = 0;
69  $this->{"timeout" . $n} = 0;
70  }
71 
72  $this->start();
73  }

Member Function Documentation

◆ close()

close ( )
121  {
122  $this->stop = true;
123  }

◆ getThreadName()

getThreadName ( )
Returns
string
248  {
249  return "RCON";
250  }

◆ isWaiting()

isWaiting ( )
Returns
bool
45  {
46  return $this->waiting === true;
47  }

◆ run()

run ( )
125  {
126 
127  while($this->stop !== true){
128  $this->synchronized(function(){
129  $this->wait(2000);
130  });
131  $r = [$socket = $this->socket];
132  $w = null;
133  $e = null;
134  if(socket_select($r, $w, $e, 0) === 1){
135  if(($client = socket_accept($this->socket)) !== false){
136  socket_set_block($client);
137  socket_set_option($client, SOL_SOCKET, SO_KEEPALIVE, 1);
138  $done = false;
139  for($n = 0; $n < $this->maxClients; ++$n){
140  if($this->{"client" . $n} === null){
141  $this->{"client" . $n} = $client;
142  $this->{"status" . $n} = 0;
143  $this->{"timeout" . $n} = microtime(true) + 5;
144  $done = true;
145  break;
146  }
147  }
148  if($done === false){
149  @socket_close($client);
150  }
151  }
152  }
153 
154  for($n = 0; $n < $this->maxClients; ++$n){
155  $client = &$this->{"client" . $n};
156  if($client !== null){
157  if($this->{"status" . $n} !== -1 and $this->stop !== true){
158  if($this->{"status" . $n} === 0 and $this->{"timeout" . $n} < microtime(true)){ //Timeout
159  $this->{"status" . $n} = -1;
160  continue;
161  }
162  $p = $this->readPacket($client, $size, $requestID, $packetType, $payload);
163  if($p === false){
164  $this->{"status" . $n} = -1;
165  continue;
166  }elseif($p === null){
167  continue;
168  }
169 
170  switch($packetType){
171  case 9: //Protocol check
172  if($this->{"status" . $n} !== 1){
173  $this->{"status" . $n} = -1;
174  continue;
175  }
176  $this->writePacket($client, $requestID, 0, RCON::PROTOCOL_VERSION);
177  $this->response = "";
178 
179  if($payload == RCON::PROTOCOL_VERSION) $this->logger->setSendMsg(true); //GeniRCON output
180  break;
181  case 4: //Logger
182  if($this->{"status" . $n} !== 1){
183  $this->{"status" . $n} = -1;
184  continue;
185  }
186  $res = (array) [
187  "serverStatus" => unserialize($this->serverStatus),
188  "logger" => str_replace("\n", "\r\n", trim($this->logger->getMessages()))
189  ];
190  $this->writePacket($client, $requestID, 0, serialize($res));
191  $this->response = "";
192  break;
193  case 3: //Login
194  if($this->{"status" . $n} !== 0){
195  $this->{"status" . $n} = -1;
196  continue;
197  }
198  if($payload === $this->password){
199  socket_getpeername($client, $addr, $port);
200  $this->response = "[INFO] Successful Rcon connection from: /$addr:$port";
201  $this->response = "";
202  $this->writePacket($client, $requestID, 2, "");
203  $this->{"status" . $n} = 1;
204  }else{
205  $this->{"status" . $n} = -1;
206  $this->writePacket($client, -1, 2, "");
207  continue;
208  }
209  break;
210  case 2: //Command
211  if($this->{"status" . $n} !== 1){
212  $this->{"status" . $n} = -1;
213  continue;
214  }
215  if(strlen($payload) > 0){
216  $this->cmd = ltrim($payload);
217  $this->synchronized(function(){
218  $this->waiting = true;
219  $this->wait();
220  });
221  $this->waiting = false;
222  $this->writePacket($client, $requestID, 0, str_replace("\n", "\r\n", trim($this->response)));
223  $this->response = "";
224  $this->cmd = "";
225  }
226  break;
227  }
228 
229  }else{
230  @socket_set_option($client, SOL_SOCKET, SO_LINGER, ["l_onoff" => 1, "l_linger" => 1]);
231  @socket_shutdown($client, 2);
232  @socket_set_block($client);
233  @socket_read($client, 1);
234  @socket_close($client);
235  $this->{"status" . $n} = 0;
236  $this->{"client" . $n} = null;
237  }
238  }
239  }
240  }
241  unset($this->socket, $this->cmd, $this->response, $this->stop);
242  exit(0);
243  }

Field Documentation

◆ $cmd

$cmd

◆ $response

$response

◆ $serverStatus

$serverStatus

◆ $stop

$stop

The documentation for this class was generated from the following file: