Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
implement welcome, pending, handler features
Browse files Browse the repository at this point in the history
  • Loading branch information
yggverse committed Apr 25, 2024
1 parent 5a68a95 commit 5e662c3
Show file tree
Hide file tree
Showing 2 changed files with 175 additions and 2 deletions.
93 changes: 93 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,99 @@ Set server status `true`|`false` to shutdown immediately

Get current server status

#### Server::setWelcome

Define application logic on peer connection established

``` php
$server->setWelcome(
function (
string $connect
): ?string
{
printf(
"connected: %s\n\r",
$connect
);

return sprintf(
"welcome, %s\n\r",
$connect
);
}
);
```

#### Server::getWelcome

Get current `Welcome` function, `null` by default

#### Server::setPending

Define application logic on peer make initial request

``` php
$server->setPending(
function (
string $request,
string $connect
): ?string
{
printf(
"connection: %s requested: %s",
$connect,
$request,
);

return sprintf(
"received: %s",
$request
);
}
);
```

#### Server::getPending

Get current `Pending` function, `null` by default

#### Server::setHandler

Define basic application logic on complete packet received.

Could be also defined as [Server::start](https://github.com/YGGverse/nps-php#serverstart) argument.

``` php
$server->setHandler(
function (
bool $success,
string $content,
string $request,
string $connect
): ?string
{
printf(
'connection: %s request: %s',
$connect,
$request
);

if ($success)
{
var_dump(
$content
);
}

return "thank you!\n\r";
}
);
```

#### Server::getHandler

Get current `Handler` function, `null` by default

#### Server::start

Run server object using this method.
Expand Down
84 changes: 82 additions & 2 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ class Server
private int $_line;
private bool $_live;

private $_welcome = null;
private $_pending = null;
private $_handler = null;

public function __construct(
string $host = '127.0.0.1',
int $port = 1915,
Expand Down Expand Up @@ -111,10 +115,53 @@ public function setLive(
$this->_live = $value;
}

public function getWelcome(): callable
{
return $this->_welcome;
}

public function setWelcome(
callable $function
): void
{
$this->_welcome = $function;
}

public function getPending(): callable
{
return $this->_pending;
}

public function setPending(
callable $function
): void
{
$this->_pending = $function;
}

public function getHandler(): callable
{
return $this->_handler;
}

public function setHandler(
callable $function
): void
{
$this->_handler = $function;
}

public function start(
?callable $handler = null
): void
{
if ($handler)
{
$this->setHandler(
$handler
);
}

$socket = stream_socket_server(
sprintf(
'tcp://%s:%d',
Expand All @@ -141,6 +188,22 @@ public function start(
$socket, -1, $connect
);

if ($this->_welcome)
{
$response = call_user_func(
$this->_welcome,
$connect
);

if ($response)
{
fwrite(
$incoming,
$response
);
}
}

stream_set_blocking(
$incoming,
true
Expand All @@ -151,6 +214,23 @@ public function start(
$this->_line
);

if ($this->_pending)
{
$response = call_user_func(
$this->_pending,
$request,
$connect
);

if ($response)
{
fwrite(
$incoming,
$response
);
}
}

$success = true;

$content = '';
Expand Down Expand Up @@ -185,10 +265,10 @@ public function start(
false
);

if ($handler)
if ($this->_handler)
{
$response = call_user_func(
$handler,
$this->_handler,
$success,
$content,
$request,
Expand Down

0 comments on commit 5e662c3

Please sign in to comment.