Exemplo n.º 1
0
 /**
  * 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);
 }
Exemplo n.º 2
0
	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);
	}
Exemplo n.º 3
0
 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;
     }
 }
Exemplo n.º 4
0
 /**
  * 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;
 }
Exemplo n.º 5
0
	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;
	}
Exemplo n.º 6
0
 /**
  * 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);
 }
Exemplo n.º 7
0
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:
	}
}
Exemplo n.º 8
0
	public function end()
	{
		$SH = SocketHandler::getInstance();
		$SH->interrupt();
	}
Exemplo n.º 9
0
	protected function _destroy()
	{
		$SH = SocketHandler::getInstance();
		$SH->close($this->sid);
		unset($this);
	}
 public function keepAlive()
 {
     parent::keepAlive();
 }
Exemplo n.º 11
0
 public function keepAlive()
 {
     $this->handler->keepAlive();
 }
Exemplo n.º 12
0
	public function end()
	{
		$SH = SocketHandler::getInstance();
		$SH->interrupt();

		$this->running = FALSE;
	}