PHP / Composer Library for NPS Protocol
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

223 lines
4.1 KiB

5 months ago
# nps-php
5 months ago
5 months ago
PHP 8 / Composer Library for NPS Protocol
5 months ago
5 months ago
Like Titan for Gemini, NPS is the satellite for NEX protocol (see also [nex-php](https://github.com/YGGverse/nex-php))\
it listen for single dot in line to signal the package ending.
5 months ago
5 months ago
## Specification
`nex://nightfall.city/nps/`
5 months ago
## Usage
```
composer require yggverse/nps
```
## Server
Build interactive server instance to listen NPS protocol connections!
``` php
$server = new \Yggverse\Nps\Server;
```
5 months ago
Provide optional `host`, `port`, `size`, `line` and `live` arguments in constructor:
5 months ago
``` php
$server = new \Yggverse\Nps\Server('127.0.0.1', 1915);
```
5 months ago
Alternatively, use following setters after object initiation
5 months ago
5 months ago
#### Server::setHost
5 months ago
Bind server host to listen incoming connections, `127.0.0.1` by default
5 months ago
#### Server::getHost
5 months ago
Get current server host
5 months ago
#### Server::setPort
5 months ago
Bind server port to listen incoming connections, `1915` by default
5 months ago
#### Server::getPort
5 months ago
Get current server port
5 months ago
#### Server::setSize
5 months ago
Set total content length limit by [mb_strlen](https://www.php.net/manual/en/function.mb-strlen.php), `0` by default (unlimited)
5 months ago
#### Server::getSize
5 months ago
Get current content length limit
#### Server::setLine
Set packet line limit in bytes passing to [fread](https://www.php.net/manual/en/function.fread.php#length), `1024` by default
#### Server::getLine
Get current packet line limit
5 months ago
#### Server::setLive
5 months ago
Set server status `true`|`false` to shutdown immediately
5 months ago
#### Server::getLive
5 months ago
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
5 months ago
#### 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 (
5 months ago
bool $success,
5 months ago
string $content,
5 months ago
string $request,
string $connect
): ?string
{
5 months ago
printf(
'connection: %s request: %s',
5 months ago
$connect,
$request
);
5 months ago
if ($success)
{
var_dump(
$content
);
}
5 months ago
return 'thank you!'; // null|string response
5 months ago
}
);
```
#### Server::stop
Stop server instance.
Same to `Server::setLive(false)`
5 months ago
5 months ago
### Testing
5 months ago
1. `nc 127.0.0.1 1915` - connect server using `nc`
5 months ago
2. `test` - enter the target path
5 months ago
3. `YOUR MESSAGE GOES HERE` - enter the message text
4. `.` - commit package with dot
To send any file:
``` file.txt
5 months ago
test
5 months ago
╦ ╦╔═╗╔═╗╔╦╗╦═╗╔═╗╔═╗╦╦
╚╦╝║ ╦║ ╦ ║║╠╦╝╠═╣╚═╗║║
╩ ╚═╝╚═╝═╩╝╩╚═╩ ╩╚═╝╩╩═╝
.
```
5 months ago
`cat file.txt | nc 127.0.0.1 1915`