Php - 线程化堆栈中丢失资源套接字
Php - Lost ressource socket in a stack Threaded
我有 2 个 class,ListenUser class 获取新用户并推入 ReadSocket class 这个新用户的资源套接字。
问题是当我将新用户推入堆栈时,我自动丢失了堆栈中的资源套接字,但它继续在 ListenUser class 中工作。
如何将新用户放入 ReadSocket 堆栈并保持连接?
ListenUser class 接收服务器套接字后的代码
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 class 代码
<?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;
}
}
我找到了解决方案,套接字(socket_create、socket_bind 和 socket_listen)在无线程 class 中是 used/declared。所以我扩展到线程 class 没关系:)
我有 2 个 class,ListenUser class 获取新用户并推入 ReadSocket class 这个新用户的资源套接字。 问题是当我将新用户推入堆栈时,我自动丢失了堆栈中的资源套接字,但它继续在 ListenUser class 中工作。 如何将新用户放入 ReadSocket 堆栈并保持连接?
ListenUser class 接收服务器套接字后的代码
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 class 代码
<?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;
}
}
我找到了解决方案,套接字(socket_create、socket_bind 和 socket_listen)在无线程 class 中是 used/declared。所以我扩展到线程 class 没关系:)