/** * The constructor, creates the socket and starts listening * * @param integer $port Port to bind to * @param string $bind IP address to bind to * @return */ public function __construct($port, $bind = '0.0.0.0') { $socket = stream_socket_server("tcp://{$bind}:{$port}", $this->errno, $this->errstr); if (false === $socket) { throw new TCPServerException($this->errno); } $this->meta['bind'] = $bind; $this->meta['port'] = $port; parent::__construct($socket); }
public function write($format) { if (strlen($format) == 0) return; $SH = SocketHandler::getInstance(); $args = func_get_args(); array_unshift($args, $this->sid); call_user_func_array(array($SH, 'send'), $args); }
public static function Bind() { if (file_exists(self::$File)) { unlink(self::$File); } self::$Socket_Bind = socket_bind(self::$Socket, self::$File); if (self::$Socket_Bind !== false) { return true; } else { return false; } }
/** * Handles DaemonCore requests. * * @param string $Input * @return boolean */ public static function Start($Input) { System_Daemon::debug('Starting "DaemonCore::Start" subprocess.'); $retVal = null; $Input = explode(" ", $Input, 2); switch ($Input[0]) { case 'checkAll': $retVal = self::checkAllData(); break; case 'Restart': System_Daemon::info('Running Restart subprocess.'); SocketHandler::Close(); System_Daemon::restart(); break; case 'SaveConfig': System_Daemon::debug('Running SaveConfig subprocess.'); if (isset($Input[1]) && is_array($Input[1])) { foreach (json_decode(trim($Input[1])) as $name => $wert) { if (isset(DaemonConfig::$cfg->{$name})) { DaemonConfig::$cfg->{$name} = $wert; } } } DaemonConfig::Save(); DaemonConfig::SaveOldConfig(); System_Daemon::debug('Finished SaveConfig subprocess.'); $retVal = true; break; case 'Setup': if (file_exists(dirname(__FILE__) . '/DaemonCoreSetup.php')) { require_once dirname(__FILE__) . '/DaemonCoreSetup.php'; if (isset($Input[1]) && $Input[1] != '') { $retVal = Setup($Input[1]); } } else { $retVal = false; } break; default: System_Daemon::warning("Don't know what to do with " . $Input[0]); $retVal = false; break; } System_Daemon::debug('Finished "DaemonCore::Start" subprocess.'); return $retVal; }
public function readData($sid, $data) { $d = explode("\n", $data); $line = array_shift($d); $data = implode("\n", $d); echo $line."\n"; switch (SocketHandler::getType($sid)) { case SH_CLIENT: $this->clients[$sid]->parse($line); break; case SH_SERVER: // $this->servers[$sid]->parse($line); break; } return $data; }
/** * The constructor, creates the socket and establishes the connection * * @param string $host Host to connect to * @param integer $port Port to connect to * @param bool $ssl True if an ssl connection, defaults to false * @param string $bind IP address to bind to * @param integer $bindport Port number to bind to * @return */ public function __construct($host, $port, $ssl = false, $bind = '', $bindport = 0, $verifyssl = false) { $options = array('socket' => array()); if (!empty($bind) || $bindport > 0 && $bindport < 65536) { $options['socket']['bindto'] = ($bind == '' ? '0' : $bind) . ($bindport > 0 ? ':' . $bindport : ''); } if ($ssl) { $options['ssl']['verify_peer'] = $verifyssl; $options['ssl']['allow_self_signed'] = !$verifyssl; } $context = stream_context_create($options); $socket = stream_socket_client(($ssl ? 'ssl' : 'tcp') . "://{$host}:{$port}", $this->errno, $this->errstr, 30, ($ssl ? 0 : STREAM_CLIENT_ASYNC_CONNECT) | STREAM_CLIENT_CONNECT, $context); if (false === $socket) { throw new TCPClientException($this->errno); } $this->meta['host'] = $host; $this->meta['port'] = $port; if (false === stream_set_blocking($socket, 0)) { throw new TCPClientException($this->errno); } parent::__construct($socket); }
function signal_handler ($signo) { switch ($signo) { case SIGINT: _log(L_INFO, "Received SIGINT. Stopping..."); $SH = SocketHandler::getInstance(); $SH->interrupt(); break; case SIGTERM: _log(L_INFO, "Received SIGTERM. Stopping..."); $SH = SocketHandler::getInstance(); $SH->interrupt(); break; case SIGHUP: _log(L_INFO, "Received SIGHUP. Rehashing."); $config = Configurator::getInstance(); $config->rehash(); break; default: } }
public function end() { $SH = SocketHandler::getInstance(); $SH->interrupt(); }
protected function _destroy() { $SH = SocketHandler::getInstance(); $SH->close($this->sid); unset($this); }
public function keepAlive() { parent::keepAlive(); }
public function keepAlive() { $this->handler->keepAlive(); }
public function end() { $SH = SocketHandler::getInstance(); $SH->interrupt(); $this->running = FALSE; }