PHP Threading SimpleXMLElement

I was looking for EVERYWHERE for this and I have a lot of problems, but I don't think this is a problem with the actual code. Basically this code starts the socket server (login and game) in two separate streams, I basically converted this code from the non-streaming version, but I was not able to get this to work for the streams.

include "socket.php";
include "dep.php";

class Server extends Thread {
    public $server;
    public $config;
    public function __construct($type){
        //$this->config = (string)$type;
        $this->run2($type);
        $this->run();
    }
    public function run(){
        while(true){
            $this->server->loop();
        }
    }
    public function run2($config){
        $this->server = new sokserv($config);
        $this->server->init();
        //while(true){
        //  $this->server->loop();
        //}
    }
}
$login = new Server('Config/config.xml');
$game = new Server("Config/config2.xml");
The error received is 
Fatal error: Uncaught exception 'Exception' with message 'Serialization of 'SimpleXMLElement' is not allowed' in C:\Users\=\Desktop\Test\Start.php:19
Stack trace:
#0 C:\Users\=\Desktop\Test\Start.php(19): Server->run2()
#1 C:\Users\=\Desktop\Test\Start.php(10): Server->run2('Config/config.x...')
#2 C:\Users\=\Desktop\Test\Start.php(26): Server->__construct('Config/config.x...')
#3 {main}
  thrown in C:\Users\=\Desktop\Test\Start.php on line 19

However, returning to the old code is working fine. Sokserv first bit (I removed some public vars because they contain personal information)

class sokserv
{
    public $ip;
    public $port;
    public $users = array();
    public $config;
    public $mysql;
    private $socket;
    public function __construct($config = "Config/config.xml")
    {
        $this->readConfig($config);
    }
    public function readConfig($file)
    {
        if (!file_exists($file))
            die("Could not find config");
        $this->config = simplexml_load_file($file);
    }

And if you want it to be xml:

<server>
    <port>6112</port>
    <ip>0</ip>
    <mysql>
        <host>127.0.0.1</host>
        <username>root</username>
        <dbname></dbname> 
        <password></password>
        <table></table>
    </mysql>
</server>
+4
source share
2 answers

Zend TSRM , ; - . , , . pthreads , , .

, , PHP, pthreads , , . , , pthreads , - . , SimpleXML - .

, , , pthreads, . , pthreads, API , , , , .

, , , pthreads:

<?php
class Config extends Stackable {
    /**
    * Constructs thread safe, sharable configuration from complex XML
    * @param mixed $xml         SimpleXMLElement or location of XML file
    * @param array &$objects    reference store
    */
    public function __construct($xml, &$objects) {
        if ($xml instanceof SimpleXMLElement) {
            foreach ($xml as $key => $value)
                $this[$key] = (string) $value;
        } else {
            foreach (simplexml_load_string(
                        $xml) as $key => $value) {
                if ($value->children()) {
                    $this[$key] = new Config($value, $objects);
                } else $this[$key] = (string) $value;
            }
        }

        /* maintain object references */
        $objects[] = $this;
    }

    public function run() {}
}

class Test extends Thread {
    protected $config;

    /**
    * Constructs thread using pre-constructed thread safe, shared configuration object
    * @param Config $config
    */
    public function __construct(Config $config) {
        $this->config = $config;
    }

    public function run() {
        /* iterate over configuration */
        printf("%d settings:\n", count($this->config));
        foreach ($this->config as $key => $data) {
            if (count($data) > 1) {
                printf( 
                    "\t%s, %d settings:\n", $key, count($data));
                foreach ($data as $name => $value) {
                    printf("\t\t%s = %s\n", $name, $value);
                }
            } else printf("\t%s = %s\n", $key, $data);
        }
        printf("\n");

        printf(
            "Host: %s:%d\n", 
            $this->config->ip, 
            $this->config->port);

        printf(
            "MySQL: %s@%s?database=%s&password=%s&table=%s\n", 
            $this->config->mysql->username,
            $this->config->mysql->host,
            $this->config->mysql->dbname,
            $this->config->mysql->password,
            $this->config->mysql->table);
    }
}

/* Example XML */
$xml = <<<XML
<server>
    <port>6112</port>
    <ip>some.ip</ip>
    <mysql>
        <host>127.0.0.1</host>
        <username>root</username>
        <dbname>somedb</dbname> 
        <password>somepass</password>
        <table>sometable</table>
    </mysql>
</server>
XML;

/* Object reference storage */
$objects = [];
$config = new Config($xml, $objects);

$thread = new Test($config);
$thread->start();

$thread->join();
?>

:

3 settings:
        port = 6112
        ip = some.ip
        mysql, 5 settings:
                host = 127.0.0.1
                username = root
                dbname = somedb
                password = somepass
                table = sometable

Host: some.ip:6112
MySQL: root@127.0.0.1?database=somedb&password=somepass&table=sometable

XML- [format], , XML , .

Config XML.

Config , , / .

, , , , , , , , , , , .

: https://gist.github.com/krakjoe/6437782

+8

PHP , pthreads , , ( ). , , Thread, Worker Stackable, , .

, PHP , , PHP. SimpleXML - , , , .

, , "":

  • - XML sokserv->readConfig . XML . SimpleXML , .
  • , XML, SimpleXMLElement, .
  • - sokserv __sleep __wakeUp Serializable. , $this->config XML- / () SimpleXML /.
  • , SimpleXMLElement . : SimpleXMLElement Serializable, . SimpleXMLElement - .

, : , ( Thread, Worker Stackable), , SimpleXML, .

-1

All Articles