Skip to content

Commit

Permalink
Added support for configuring query server keep-alive interval
Browse files Browse the repository at this point in the history
- New ts3 server seems to have changed the server query connection timeout. Using the configuration file it is possible to adjust this timeout.
  • Loading branch information
botorabi committed Sep 17, 2018
1 parent 5f70801 commit 413beea
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 22 deletions.
17 changes: 9 additions & 8 deletions examples/MultipleBotTypes/config/Configuration.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright (c) 2016-2017 by Botorabi. All rights reserved.
* Copyright (c) 2016-2018 by Botorabi. All rights reserved.
* https://github.com/botorabi/TeamSpeakPHPBots
*
* License: MIT License (MIT), read the LICENSE text in
Expand All @@ -22,13 +22,14 @@ abstract class Configuration {

//! TS3 Server Query related config
public static $TSPHPBOT_CONFIG_TS3SERVER_QUERY = [
"host" => "localhost",
"userName" => "serveradmin", // TODO put your TS3 user name here
"password" => "exffRAo3", // TODO put your TS3 user password here
"hostPort" => 10011,
"vServerPort" => 9987,
"pollInterval" => 1, // intervall of bot control steps in seconds
"nickName" => "TS3 PHP Bot" // this is the name displayed in TS3 clients
"host" => "localhost",
"userName" => "serveradmin", // TODO put your TS3 user name here
"password" => "exffRAo3", // TODO put your TS3 user password here
"hostPort" => 10011,
"vServerPort" => 9987,
"pollInterval" => 1, // intervall of bot control steps in seconds
"keepAliveInterval" => 120, // server connection keep-alive interval in seconds
"nickName" => "TS3 PHP Bot" // this is the name displayed in TS3 clients
];

//! Databank account info (MySQL)
Expand Down
26 changes: 21 additions & 5 deletions libraries/TeamSpeakPHPBots/src/com/tsphpbots/bots/BotManager.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright (c) 2016-2017 by Botorabi. All rights reserved.
* Copyright (c) 2016-2018 by Botorabi. All rights reserved.
* https://github.com/botorabi/TeamSpeakPHPBots
*
* License: MIT License (MIT), read the LICENSE text in
Expand Down Expand Up @@ -71,7 +71,6 @@ class BotManager {
* Construct the bot manager.
*/
public function __construct() {
$this->ts3Connection = new TSServerConnections();
}

/**
Expand All @@ -80,10 +79,15 @@ public function __construct() {
* @return boolean Return true if bot manager was successfully initialized, otherwise false.
*/
public function initialize() {
Log::debug(self::$TAG, "initializing the bot manager");

$this->ts3Connection = new TSServerConnections();

Log::debug(self::$TAG, " creating a connection to teamspeak server...");
// create a default server connection
$this->ts3DefaultConnection = $this->createServerConnection(Config::getTS3ServerQuery("nickName"));
if (is_null($this->ts3DefaultConnection)) {
Log::error(self::$TAG, "could not initialize bot manager, no teamspeak server connection!");
Log::error(self::$TAG, " could not initialize bot manager, no teamspeak server connection!");
return false;
}
return true;
Expand Down Expand Up @@ -123,7 +127,19 @@ public function update() {
}
// limit the timout to a minimum of 50 ms
$timeout = $timeout < 50 ? 50 : $timeout;
$this->ts3Connection->update($timeout);
try {
$this->ts3Connection->update($timeout);
}
catch(\Exception $e) {
Log::error(self::$TAG, "an exception occured while communicating to server");
Log::error(self::$TAG, " backtrace\n" . $e->getTraceAsString());

$this->shutdown();
Log::error(self::$TAG, " trying to reinitialize the connection in 5 seconds...");
usleep(5000);
$this->initialize();
$this->loadBots();
}
}

/**
Expand Down Expand Up @@ -504,4 +520,4 @@ public function notifyBotMessage($botType, $botId, $text) {
return false;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ abstract class Config {
*
* @var string The framework version
*/
protected static $FRAMEWORK_VERSION = "1.0.0";
protected static $FRAMEWORK_VERSION = "1.0.1";

/**
* Given an array and a token name return its value if it exists.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright (c) 2016-2017 by Botorabi. All rights reserved.
* Copyright (c) 2016-2018 by Botorabi. All rights reserved.
* https://github.com/botorabi/TeamSpeakPHPBots
*
* License: MIT License (MIT), read the LICENSE text in
Expand Down Expand Up @@ -31,6 +31,11 @@ class TSServerConnections {
*/
protected $ts3servers = [];

/**
* @var int Server connection's keep-alive-interval in seconds
*/
protected $keepAliveInterval = 60;

/**
* @var int Notification registration flag for "server"
*/
Expand Down Expand Up @@ -78,6 +83,11 @@ protected function init() {
\TeamSpeak3_Helper_Signal::getInstance()->subscribe("notifyLogin", array($this, "onLogin"));
\TeamSpeak3_Helper_Signal::getInstance()->subscribe("serverqueryWaitTimeout", array($this, "onTimeout"));
\TeamSpeak3_Helper_Signal::getInstance()->subscribe("notifyEvent", array($this, "onEvent"));

if (Config::getTS3ServerQuery("keepAliveInterval")) {
$this->keepAliveInterval = Config::getTS3ServerQuery("keepAliveInterval");
}
Log::debug(self::$TAG, "setting server connection keep-alive interval to " . $this->keepAliveInterval . " seconds");
}

/**
Expand Down Expand Up @@ -156,11 +166,16 @@ public function createConnection($nickName, $cbEvent, $registrationFlags = 26) {
*/
public function shutdown() {
Log::debug(self::$TAG, "shutting down the teamspeak connection manager");
// close all server connections
foreach($this->ts3servers as $srv) {
$srv["server"]->request("quit");
try {
// close all server connections
foreach($this->ts3servers as $srv) {
$srv["server"]->request("quit");
}
$this->ts3servers = [];
}
catch(\Exception $e) {
Log::error(self::$TAG, "an exception occured while quitting the connection to server, reason: " . $e->getMessage());
}
$this->ts3servers = [];
}

/**
Expand All @@ -171,7 +186,7 @@ public function shutdown() {
public function update($timeout = 0) {
foreach($this->ts3servers as $srv) {
$adapter = $srv["server"]->getAdapter();
if($adapter->getQueryLastTimestamp() < time() - 300) {
if($adapter->getQueryLastTimestamp() < time() - $this->keepAliveInterval) {
//Log::debug(self::$TAG, "sending keep-alive command");
$adapter->request("clientupdate");
}
Expand Down Expand Up @@ -232,7 +247,7 @@ public function onConnect(\TeamSpeak3_Adapter_ServerQuery $adapter) {
* @return void
*/
public function onTimeout($seconds, \TeamSpeak3_Adapter_ServerQuery $adapter) {
if($adapter->getQueryLastTimestamp() < time()-300) {
if($adapter->getQueryLastTimestamp() < time() - $this->keepAliveInterval) {
//Log::debug(self::$TAG, "sending keep-alive command");
$adapter->request("clientupdate");
}
Expand Down Expand Up @@ -267,4 +282,4 @@ public function onEvent(\TeamSpeak3_Adapter_ServerQuery_Event $event, \TeamSpeak
}
}
}
}
}

0 comments on commit 413beea

Please sign in to comment.