diff --git a/examples/api-skeleton/bin/swow.php b/examples/api-skeleton/bin/swow.php index 7133c8ff..63072c23 100644 --- a/examples/api-skeleton/bin/swow.php +++ b/examples/api-skeleton/bin/swow.php @@ -7,7 +7,7 @@ ini_set('memory_limit', '1G'); date_default_timezone_set('Asia/Shanghai'); -!defined('BASE_PATH') && define('BASE_PATH', dirname(__DIR__, 1)); +! defined('BASE_PATH') && define('BASE_PATH', dirname(__DIR__, 1)); require BASE_PATH . '/vendor/autoload.php'; @@ -15,14 +15,103 @@ use App\Container\Logger; use App\Error; use App\Vega; use Dotenv\Dotenv; -use App\Container\Swow\Http\Server; +use Swow\CoroutineException; +use Swow\Errno; +use Swow\Http\ResponseException; +use Swow\Http\Server as HttpServer; +use Swow\Socket; +use Swow\SocketException; +use function Swow\Sync\waitAll; Dotenv::createUnsafeImmutable(__DIR__ . '/../', '.env')->load(); define("APP_DEBUG", env('APP_DEBUG')); Error::register(); +class SwowServer extends HttpServer +{ + /** + * @var string|null + */ + public $host = null; + + /** + * @var int|null + */ + public $port = null; + + /** + * @var callable + */ + protected $handler; + + /** + * @param string $name + * @param int $port + * @param int $flags + * @return static + */ + public function bind(string $name, int $port = 0, int $flags = Socket::BIND_FLAG_NONE): static + { + $this->host = $name; + $this->port = $port; + parent::bind($name, $port, $flags); + return $this; + } + + public function handle(callable $callable) + { + $this->handler = $callable; + return $this; + } + + public function start() + { + $this->listen(); + \Swow\Coroutine::run(function () { + while (true) { + try { + $connection = $this->acceptConnection(); + \Swow\Coroutine::run(function () use ($connection) { + try { + while (true) { + $request = null; + try { + $request = $connection->recvHttpRequest(); + $handler = $this->handler; + $handler($request, $connection); + } catch (ResponseException $exception) { + $connection->error($exception->getCode(), $exception->getMessage()); + } + if (!$request || !$request->getKeepAlive()) { + break; + } + } + } catch (\Throwable $exception) { + Logger::instance()->error((string)$exception); + } finally { + $connection->close(); + } + }); + } catch (SocketException|CoroutineException $exception) { + if (in_array($exception->getCode(), [Errno::EMFILE, Errno::ENFILE, Errno::ENOMEM], true)) { + Logger::instance()->warning('Socket resources have been exhausted.'); + sleep(1); + } else { + Logger::instance()->error((string)$exception); + break; + } + } catch (\Throwable $exception) { + Logger::instance()->error((string)$exception); + } + } + }); + + waitAll(); + } +} + $vega = Vega::new(); -$server = new Server(); +$server = new SwowServer(); $host = '0.0.0.0'; $port = 9501; $server->bind($host, $port)->handle($vega->handler()); diff --git a/examples/api-skeleton/src/Container/Swow/Coroutine.php b/examples/api-skeleton/src/Container/Swow/Coroutine.php deleted file mode 100644 index 7a881e05..00000000 --- a/examples/api-skeleton/src/Container/Swow/Coroutine.php +++ /dev/null @@ -1,136 +0,0 @@ -context = new ArrayObject(); - $this->parentId = static::getCurrent()->getId(); - } - - public function __destruct() - { - while (!empty($this->deferCallbacks)) { - array_shift($this->deferCallbacks)(); - } - } - - /** - * @param ...$data - * @return static - */ - public function execute(...$data) - { - $this->resume(...$data); - - return $this; - } - - public function getContext() - { - return $this->context; - } - - public function getParentId(): int - { - return $this->parentId; - } - - public function addDefer(callable $callable) - { - array_unshift($this->deferCallbacks, $callable); - } - - /** - * @param callable $callable - * @param ...$data - * @return static - */ - public static function create(callable $callable, ...$data) - { - $coroutine = new self($callable); - $coroutine->resume(...$data); - return $coroutine; - } - - public static function id(): int - { - return static::getCurrent()->getId(); - } - - public static function pid(?int $id = null): int - { - if ($id === null) { - $coroutine = static::getCurrent(); - if ($coroutine instanceof static) { - return static::getCurrent()->getParentId(); - } - return 0; - } - - $coroutine = static::get($id); - if ($coroutine === null) { - throw new CoroutineDestroyedException(sprintf('Coroutine #%d has been destroyed.', $id)); - } - - return $coroutine->getParentId(); - } - - public static function set(array $config): void - { - } - - /** - * @param int|null $id - * @return ArrayObject|null - */ - public static function getContextFor(?int $id = null): ?ArrayObject - { - $coroutine = is_null($id) ? static::getCurrent() : static::get($id); - if ($coroutine === null) { - return null; - } - if ($coroutine instanceof static) { - return $coroutine->getContext(); - } - if (static::$mainContext === null) { - static::$mainContext = new ArrayObject(); - } - return static::$mainContext; - } - - public static function defer(callable $callable): void - { - $coroutine = static::getCurrent(); - if ($coroutine instanceof static) { - $coroutine->addDefer($callable); - } - } -} \ No newline at end of file diff --git a/examples/api-skeleton/src/Container/Swow/Exception/CoroutineDestroyedException.php b/examples/api-skeleton/src/Container/Swow/Exception/CoroutineDestroyedException.php deleted file mode 100644 index 588d495a..00000000 --- a/examples/api-skeleton/src/Container/Swow/Exception/CoroutineDestroyedException.php +++ /dev/null @@ -1,8 +0,0 @@ -host = $name; - $this->port = $port; - parent::bind($name, $port, $flags); - return $this; - } - - public function handle(callable $callable) - { - $this->handler = $callable; - return $this; - } - - public function start() - { - $this->listen(); - Coroutine::create(function () { - while (true) { - try { - $connection = $this->acceptConnection(); - Coroutine::create(function () use ($connection) { - try { - while (true) { - $request = null; - try { - $request = $connection->recvHttpRequest(); - $handler = $this->handler; - $handler($request, $connection); - } catch (ResponseException $exception) { - $connection->error($exception->getCode(), $exception->getMessage()); - } - if (!$request || !$request->getKeepAlive()) { - break; - } - } - } catch (\Throwable $exception) { - Logger::instance()->error((string)$exception); - } finally { - $connection->close(); - } - }); - } catch (SocketException|CoroutineException $exception) { - if (in_array($exception->getCode(), [Errno::EMFILE, Errno::ENFILE, Errno::ENOMEM], true)) { - Logger::instance()->warning('Socket resources have been exhausted.'); - sleep(1); - } else { - Logger::instance()->error((string)$exception); - break; - } - } catch (\Throwable $exception) { - Logger::instance()->error((string)$exception); - } - } - }); - - waitAll(); - } -} \ No newline at end of file diff --git a/examples/api-skeleton/src/Container/Swow/Swow.php b/examples/api-skeleton/src/Container/Swow/Swow.php deleted file mode 100644 index 4e0138e1..00000000 --- a/examples/api-skeleton/src/Container/Swow/Swow.php +++ /dev/null @@ -1,66 +0,0 @@ -host = $name; - $this->port = $port; - parent::bind($name, $port, $flags); - return $this; - } - - public function handle(callable $callable) - { - $this->handler = $callable; - return $this; - } - - public function start() - { - $this->listen(); - while (true) { - Coroutine::create($this->handler, $this->accept()); - } - } -} \ No newline at end of file diff --git a/examples/web-skeleton/bin/swow.php b/examples/web-skeleton/bin/swow.php index 7133c8ff..9e2278b4 100644 --- a/examples/web-skeleton/bin/swow.php +++ b/examples/web-skeleton/bin/swow.php @@ -7,7 +7,7 @@ ini_set('memory_limit', '1G'); date_default_timezone_set('Asia/Shanghai'); -!defined('BASE_PATH') && define('BASE_PATH', dirname(__DIR__, 1)); +! defined('BASE_PATH') && define('BASE_PATH', dirname(__DIR__, 1)); require BASE_PATH . '/vendor/autoload.php'; @@ -15,14 +15,103 @@ use App\Container\Logger; use App\Error; use App\Vega; use Dotenv\Dotenv; -use App\Container\Swow\Http\Server; +use Swow\CoroutineException; +use Swow\Errno; +use Swow\Http\ResponseException; +use Swow\Http\Server as HttpServer; +use Swow\Socket; +use Swow\SocketException; +use function Swow\Sync\waitAll; Dotenv::createUnsafeImmutable(__DIR__ . '/../', '.env')->load(); define("APP_DEBUG", env('APP_DEBUG')); Error::register(); +class SwowServer extends HttpServer +{ + /** + * @var string|null + */ + public $host = null; + + /** + * @var int|null + */ + public $port = null; + + /** + * @var callable + */ + protected $handler; + + /** + * @param string $name + * @param int $port + * @param int $flags + * @return static + */ + public function bind(string $name, int $port = 0, int $flags = Socket::BIND_FLAG_NONE): static + { + $this->host = $name; + $this->port = $port; + parent::bind($name, $port, $flags); + return $this; + } + + public function handle(callable $callable) + { + $this->handler = $callable; + return $this; + } + + public function start() + { + $this->listen(); + \Swow\Coroutine::run(function () { + while (true) { + try { + $connection = $this->acceptConnection(); + \Swow\Coroutine::run(function () use ($connection) { + try { + while (true) { + $request = null; + try { + $request = $connection->recvHttpRequest(); + $handler = $this->handler; + $handler($request, $connection); + } catch (ResponseException $exception) { + $connection->error($exception->getCode(), $exception->getMessage()); + } + if (!$request || !$request->getKeepAlive()) { + break; + } + } + } catch (\Throwable $exception) { + Logger::instance()->error((string)$exception); + } finally { + $connection->close(); + } + }); + } catch (SocketException|CoroutineException $exception) { + if (in_array($exception->getCode(), [Errno::EMFILE, Errno::ENFILE, Errno::ENOMEM], true)) { + Logger::instance()->warning('Socket resources have been exhausted.'); + sleep(1); + } else { + Logger::instance()->error((string)$exception); + break; + } + } catch (\Throwable $exception) { + Logger::instance()->error((string)$exception); + } + } + }); + + waitAll(); + } +} + $vega = Vega::new(); -$server = new Server(); +$server = new SwowServer(); $host = '0.0.0.0'; $port = 9501; $server->bind($host, $port)->handle($vega->handler()); @@ -41,7 +130,4 @@ printf("PHP Version: %s\n", PHP_VERSION); printf("Swow Version: %s\n", '0.1.0'); printf("Listen Addr: http://%s:%d\n", $host, $port); Logger::instance()->info('Start swow coroutine server'); -$server->start(); - - - +$server->start(); \ No newline at end of file diff --git a/examples/web-skeleton/src/Container/Swow/Coroutine.php b/examples/web-skeleton/src/Container/Swow/Coroutine.php deleted file mode 100644 index 7a881e05..00000000 --- a/examples/web-skeleton/src/Container/Swow/Coroutine.php +++ /dev/null @@ -1,136 +0,0 @@ -context = new ArrayObject(); - $this->parentId = static::getCurrent()->getId(); - } - - public function __destruct() - { - while (!empty($this->deferCallbacks)) { - array_shift($this->deferCallbacks)(); - } - } - - /** - * @param ...$data - * @return static - */ - public function execute(...$data) - { - $this->resume(...$data); - - return $this; - } - - public function getContext() - { - return $this->context; - } - - public function getParentId(): int - { - return $this->parentId; - } - - public function addDefer(callable $callable) - { - array_unshift($this->deferCallbacks, $callable); - } - - /** - * @param callable $callable - * @param ...$data - * @return static - */ - public static function create(callable $callable, ...$data) - { - $coroutine = new self($callable); - $coroutine->resume(...$data); - return $coroutine; - } - - public static function id(): int - { - return static::getCurrent()->getId(); - } - - public static function pid(?int $id = null): int - { - if ($id === null) { - $coroutine = static::getCurrent(); - if ($coroutine instanceof static) { - return static::getCurrent()->getParentId(); - } - return 0; - } - - $coroutine = static::get($id); - if ($coroutine === null) { - throw new CoroutineDestroyedException(sprintf('Coroutine #%d has been destroyed.', $id)); - } - - return $coroutine->getParentId(); - } - - public static function set(array $config): void - { - } - - /** - * @param int|null $id - * @return ArrayObject|null - */ - public static function getContextFor(?int $id = null): ?ArrayObject - { - $coroutine = is_null($id) ? static::getCurrent() : static::get($id); - if ($coroutine === null) { - return null; - } - if ($coroutine instanceof static) { - return $coroutine->getContext(); - } - if (static::$mainContext === null) { - static::$mainContext = new ArrayObject(); - } - return static::$mainContext; - } - - public static function defer(callable $callable): void - { - $coroutine = static::getCurrent(); - if ($coroutine instanceof static) { - $coroutine->addDefer($callable); - } - } -} \ No newline at end of file diff --git a/examples/web-skeleton/src/Container/Swow/Exception/CoroutineDestroyedException.php b/examples/web-skeleton/src/Container/Swow/Exception/CoroutineDestroyedException.php deleted file mode 100644 index 588d495a..00000000 --- a/examples/web-skeleton/src/Container/Swow/Exception/CoroutineDestroyedException.php +++ /dev/null @@ -1,8 +0,0 @@ -host = $name; - $this->port = $port; - parent::bind($name, $port, $flags); - return $this; - } - - public function handle(callable $callable) - { - $this->handler = $callable; - return $this; - } - - public function start() - { - $this->listen(); - Coroutine::create(function () { - while (true) { - try { - $connection = $this->acceptConnection(); - Coroutine::create(function () use ($connection) { - try { - while (true) { - $request = null; - try { - $request = $connection->recvHttpRequest(); - $handler = $this->handler; - $handler($request, $connection); - } catch (ResponseException $exception) { - $connection->error($exception->getCode(), $exception->getMessage()); - } - if (!$request || !$request->getKeepAlive()) { - break; - } - } - } catch (\Throwable $exception) { - Logger::instance()->error((string)$exception); - } finally { - $connection->close(); - } - }); - } catch (SocketException|CoroutineException $exception) { - if (in_array($exception->getCode(), [Errno::EMFILE, Errno::ENFILE, Errno::ENOMEM], true)) { - Logger::instance()->warning('Socket resources have been exhausted.'); - sleep(1); - } else { - Logger::instance()->error((string)$exception); - break; - } - } catch (\Throwable $exception) { - Logger::instance()->error((string)$exception); - } - } - }); - - waitAll(); - } -} \ No newline at end of file diff --git a/examples/web-skeleton/src/Container/Swow/Swow.php b/examples/web-skeleton/src/Container/Swow/Swow.php deleted file mode 100644 index 4e0138e1..00000000 --- a/examples/web-skeleton/src/Container/Swow/Swow.php +++ /dev/null @@ -1,66 +0,0 @@ -host = $name; - $this->port = $port; - parent::bind($name, $port, $flags); - return $this; - } - - public function handle(callable $callable) - { - $this->handler = $callable; - return $this; - } - - public function start() - { - $this->listen(); - while (true) { - Coroutine::create($this->handler, $this->accept()); - } - } -} \ No newline at end of file