public static function checkTimeouts() { global $MASTERTIMEOUT; global $MASTERUPDATETIME; $curtime = time(); if ($curtime - self::$lastHeartbeatTime >= $MASTERUPDATETIME) { self::$lastHeartbeatTime = $curtime; foreach (self::$servers as $srv) { try { $hb = new MasterHeartbeat($srv); SocketManager::addSocket($hb); } catch (Exception $e) { print "Heartbeat error: " . $e->getMessage() . "\n"; } } } foreach (self::$servers as $srv) { if ($curtime - $srv->lastHeartbeatTime >= $MASTERTIMEOUT) { if ($srv->removeable === true) { print "Masterserver Timeout, removing {$srv->ip}:{$srv->port}\n"; self::removeServer($srv->ip, $srv->port); } else { print "Masterserver Timeout (unremoveable) {$srv->ip}:{$srv->port}\n"; } } } }
public function __construct($ip, $port) { $this->IP = $ip; $this->Port = $port; $this->Socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname("tcp")); $this->Connected = false; SocketManager::AddSocket($this); }
public function onReadyToReceive() { while (($thenewsock = @socket_accept($this->_socket)) !== false) { print "New Client: {$thenewsock}\n"; $newClient = new ClientSock($thenewsock); SocketManager::addSocket($newClient); } }
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA require_once "config.inc.php"; require_once "ClientSock.php"; require_once "ServerInfo.php"; require_once "ServerList.php"; require_once "MasterList.php"; require_once "MasterInfo.php"; require_once "UDPListener.php"; require_once "TCPListener.php"; set_time_limit(0); $udpListener = new UDPListener($listenip, $listenport); $tcpListener = new TCPListener($listenip, $listenport); ServerList::Initialize(); MasterList::Initialize(); SocketManager::Initialize(); $testms = new MasterInfo("83.142.228.166", 28900, false); MasterList::addServer($testms); SocketManager::addSocket($udpListener); SocketManager::addSocket($tcpListener); $isRunning = true; while ($isRunning) { ServerList::checkTimeouts(); MasterList::checkTimeouts(); MasterList::dumpServers(); ServerList::dumpServers(); SocketManager::handleEvents(); }
<?php namespace api\Sockets; class SocketManager { public static $Sockets; public static function Init() { self::$Sockets = []; } public static function AddSocket($socket) { self::$Sockets[] = $socket; } public static function CloseAllSockets() { foreach (self::$Sockets as $socket) { $socket->Close(); } } } SocketManager::Init();