nps-php/README.md

227 lines
4.2 KiB
Markdown
Raw Normal View History

2024-04-24 21:04:34 +03:00
# nps-php
2024-04-24 21:22:13 +03:00
2024-04-24 22:29:07 +03:00
PHP 8 / Composer Library for NPS Protocol
2024-04-24 21:22:13 +03:00
2024-04-24 23:12:17 +03:00
Like Titan for Gemini, NPS is the satellite for NEX protocol (see also [nex-php](https://github.com/YGGverse/nex-php))\
2024-04-24 22:59:50 +03:00
it listen for single dot in line to signal the package ending.
2024-04-24 22:09:39 +03:00
2024-04-24 22:23:24 +03:00
## Specification
`nex://nightfall.city/nps/`
2024-04-24 21:22:13 +03:00
## Usage
```
composer require yggverse/nps
```
## Server
Build interactive server instance to listen NPS protocol connections!
``` php
$server = new \Yggverse\Nps\Server;
```
2024-04-25 07:04:15 +03:00
Provide optional `host`, `port`, `size`, `line` and `live` arguments in constructor:
2024-04-24 21:22:13 +03:00
``` php
$server = new \Yggverse\Nps\Server('127.0.0.1', 1915);
```
2024-04-25 07:04:59 +03:00
Alternatively, use following setters after object initiation
2024-04-25 07:04:15 +03:00
2024-04-24 21:22:13 +03:00
#### Server::setHost
2024-04-25 06:59:05 +03:00
Bind server host to listen incoming connections, `127.0.0.1` by default
2024-04-24 21:22:13 +03:00
#### Server::getHost
2024-04-25 06:59:05 +03:00
Get current server host
2024-04-24 21:22:13 +03:00
#### Server::setPort
2024-04-25 06:59:05 +03:00
Bind server port to listen incoming connections, `1915` by default
2024-04-24 21:22:13 +03:00
#### Server::getPort
2024-04-25 06:59:05 +03:00
Get current server port
2024-04-24 21:22:13 +03:00
#### Server::setSize
2024-04-25 06:59:05 +03:00
Set total content length limit by [mb_strlen](https://www.php.net/manual/en/function.mb-strlen.php), `0` by default (unlimited)
2024-04-24 21:22:13 +03:00
#### Server::getSize
2024-04-25 06:59:05 +03:00
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
2024-04-24 21:22:13 +03:00
#### Server::setLive
2024-04-25 06:59:05 +03:00
Set server status `true`|`false` to shutdown immediately
2024-04-24 21:22:13 +03:00
#### Server::getLive
2024-04-25 06:59:05 +03:00
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
2024-04-25 16:43:49 +03:00
Define basic application logic on complete packet received
2024-04-25 16:43:49 +03:00
* 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
);
}
2024-04-25 16:43:49 +03:00
return 'thank you!';
}
);
```
#### Server::getHandler
Get current `Handler` function, `null` by default
2024-04-24 21:22:13 +03:00
#### Server::start
2024-04-25 16:45:30 +03:00
Run server object
2024-04-24 21:22:13 +03:00
2024-04-25 16:45:07 +03:00
``` php
$server->start();
```
Optionally, define handler function as the argument to process application logic dependent of client request
2024-04-24 21:22:13 +03:00
``` php
$server->start(
function (
2024-04-25 06:59:05 +03:00
bool $success,
2024-04-24 22:29:07 +03:00
string $content,
2024-04-24 21:22:13 +03:00
string $request,
string $connect
2024-04-25 14:51:13 +03:00
): ?string
{
2024-04-24 21:22:13 +03:00
printf(
2024-04-24 22:59:50 +03:00
'connection: %s request: %s',
2024-04-24 21:22:13 +03:00
$connect,
2024-04-24 22:59:50 +03:00
$request
);
2024-04-25 06:59:05 +03:00
if ($success)
{
var_dump(
$content
);
}
2024-04-25 14:51:13 +03:00
2024-04-25 15:23:40 +03:00
return 'thank you!'; // null|string response
2024-04-24 21:22:13 +03:00
}
);
```
#### Server::stop
Stop server instance.
Same to `Server::setLive(false)`
2024-04-24 22:16:45 +03:00
2024-04-24 23:04:09 +03:00
### Testing
2024-04-24 22:16:45 +03:00
1. `nc 127.0.0.1 1915` - connect server using `nc`
2024-04-24 22:18:33 +03:00
2. `test` - enter the target path
2024-04-24 22:16:45 +03:00
3. `YOUR MESSAGE GOES HERE` - enter the message text
4. `.` - commit package with dot
To send any file:
``` file.txt
2024-04-24 22:18:33 +03:00
test
2024-04-24 22:16:45 +03:00
╦ ╦╔═╗╔═╗╔╦╗╦═╗╔═╗╔═╗╦╦
╚╦╝║ ╦║ ╦ ║║╠╦╝╠═╣╚═╗║║
╩ ╚═╝╚═╝═╩╝╩╚═╩ ╩╚═╝╩╩═╝
.
```
2024-04-24 22:23:24 +03:00
`cat file.txt | nc 127.0.0.1 1915`