我有2个类,ListenUser类获取新用户并在ReadSocket类中推送这个新用户的ressource套接字。 问题是当我在堆栈中推送新用户时,我自动丢失了堆栈中的ressource套接字,但它继续在ListenUser类中工作。 如何将新用户放入ReadSocket堆栈并锁定连接?
接收服务器套接字后的ListenUser类代码
class ListenUser
{
/*
*/
public function __construct($socket, $debug = false)
{
$this->socket = $socket
$this->StackSocket = new StackSocket; // class StackSocket extends Threaded {}
$this->ReadSocket = new ReadSocket($this->StackSocket);
}
/*
*/
public function run()
{
while (true) {
if (($user = socket_accept($this->socket)) !== false)
{
//
$this->ReadSocket->synchronized(function($thread) {
if ($thread->statutThread == true) {
$thread->wait();
}
$thread->statutUsers = true;
$thread->notify();
}, $this->ReadSocket);
//
$this->ReadSocket->synchronized(function($thread, $user) {
$thread->addUser($user); // Add user
$thread->statutUsers = false;
$thread->notify();
}, $this->ReadSocket, $user);
}
}
}
}
我的ReadSocket类代码
<?php
class ReadSocket extends Thread
{
/*
*/
public function __construct($stack, $debug = false)
{
$this->stack = $stack;
}
/*
*/
public function run()
{
while (true)
{
$this->synchronized(function($thread) {
if ($thread->statutUsers == true) {
$thread->wait();
}
$thread->statutThread = true;
$thread->notify();
}, $this);
$this->statutThread = false;
}
return $this;
}
/*
*/
public function addUser($user)
{
print_r($user); // my ressource socket is ok
$this->stack[] = $user;
print_r($this->stack[0]); // I lost ressource socket
return $this;
}
}
答案 0 :(得分:0)
我发现在无线程类中使用/声明了解决方案socket(socket_create,socket_bind和socket_listen)。所以我扩展到线程类,它没关系:)