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

Commit

Permalink
implement server class
Browse files Browse the repository at this point in the history
  • Loading branch information
yggverse committed Apr 24, 2024
1 parent 0931d2e commit 0197b36
Show file tree
Hide file tree
Showing 2 changed files with 190 additions and 3 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
PHP 8 / Composer Library for NPS Protocol

Unlike Titan for Gemini, NPS is the satellite for Nex protocol (see also [nex-php](https://github.com/YGGverse/nex-php))\
it listen for dot in line to signal the package ending.
it listen for single dot in line to signal the package ending.

## Specification

Expand Down Expand Up @@ -52,9 +52,12 @@ $server->start(
string $connect
) {
printf(
'connection: %s request: %s content: %s',
'connection: %s request: %s',
$connect,
$request,
$request
);

var_dump(
$content
);
}
Expand Down
184 changes: 184 additions & 0 deletions src/Server.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
<?php

namespace Yggverse\Nps;

class Server
{
private string $_host;
private int $_port;
private int $_size;
private bool $_live;

public function __construct(
string $host = '127.0.0.1',
int $port = 1915,
int $size = 1024,
bool $live = true
) {
$this->setHost(
$host
);

$this->setPort(
$port
);

$this->setSize(
$size
);

$this->setLive(
$live
);
}

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 getLive(): bool
{
return $this->_live;
}

public function setLive(
bool $value
): void
{
$this->_live = $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
);

do
{
if (!$this->_live)
{
fclose(
$socket
);

break;
}

$incoming = stream_socket_accept(
$socket, -1, $connect
);

stream_set_blocking(
$incoming,
true
);

$request = fread(
$incoming,
$this->_size
);

$content = '';

do
{
$line = trim(
fread(
$incoming,
$this->_size
)
);

if ($line == '.')
{
break;
}

$content .= $line;

} while ($this->_live);

stream_set_blocking(
$incoming,
false
);

fclose(
$incoming
);

if ($handler)
{
$response = call_user_func(
$handler,
$content,
$request,
$connect
);
}

} while ($this->_live);
}

public function stop(): void
{
$this->setLive(
false
);
}
}

0 comments on commit 0197b36

Please sign in to comment.