diff --git a/README.md b/README.md index 70683bf..1581e5e 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/Server.php b/src/Server.php index b465a2c..b877c6e 100644 --- a/src/Server.php +++ b/src/Server.php @@ -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, @@ -111,10 +115,53 @@ class Server $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', @@ -141,6 +188,22 @@ class Server $socket, -1, $connect ); + if ($this->_welcome) + { + $response = call_user_func( + $this->_welcome, + $connect + ); + + if ($response) + { + fwrite( + $incoming, + $response + ); + } + } + stream_set_blocking( $incoming, true @@ -151,6 +214,23 @@ class Server $this->_line ); + if ($this->_pending) + { + $response = call_user_func( + $this->_pending, + $request, + $connect + ); + + if ($response) + { + fwrite( + $incoming, + $response + ); + } + } + $success = true; $content = ''; @@ -185,10 +265,10 @@ class Server false ); - if ($handler) + if ($this->_handler) { $response = call_user_func( - $handler, + $this->_handler, $success, $content, $request,