weixin_39957068
weixin_39957068
2021-01-09 04:45

Unknown resource type in different contexts

Hello, I have a problem, i programmed a class where construct a instance of WinSockets, then start the instance with a new thread, i tried to access to a property where i save a reference of socket, but, show "resource 1 of unknown type". what's happened here? Best regards. Sorry for my bad english, i speak only spanish D:

该提问来源于开源项目:krakjoe/pthreads

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

7条回答

  • weixin_39957068 weixin_39957068 3月前

    I trying to recopy my problem, I realized that if I created the resource calling context, then the thread to access the property containing the resource, creating copies every time. The solution was to set the property to its current value as follows:

    $ this->resourceSocket = $ this->resourceSocket;

    He fixed all the problems.

    In this example, the loop while, show me (var_dump) resource(1) of type Socket, in repeat: resource(2) of type socket, to 1900 aprox. then receive message, and the var_dump show resource(1) of type unknown, this is the original problem.

    <?php

    class mysocket extends Thread { private $socket = null; private $address = null; private $port = 0;

    
    public function __construct($address, $port)
    {
        $this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
        var_dump($this->socket); // ok
        $this->address = $address;
        $this->port = $port;
    }
    <p>public function run()
    {
        var_dump($this->socket); // ok
        if(socket_connect($this->socket, $this->address, $this->port)===false)
            echo 'Error on connect';
        // spectate for incomming new messages
        while(true)
            $this->refresh();
    }</p>
    <p>public function refresh()
    {
            //$this->lock();
            $read = array($this->socket);
            $write = null;
            $exceptions = null;
            var_dump($this->socket);
            if(($result = socket_select($read, $write, $exceptions, 0)) === false)
                $this->onDisconnect();
            if(isset($read[0]))
                $this->socket = $read[0];
            if($result > 0) 
            {
                var_dump($this->socket);
                // SOCKET RECEIVE MESSAGE OR CHANGE STATUS
                if (false === ($len = socket_recv($this->socket, $buf, 2048, 0)))
                    echo 'error in read';
                if($buf === null)
                { // receive end signal
                    $this->endLoop = true;
                    $this->onDisconnect();
                } else { // show message
                    echo $buf;<br>
                }
                return true;
            } else { return false; }
            //$this->unlock();
    }</p>
    <p>public function onDiscconect()
    {
        echo 'Disconnected D:';
    }
    fix: </p>
    <p>after
    public function refresh()
        {</p>
    <p>add:
    $this->after
    public function refresh()
        {</p>
    <p>add:
    $this-></p>
    <p>}</p>
    <p>$sock = new mysocket('irc.freenode.net', 6667);</p>
    <p>$sock->start();</p>
    <p>fix: </p>
    <p>after
    public function refresh()
        {</p>
    <p>add:
    $this->socket = $this->socket;</p>
    <p>then no make new copyes, and not more problems.</p>
    <p>The pthreads work fine with Sockets. In this moment, I work for finish the support of my class PHPSocketMaster for pThreads.</p>
    <p>thanks for your help!, </p>
    点赞 评论 复制链接分享
  • weixin_39814454 weixin_39814454 3月前

    Did you look at the examples? e.g. https://github.com/krakjoe/pthreads/blob/master/examples/Sockets.php or also https://github.com/krakjoe/pthreads/blob/master/examples/MySQLi.php

    Otherwise please post some code, something like this should actually work:

     php
    class WhatEver extends Worker {
     private $sock;
     public __construct() { 
       $this->sock = new COM("WinSock"); // or whatever...
     }
    
     public function run() {
       // access $sock with $this->sock
     }
    }
    
    $we = new WhatEver();
    $we->start();
    

    regards Simon

    点赞 评论 复制链接分享
  • weixin_39957068 weixin_39957068 3月前

    thanks, first i don't use Component object model winosck, i use the native control for scokets, ¿why? Com only work in wincows. the second example is fine :)

    thanks, bye!

    https://github.com/alexander171294/PHPSocketMaster

    EDIT:

    I tried the options, but, the resource of Sockets (using native sockets), is not possible sharing, the resource lost type and don't work.

    Now i test the first example, but use com is a problem.

    this is the code: https://github.com/alexander171294/PHPSocketMaster/blob/develop/src/class/ThreadSocketMaster.php

    and this is the original (without threads): https://github.com/alexander171294/PHPSocketMaster/blob/develop/src/class/SocketMaster.php

    点赞 评论 复制链接分享
  • weixin_39814454 weixin_39814454 3月前

    Well, I'm using my mobile phone right now.. But am I right that you try to implement a socket server? Somethin like this: https://github.com/krakjoe/pthreads/blob/master/examples/SocketServer.php ?

    Regards

    点赞 评论 复制链接分享
  • weixin_39957068 weixin_39957068 3月前

    yes and noup, really i develop a classes for implement clients, servers, websocketservers, and others, the classes work fine without threads, but i like add threads support, for the advantage this entails.

    In order to improve PHPSocketMaster, i developed a new class, in this appears the problema with reference of Socket, in my opinion, this occurs by the serialize and unserialize property, causing lost type of the resource and leave work.

    saludos! Regards

    点赞 评论 复制链接分享
  • weixin_39814454 weixin_39814454 3月前

    mhm, just looked over it quickly, didn't even look into the pthreads issue what you have here. I would suggest you provide a simple and cleaned up code to show your issue. Some things in your code actually will lead to cpu problems and other issues. e.g.

     php
    while($this->endLoop == false)
            {
                $this->refresh();
            }
    

    this will probably freeze the script anyway.

    and please read the documentation of pthreads (again), all objects you want to pass to a thread that shouldn't get serialized, must be inherited from a pthread's class! I see in your code that this is definitely not the case:

     php
     $pool = new \Pool(1, 'PHPSocketMaster\trabajador', [$this]); 
    

    where $this is a instance of SocketMaster, which isn't extended from any Stackable/Thread class, but contains the socket (which will then get serialized or whatever happens with a socket ;)) Keep in mind that resources are actually unsupported.

    regards Simon

    点赞 评论 复制链接分享
  • weixin_39957068 weixin_39957068 3月前

    sorry my mistake was to pass that code since it was modified and testing is not complete

    you're right, pass a clean code at a time.

    I would like sugirieras other form of Relaod status sockets continuous and uninterrupted without using a loop.

    Moreover soon I leave the code clean my specific problem.

    I reread the documentation.

    点赞 评论 复制链接分享

为你推荐