diff --git a/HttpServer/Http/HttpParams.php b/HttpServer/Http/HttpParams.php index 4a39a55a..edd14c21 100644 --- a/HttpServer/Http/HttpParams.php +++ b/HttpServer/Http/HttpParams.php @@ -263,17 +263,7 @@ class HttpParams */ public function int(string $name, bool $isNeed = FALSE, array|int|null $min = NULL, int|null $max = NULL): ?int { - $int = $this->required($name, $isNeed); - if (is_null($int)) return null; - if (is_array($min)) { - list($min, $max) = $min; - } - $length = strlen((string)$int); - if (!is_numeric($int) || intval($int) != $int) { - throw new RequestException("The request parameter $name must integer."); - } - $this->between($length, $min, $max); - return (int)$int; + return (int)$this->required($name, $isNeed); } /** @@ -285,15 +275,7 @@ class HttpParams */ public function float(string $name, bool $isNeed = FALSE, int $round = 0): ?float { - $int = $this->required($name, $isNeed); - if ($int === null) { - return null; - } - if ($round > 0) { - return round(floatval($int), $round); - } else { - return floatval($int); - } + return (float)$this->required($name, $isNeed); } /** @@ -306,37 +288,7 @@ class HttpParams */ public function string(string $name, bool $isNeed = FALSE, int|array|null $length = NULL): ?string { - $string = $this->required($name, $isNeed); - if ($string === null || $length === null) { - return $string; - } - if (is_numeric($length)) { - if ($length !== mb_strlen((string)$string)) { - throw new RequestException("The minimum value cannot be lower than $length, has length $length"); - } - return $string; - } else { - return $this->between($string, $length[0], $length[1] ?? $length[0]); - } - } - - /** - * @param $string - * @param $min - * @param $max - * @return mixed - * @throws RequestException - */ - private function between($string, $min, $max): mixed - { - $_length = mb_strlen((string)$string); - if ($min !== NULL && $_length < $min) { - throw new RequestException("The minimum value cannot be lower than $min, has length $_length"); - } - if ($max !== NULL && $_length > $max) { - throw new RequestException("Maximum cannot exceed $max, has length " . $_length); - } - return $string; + return (string)$this->required($name, $isNeed); } /** @@ -368,11 +320,7 @@ class HttpParams */ public function bool(string $name, bool $isNeed = FALSE): bool { - $email = $this->required($name, $isNeed); - if ($email === null) { - return false; - } - return (bool)$email; + return (boolean)$this->required($name, $isNeed); } /** diff --git a/HttpServer/Server.php b/HttpServer/Server.php index d386b453..53761863 100644 --- a/HttpServer/Server.php +++ b/HttpServer/Server.php @@ -304,10 +304,10 @@ class Server extends HttpService return; } foreach ($config['events'] as $name => $_event) { - if ($name === Event::SERVER_CLIENT_CLOSE) { - Event::on($name, $_event); - }else{ + if ($name !== Event::SERVER_CLIENT_CLOSE) { Event::on('listen ' . $config['port'] . ' ' . $name, $_event); + } else { + Event::on($name, $_event); } } } @@ -342,10 +342,8 @@ class Server extends HttpService exit($this->addError(sprintf('Listen %s::%d fail.', $config['host'], $config['port']))); } - if (isset($config['settings']) && is_array($config['settings'])) { - $newListener->set($config['settings']); - } - $this->onListenerBind($config); + $newListener->set($config['settings'] ?? []); + $this->onListenerBind($newListener, $config); return $this->swoole; } @@ -405,9 +403,9 @@ class Server extends HttpService * @return Packet|Websocket|Receive|Http|null * @throws Exception */ - private function onListenerBind($config): Packet|Websocket|Receive|Http|null + private function onListenerBind($server, $config): Packet|Websocket|Receive|Http|null { - $this->bindServerEvent($config['type']); + $this->bindServerEvent($server, $config['type']); $this->debug(sprintf('Check listen %s::%d -> ok', $config['host'], $config['port'])); @@ -419,18 +417,16 @@ class Server extends HttpService * @param string $type * @throws Exception */ - private function bindServerEvent($type = self::TCP) + private function bindServerEvent($server, $type = self::TCP) { - if (in_array($type, [self::PACKAGE, self::TCP])) { - $this->onBindCallback('connect', [make(OnConnect::class), 'onHandler']); - $this->onBindCallback('close', [make(OnClose::class), 'onHandler']); - if ($type == self::PACKAGE) { - $this->onBindCallback('packet', [make(OnPacket::class), 'onHandler']); - } else if ($type == self::TCP) { - $this->onBindCallback('receive', [make(OnReceive::class), 'onHandler']); - } + if (self::PACKAGE == $type) { + $this->onBindCallback($server, 'packet', [make(OnPacket::class), 'onHandler']); + } else if ($type == self::TCP) { + $this->onBindCallback($server, 'connect', [make(OnConnect::class), 'onHandler']); + $this->onBindCallback($server, 'close', [make(OnClose::class), 'onHandler']); + $this->onBindCallback($server, 'receive', [make(OnReceive::class), 'onHandler']); } else if ($type === self::HTTP) { - $this->onBindCallback('request', [make(OnRequest::class), 'onHandler']); + $this->onBindCallback($server, 'request', [make(OnRequest::class), 'onHandler']); } else { throw new Exception('Unknown server type(' . $type . ').'); } @@ -442,12 +438,12 @@ class Server extends HttpService * @param $callback * @throws Exception */ - public function onBindCallback($name, $callback) + public function onBindCallback($server, $name, $callback) { - if ($this->swoole->getCallback($name) !== null) { + if ($server->getCallback($name) !== null) { return; } - $this->swoole->on($name, $callback); + $server->on($name, $callback); } diff --git a/Rpc/Service.php b/Rpc/Service.php index 7ba2a89f..bacb711d 100644 --- a/Rpc/Service.php +++ b/Rpc/Service.php @@ -32,67 +32,69 @@ use Snowflake\Snowflake; class Service extends Component { + const defaultConfig = [ + 'open_tcp_keepalive' => true, + 'tcp_keepidle' => 30, + 'tcp_keepinterval' => 10, + 'tcp_keepcount' => 10, + 'open_http_protocol' => false, + 'open_websocket_protocol' => false, + ]; - /** - * @param Packet|Websocket|Receive|Http|null $server - * @throws ConfigException - * @throws Exception - */ - public function instance(Packet|Websocket|Receive|null|Http $server): void - { - $service = Config::get('rpc'); - if (empty($service) || !is_array($service)) { - return; - } - $mode = $service['mode'] ?? SWOOLE_SOCK_TCP6; + /** + * @param Packet|Websocket|Receive|Http|null $server + * @throws ConfigException + * @throws Exception + */ + public function instance(Packet|Websocket|Receive|null|Http $server): void + { + $service = Config::get('rpc'); + if (empty($service) || !is_array($service)) { + return; + } + $mode = $service['mode'] ?? SWOOLE_SOCK_TCP6; - if (Snowflake::port_already($service['port'])) { - throw new Exception($this->already($service)); - } - $this->debug(Snowflake::listen($service)); + if (Snowflake::port_already($service['port'])) { + throw new Exception($this->already($service)); + } + $this->debug(Snowflake::listen($service)); - $this->addCallback($mode); + if (!isset($service['setting'])) { + $service['setting'] = self::defaultConfig; + } - $rpcServer = $server->addlistener($service['host'], $service['port'], $mode); - $rpcServer->set($service['setting'] ?? [ - 'open_tcp_keepalive' => true, - 'tcp_keepidle' => 30, - 'tcp_keepinterval' => 10, - 'tcp_keepcount' => 10, - 'open_http_protocol' => false, - 'open_websocket_protocol' => false, - ]); - } + $rpcServer = $server->addlistener($service['host'], $service['port'], $mode); + $rpcServer->set($service['setting']); + $this->addCallback($rpcServer, $mode); + } - /** - * @param $service - * @return string - */ - #[Pure] private function already($service): string - { - return sprintf('Port %s::%d is already.', $service['host'], $service['port']); - } + /** + * @param $service + * @return string + */ + #[Pure] private function already($service): string + { + return sprintf('Port %s::%d is already.', $service['host'], $service['port']); + } - /** - * @param $mode - * @throws Exception - */ - private function addCallback($mode) - { - $tcp = [SWOOLE_SOCK_TCP, SWOOLE_TCP, SWOOLE_TCP6, SWOOLE_SOCK_TCP6]; - - $server = Snowflake::app()->getServer(); - $server->onBindCallback('connect', [make(OnConnect::class), 'onHandler']); - $server->onBindCallback('close', [make(OnClose::class), 'onHandler']); - - if (in_array($mode, $tcp)) { - $server->onBindCallback('receive', [make(OnReceive::class), 'onHandler']); - } else { - $server->onBindCallback('packet', [make(OnReceive::class), 'onHandler']); - } - } + /** + * @param $mode + * @throws Exception + */ + private function addCallback($rpcServer, $mode) + { + $tcp = [SWOOLE_SOCK_TCP, SWOOLE_TCP, SWOOLE_TCP6, SWOOLE_SOCK_TCP6]; + $server = Snowflake::app()->getServer(); + if (in_array($mode, $tcp)) { + $server->onBindCallback($rpcServer, 'connect', [make(OnConnect::class), 'onHandler']); + $server->onBindCallback($rpcServer, 'close', [make(OnClose::class), 'onHandler']); + $server->onBindCallback($rpcServer, 'receive', [make(OnReceive::class), 'onHandler']); + } else { + $server->onBindCallback($rpcServer, 'packet', [make(OnReceive::class), 'onHandler']); + } + } } diff --git a/System/Error/Logger.php b/System/Error/Logger.php index d55ac98a..6a2fc654 100644 --- a/System/Error/Logger.php +++ b/System/Error/Logger.php @@ -31,9 +31,14 @@ class Logger extends Component private array $logs = []; + private array $sources = []; + + public function init() { Event::on(Event::SYSTEM_RESOURCE_CLEAN, [$this, 'insert']); + Event::on(Event::SYSTEM_RESOURCE_CLEAN, [$this, 'closeSource']); + Event::on(Event::SYSTEM_RESOURCE_RELEASES, [$this, 'closeSource']); Event::on(Event::SYSTEM_RESOURCE_RELEASES, [$this, 'insert']); } @@ -72,12 +77,12 @@ class Logger extends Component $this->writer($message, $method); } - /** - * @param mixed $message - * @param string $method - * @param null $file - * @throws Exception - */ + /** + * @param mixed $message + * @param string $method + * @param null $file + * @throws Exception + */ public function success(mixed $message, string $method = 'app', $file = null) { $this->writer($message, $method); @@ -160,33 +165,53 @@ class Logger extends Component return end($filetype); } - /** - * @param string $messages - * @param string $method - * @throws Exception - */ + /** + * @param string $messages + * @param string $method + * @throws Exception + */ public function write(string $messages, string $method = 'app') { - return; - if (empty($messages)) { + if (empty($messages)) { + return; } - $fileName = 'server-' . date('Y-m-d') . '.log'; - $dirName = 'log/' . (empty($method) ? 'app' : $method); - $logFile = '[' . date('Y-m-d H:i:s') . ']:' . PHP_EOL . $messages . PHP_EOL; - Snowflake::writeFile(storage($fileName, $dirName), $logFile, FILE_APPEND); + $dirName = 'log/' . ($method ?? 'app'); - $files = glob(storage(null, $dirName) . '/*'); - if (count($files) >= 15) { - $command = 'find ' . storage(null, $dirName) . '/ -mtime +15 -name "*.log" -exec rm -rf {} \;'; - if (Coroutine::getCid() !== -1) { - Coroutine\System::exec($command); - } else { - \shell_exec($command); - } + $fileName = storage('server-' . date('Y-m-d') . '.log', $dirName); + if (!isset($this->sources[$fileName])) { + $this->sources[$fileName] = fopen(storage($fileName, $dirName), 'rw'); } + fwrite($this->sources[$fileName], '[' . date('Y-m-d H:i:s') . ']:' . PHP_EOL . $messages . PHP_EOL); + + $this->clearHistoryFile($dirName); } + + /** + * 清理文件资源 + */ + public function closeSource() + { + foreach ($this->sources as $source) { + fclose($source); + } + $this->sources = []; + } + + + /** + * @param string $dirName + * @throws \Exception + */ + private function clearHistoryFile(string $dirName) + { + $command = 'find ' . storage(null, $dirName) . '/ -mtime +15 -name "*.log" -exec rm -rf {} \;'; + + Coroutine::getCid() !== -1 ? Coroutine\System::exec($command) : \shell_exec($command); + } + + /** * @param $logFile * @return string