mirror of
https://github.com/YGGverse/nex-php.git
synced 2026-03-31 17:55:31 +00:00
add server stop method
This commit is contained in:
parent
89356780c1
commit
a241dddbd2
2 changed files with 46 additions and 3 deletions
|
|
@ -61,6 +61,8 @@ $server = new \Yggverse\Nex\Server('127.0.0.1', 1915);
|
||||||
#### Server::getPort
|
#### Server::getPort
|
||||||
#### Server::setSize
|
#### Server::setSize
|
||||||
#### Server::getSize
|
#### Server::getSize
|
||||||
|
#### Server::setLive
|
||||||
|
#### Server::getLive
|
||||||
|
|
||||||
#### Server::start
|
#### Server::start
|
||||||
|
|
||||||
|
|
@ -82,3 +84,9 @@ $server->start(
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### Server::stop
|
||||||
|
|
||||||
|
Stop server instance.
|
||||||
|
|
||||||
|
Same to `Server::setLive(false)`
|
||||||
|
|
|
||||||
|
|
@ -7,11 +7,13 @@ class Server
|
||||||
private string $_host;
|
private string $_host;
|
||||||
private int $_port;
|
private int $_port;
|
||||||
private int $_size;
|
private int $_size;
|
||||||
|
private bool $_live;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
string $host = '127.0.0.1',
|
string $host = '127.0.0.1',
|
||||||
int $port = 1915,
|
int $port = 1915,
|
||||||
int $size = 1024
|
int $size = 1024,
|
||||||
|
bool $live = true
|
||||||
) {
|
) {
|
||||||
$this->setHost(
|
$this->setHost(
|
||||||
$host
|
$host
|
||||||
|
|
@ -24,6 +26,10 @@ class Server
|
||||||
$this->setSize(
|
$this->setSize(
|
||||||
$size
|
$size
|
||||||
);
|
);
|
||||||
|
|
||||||
|
$this->setLive(
|
||||||
|
$live
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getHost(): string
|
public function getHost(): string
|
||||||
|
|
@ -75,6 +81,18 @@ class Server
|
||||||
$this->_size = $value;
|
$this->_size = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getLive(): bool
|
||||||
|
{
|
||||||
|
return $this->_live;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setLive(
|
||||||
|
bool $value
|
||||||
|
): void
|
||||||
|
{
|
||||||
|
$this->_live = $value;
|
||||||
|
}
|
||||||
|
|
||||||
public function start(
|
public function start(
|
||||||
?callable $handler = null
|
?callable $handler = null
|
||||||
): void
|
): void
|
||||||
|
|
@ -90,8 +108,17 @@ class Server
|
||||||
STREAM_SERVER_BIND | STREAM_SERVER_LISTEN
|
STREAM_SERVER_BIND | STREAM_SERVER_LISTEN
|
||||||
);
|
);
|
||||||
|
|
||||||
while ($socket)
|
do
|
||||||
{
|
{
|
||||||
|
if (!$this->_live)
|
||||||
|
{
|
||||||
|
fclose(
|
||||||
|
$socket
|
||||||
|
);
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
$incoming = stream_socket_accept(
|
$incoming = stream_socket_accept(
|
||||||
$socket, -1, $connect
|
$socket, -1, $connect
|
||||||
);
|
);
|
||||||
|
|
@ -123,6 +150,14 @@ class Server
|
||||||
fclose(
|
fclose(
|
||||||
$incoming
|
$incoming
|
||||||
);
|
);
|
||||||
}
|
|
||||||
|
} while ($this->_live);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function stop(): void
|
||||||
|
{
|
||||||
|
$this->setLive(
|
||||||
|
false
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue