mirror of
https://github.com/YGGverse/nex-php.git
synced 2025-03-13 06:01:25 +00:00
implement nex protocol server
This commit is contained in:
parent
ae8cdbeeaf
commit
dff658c1f2
42
README.md
42
README.md
@ -39,4 +39,44 @@ var_dump(
|
||||
#### Request::setQuery
|
||||
#### Request::getQuery
|
||||
#### Request::getOptions
|
||||
#### Request::setOptions
|
||||
#### Request::setOptions
|
||||
|
||||
## Server
|
||||
|
||||
Build interactive server instance to listen Nex protocol connections!
|
||||
|
||||
``` php
|
||||
$server = new \Yggverse\Nex\Server;
|
||||
```
|
||||
|
||||
Provide optional `host`, `port` and `size` arguments in constructor or use available setters after object initiation.
|
||||
|
||||
``` php
|
||||
$server = new \Yggverse\Nex\Server('127.0.0.1', 1915);
|
||||
```
|
||||
|
||||
#### Server::setHost
|
||||
#### Server::getHost
|
||||
#### Server::setPort
|
||||
#### Server::getPort
|
||||
|
||||
#### Server::start
|
||||
|
||||
Run server object using this method.
|
||||
|
||||
Define handler function as the argument to process application logic dependent of client request.
|
||||
|
||||
``` php
|
||||
$server->start(
|
||||
function (
|
||||
string $request,
|
||||
string $connect
|
||||
) {
|
||||
printf(
|
||||
'connection: %s request: %s',
|
||||
$connect,
|
||||
$request
|
||||
);
|
||||
}
|
||||
);
|
||||
```
|
||||
|
128
src/Server.php
Normal file
128
src/Server.php
Normal file
@ -0,0 +1,128 @@
|
||||
<?php
|
||||
|
||||
namespace Yggverse\Nex;
|
||||
|
||||
class Server
|
||||
{
|
||||
private string $_host;
|
||||
private int $_port;
|
||||
private int $_size;
|
||||
|
||||
public function __construct(
|
||||
string $host = '127.0.0.1',
|
||||
int $port = 1915,
|
||||
int $size = 1024
|
||||
) {
|
||||
$this->setHost(
|
||||
$host
|
||||
);
|
||||
|
||||
$this->setPort(
|
||||
$port
|
||||
);
|
||||
|
||||
$this->setSize(
|
||||
$size
|
||||
);
|
||||
}
|
||||
|
||||
public function getHost(): string
|
||||
{
|
||||
return $this->_host;
|
||||
}
|
||||
|
||||
public function setHost(
|
||||
string $value
|
||||
): void
|
||||
{
|
||||
if (false === filter_var($value, FILTER_VALIDATE_IP))
|
||||
{
|
||||
throw new \Exception();
|
||||
}
|
||||
|
||||
if (strpos($value, ':'))
|
||||
{
|
||||
$value = sprintf(
|
||||
'[%s]',
|
||||
$value
|
||||
);
|
||||
}
|
||||
|
||||
$this->_host = $value;
|
||||
}
|
||||
|
||||
public function getPort(): int
|
||||
{
|
||||
return $this->_port;
|
||||
}
|
||||
|
||||
public function setPort(
|
||||
int $value
|
||||
): void
|
||||
{
|
||||
$this->_port = $value;
|
||||
}
|
||||
|
||||
public function getSize(): int
|
||||
{
|
||||
return $this->_size;
|
||||
}
|
||||
|
||||
public function setSize(
|
||||
int $value
|
||||
): void
|
||||
{
|
||||
$this->_size = $value;
|
||||
}
|
||||
|
||||
public function start(
|
||||
?callable $handler = null
|
||||
): void
|
||||
{
|
||||
$socket = stream_socket_server(
|
||||
sprintf(
|
||||
'tcp://%s:%d',
|
||||
$this->_host,
|
||||
$this->_port
|
||||
),
|
||||
$error,
|
||||
$message,
|
||||
STREAM_SERVER_BIND | STREAM_SERVER_LISTEN
|
||||
);
|
||||
|
||||
while ($socket)
|
||||
{
|
||||
$incoming = stream_socket_accept(
|
||||
$socket, -1, $connect
|
||||
);
|
||||
|
||||
stream_set_blocking(
|
||||
$incoming,
|
||||
true
|
||||
);
|
||||
|
||||
$request = fread(
|
||||
$incoming,
|
||||
$this->_size
|
||||
);
|
||||
|
||||
stream_set_blocking(
|
||||
$incoming,
|
||||
false
|
||||
);
|
||||
|
||||
if ($handler)
|
||||
{
|
||||
$response = call_user_func(
|
||||
$handler,
|
||||
$request,
|
||||
$connect
|
||||
);
|
||||
}
|
||||
|
||||
fclose(
|
||||
$incoming
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user