改名
This commit is contained in:
@@ -1,94 +0,0 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace HttpServer\Events;
|
||||
|
||||
|
||||
use Exception;
|
||||
use HttpServer\Application;
|
||||
use Snowflake\Error\Logger;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Snowflake;
|
||||
use Swoole\Timer;
|
||||
|
||||
abstract class Callback extends Application
|
||||
{
|
||||
|
||||
/** @var \Snowflake\Application */
|
||||
protected $container;
|
||||
|
||||
|
||||
/**
|
||||
* Callback constructor.
|
||||
* @param $container
|
||||
*/
|
||||
public function __construct($container)
|
||||
{
|
||||
$this->container = $container;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param $server
|
||||
* @param $worker_id
|
||||
* @param $message
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function clear($server, $worker_id, $message)
|
||||
{
|
||||
Timer::clearAll();
|
||||
$event = Snowflake::get()->event;
|
||||
|
||||
$event->offName(Event::EVENT_AFTER_REQUEST);
|
||||
$event->offName(Event::EVENT_BEFORE_REQUEST);
|
||||
$this->eventNotify($message, $event);
|
||||
|
||||
Snowflake::clearProcessId($server->worker_pid);
|
||||
Logger::write($this->_MESSAGE[$message] . $worker_id);
|
||||
Logger::clear();
|
||||
}
|
||||
|
||||
|
||||
|
||||
const EVENT_ERROR = 'WORKER:ERROR';
|
||||
const EVENT_STOP = 'WORKER:STOP';
|
||||
const EVENT_EXIT = 'WORKER:EXIT';
|
||||
|
||||
|
||||
private $_MESSAGE = [
|
||||
self::EVENT_ERROR => 'The server error. at No.',
|
||||
self::EVENT_STOP => 'The server stop. at No.',
|
||||
self::EVENT_EXIT => 'The server exit. at No.',
|
||||
];
|
||||
|
||||
/**
|
||||
* @param $message
|
||||
* @param $event
|
||||
*/
|
||||
private function eventNotify($message, $event)
|
||||
{
|
||||
switch ($message) {
|
||||
case self::EVENT_ERROR:
|
||||
if (!$event->exists(Event::SERVER_WORKER_ERROR)) {
|
||||
return;
|
||||
}
|
||||
$event->trigger(Event::SERVER_WORKER_ERROR);
|
||||
break;
|
||||
case self::EVENT_EXIT:
|
||||
if (!$event->exists(Event::SERVER_WORKER_EXIT)) {
|
||||
return;
|
||||
}
|
||||
$event->trigger(Event::SERVER_WORKER_EXIT);
|
||||
break;
|
||||
case self::EVENT_STOP:
|
||||
if (!$event->exists(Event::SERVER_WORKER_STOP)) {
|
||||
return;
|
||||
}
|
||||
$event->trigger(Event::SERVER_WORKER_STOP);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace HttpServer\Events\Trigger;
|
||||
namespace HttpServer\Events;
|
||||
|
||||
|
||||
use HttpServer\Events\Callback;
|
||||
use HttpServer\Events\Abstracts\Callback;
|
||||
|
||||
class OnAfterReload extends Callback
|
||||
{
|
||||
+2
-2
@@ -1,10 +1,10 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace HttpServer\Events\Trigger;
|
||||
namespace HttpServer\Events;
|
||||
|
||||
|
||||
use HttpServer\Events\Callback;
|
||||
use HttpServer\Events\Abstracts\Callback;
|
||||
|
||||
class OnBeforeReload extends Callback
|
||||
{
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace HttpServer\Events;
|
||||
|
||||
|
||||
use HttpServer\Events\Abstracts\Callback;
|
||||
use HttpServer\Route\Annotation\Annotation;
|
||||
use HttpServer\Route\Annotation\Tcp;
|
||||
use HttpServer\Route\Annotation\Websocket as AWebsocket;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Snowflake;
|
||||
use Swoole\Server;
|
||||
use Exception;
|
||||
use Swoole\Http\Server as HServer;
|
||||
use Swoole\WebSocket\Server as WServer;
|
||||
|
||||
/**
|
||||
* Class OnClose
|
||||
* @package HttpServer\Events
|
||||
*/
|
||||
class OnClose extends Callback
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $fd
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onHandler(Server $server, int $fd)
|
||||
{
|
||||
try {
|
||||
if ($server instanceof WServer) {
|
||||
if (!$server->isEstablished($fd)) {
|
||||
return;
|
||||
}
|
||||
$manager = Snowflake::get()->annotation->get('websocket');
|
||||
$name = $manager->getName(AWebsocket::CLOSE);
|
||||
} else if ($server instanceof HServer) {
|
||||
$manager = Snowflake::get()->annotation->get('http');
|
||||
$name = $manager->getName(Annotation::CLOSE);
|
||||
} else {
|
||||
$manager = Snowflake::get()->annotation->get('tcp');
|
||||
$name = $manager->getName(Tcp::CLOSE);
|
||||
}
|
||||
if (!$manager->has($name)) {
|
||||
return;
|
||||
}
|
||||
$manager->runWith($name, [$fd]);
|
||||
} catch (\Throwable $exception) {
|
||||
$this->addError($exception->getMessage());
|
||||
} finally {
|
||||
$event = Snowflake::get()->event;
|
||||
$event->trigger(Event::RELEASE_ALL);
|
||||
|
||||
$logger = Snowflake::get()->getLogger();
|
||||
$logger->insert();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,18 +1,18 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace HttpServer\Events\Trigger;
|
||||
namespace HttpServer\Events;
|
||||
|
||||
|
||||
use Exception;
|
||||
use HttpServer\Events\Callback;
|
||||
use HttpServer\Events\Abstracts\Callback;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Snowflake;
|
||||
use Swoole\Server;
|
||||
|
||||
/**
|
||||
* Class OnConnect
|
||||
* @package HttpServer\Events\Trigger
|
||||
* @package HttpServer\Events
|
||||
*/
|
||||
class OnConnect extends Callback
|
||||
{
|
||||
@@ -1,11 +1,11 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace HttpServer\Events\Trigger;
|
||||
namespace HttpServer\Events;
|
||||
|
||||
|
||||
use Exception;
|
||||
use HttpServer\Events\Callback;
|
||||
use HttpServer\Events\Abstracts\Callback;
|
||||
use Swoole\Server;
|
||||
|
||||
class OnFinish extends Callback
|
||||
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace HttpServer\Events;
|
||||
|
||||
|
||||
use Exception;
|
||||
use HttpServer\Events\Abstracts\Callback;
|
||||
use HttpServer\Route\Annotation\Websocket as AWebsocket;
|
||||
use Snowflake\Snowflake;
|
||||
use Swoole\Http\Request as SRequest;
|
||||
use Swoole\Http\Response as SResponse;
|
||||
use Swoole\WebSocket\Server;
|
||||
|
||||
|
||||
/**
|
||||
* Class OnHandshake
|
||||
* @package HttpServer\Events
|
||||
*/
|
||||
class OnHandshake extends Callback
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param SRequest $request
|
||||
* @param SResponse $response
|
||||
* @return bool|string
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onHandler(SRequest $request, SResponse $response)
|
||||
{
|
||||
/** @var Server $server */
|
||||
$secWebSocketKey = $request->header['sec-websocket-key'];
|
||||
$patten = '#^[+/0-9A-Za-z]{21}[AQgw]==$#';
|
||||
if (0 === preg_match($patten, $secWebSocketKey) || 16 !== strlen(base64_decode($secWebSocketKey))) {
|
||||
return false;
|
||||
}
|
||||
$key = base64_encode(sha1(
|
||||
$request->header['sec-websocket-key'] . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11',
|
||||
TRUE
|
||||
));
|
||||
$headers = [
|
||||
'Upgrade' => 'websocket',
|
||||
'Connection' => 'Upgrade',
|
||||
'Sec-websocket-Accept' => $key,
|
||||
'Sec-websocket-Version' => '13',
|
||||
];
|
||||
if (isset($request->header['sec-websocket-protocol'])) {
|
||||
$headers['Sec-websocket-Protocol'] = $request->header['sec-websocket-protocol'];
|
||||
}
|
||||
foreach ($headers as $key => $val) {
|
||||
$response->header($key, $val);
|
||||
}
|
||||
|
||||
/** @var AWebsocket $manager */
|
||||
$manager = Snowflake::get()->annotation->get('websocket');
|
||||
if ($manager->has($manager->getName(AWebsocket::HANDSHAKE))) {
|
||||
$manager->runWith($manager->getName(AWebsocket::HANDSHAKE), [$request, $response]);
|
||||
} else {
|
||||
$response->status(502);
|
||||
$response->end();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+2
-2
@@ -1,10 +1,10 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace HttpServer\Events\Trigger;
|
||||
namespace HttpServer\Events;
|
||||
|
||||
|
||||
use HttpServer\Events\Callback;
|
||||
use HttpServer\Events\Abstracts\Callback;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Snowflake;
|
||||
use Swoole\Server;
|
||||
@@ -1,17 +1,17 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace HttpServer\Events\Trigger;
|
||||
namespace HttpServer\Events;
|
||||
|
||||
|
||||
use HttpServer\Events\Callback;
|
||||
use HttpServer\Events\Abstracts\Callback;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Snowflake;
|
||||
use Swoole\Server;
|
||||
|
||||
/**
|
||||
* Class OnManagerStop
|
||||
* @package HttpServer\Events\Trigger
|
||||
* @package HttpServer\Events
|
||||
*/
|
||||
class OnManagerStop extends Callback
|
||||
{
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace HttpServer\Events;
|
||||
|
||||
|
||||
use HttpServer\Events\Abstracts\Callback;
|
||||
use HttpServer\Route\Annotation\Websocket as AWebsocket;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Snowflake;
|
||||
use Swoole\WebSocket\Frame;
|
||||
use Swoole\WebSocket\Server;
|
||||
|
||||
/**
|
||||
* Class OnMessage
|
||||
* @package HttpServer\Events
|
||||
*/
|
||||
class OnMessage extends Callback
|
||||
{
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param Frame $frame
|
||||
* @throws
|
||||
*/
|
||||
public function onHandler(Server $server, Frame $frame)
|
||||
{
|
||||
try {
|
||||
if ($frame->opcode == 0x08) {
|
||||
return;
|
||||
}
|
||||
|
||||
$event = Snowflake::get()->event;
|
||||
if ($event->exists(Event::SERVER_MESSAGE)) {
|
||||
$event->trigger(Event::SERVER_MESSAGE, [$server, $frame]);
|
||||
} else {
|
||||
$frame->data = json_decode($frame->data, true);
|
||||
}
|
||||
|
||||
/** @var AWebsocket $manager */
|
||||
$manager = Snowflake::get()->annotation->get('websocket');
|
||||
$manager->runWith($manager->getName(AWebsocket::MESSAGE, [null, null, $frame->data['route']]), [$frame, $server]);
|
||||
} catch (\Exception $exception) {
|
||||
$this->addError($exception->getMessage(), 'websocket');
|
||||
$server->send($frame->fd, $exception->getMessage());
|
||||
} finally {
|
||||
$event = Snowflake::get()->event;
|
||||
$event->trigger(Event::EVENT_AFTER_REQUEST);
|
||||
Snowflake::get()->logger->insert();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -4,18 +4,60 @@
|
||||
namespace HttpServer\Events;
|
||||
|
||||
|
||||
use Exception;
|
||||
use Closure;
|
||||
use HttpServer\Events\Abstracts\Callback;
|
||||
use Snowflake\Core\JSON;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Snowflake;
|
||||
use Swoole\Server;
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Class OnPacket
|
||||
* @package HttpServer\Events\Trigger
|
||||
* @package HttpServer\Events
|
||||
*/
|
||||
class Packet extends Service
|
||||
class OnPacket extends Callback
|
||||
{
|
||||
|
||||
|
||||
/** @var Closure|array */
|
||||
public $unpack;
|
||||
|
||||
|
||||
/** @var Closure|array */
|
||||
public $pack;
|
||||
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function pack($data)
|
||||
{
|
||||
$callback = $this->pack;
|
||||
if (is_callable($callback, true)) {
|
||||
return $callback($data);
|
||||
}
|
||||
return JSON::encode($data);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @return mixed
|
||||
*/
|
||||
public function unpack($data)
|
||||
{
|
||||
$callback = $this->unpack;
|
||||
if (is_callable($callback, true)) {
|
||||
return $callback($data);
|
||||
}
|
||||
return JSON::decode($data);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param $data
|
||||
@@ -41,4 +83,5 @@ class Packet extends Service
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace HttpServer\Events\Trigger;
|
||||
namespace HttpServer\Events;
|
||||
|
||||
|
||||
use HttpServer\Events\Callback;
|
||||
use HttpServer\Events\Abstracts\Callback;
|
||||
|
||||
class OnPipeMessage extends Callback
|
||||
{
|
||||
@@ -4,19 +4,59 @@
|
||||
namespace HttpServer\Events;
|
||||
|
||||
|
||||
use Exception;
|
||||
use HttpServer\Events\Abstracts\Callback;
|
||||
use Snowflake\Core\JSON;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Snowflake;
|
||||
use Swoole\Server;
|
||||
use Exception;
|
||||
use Closure;
|
||||
|
||||
/**
|
||||
* Class Receive
|
||||
* Class OnReceive
|
||||
* @package HttpServer\Events
|
||||
*/
|
||||
class Receive extends Service
|
||||
class OnReceive extends Callback
|
||||
{
|
||||
|
||||
|
||||
/** @var Closure|array */
|
||||
public $unpack;
|
||||
|
||||
|
||||
/** @var Closure|array */
|
||||
public $pack;
|
||||
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function pack($data)
|
||||
{
|
||||
$callback = $this->pack;
|
||||
if (is_callable($callback, true)) {
|
||||
return $callback($data);
|
||||
}
|
||||
return JSON::encode($data);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @return mixed
|
||||
*/
|
||||
public function unpack($data)
|
||||
{
|
||||
$callback = $this->unpack;
|
||||
if (is_callable($callback, true)) {
|
||||
return $callback($data);
|
||||
}
|
||||
return JSON::decode($data);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $fd
|
||||
@@ -25,7 +65,7 @@ class Receive extends Service
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onReceive(\Swoole\Server $server, int $fd, int $reactorId, string $data)
|
||||
public function onHandler(\Swoole\Server $server, int $fd, int $reactorId, string $data)
|
||||
{
|
||||
try {
|
||||
$client = [$fd];
|
||||
@@ -4,66 +4,31 @@
|
||||
namespace HttpServer\Events;
|
||||
|
||||
|
||||
use Exception;
|
||||
use HttpServer\Events\Abstracts\Callback;
|
||||
use HttpServer\Http\Context;
|
||||
use HttpServer\Http\Request as HRequest;
|
||||
use HttpServer\Http\Response as HResponse;
|
||||
use HttpServer\ServerManager;
|
||||
use ReflectionException;
|
||||
use Snowflake\Application;
|
||||
use HttpServer\Service\Http;
|
||||
use Snowflake\Core\JSON;
|
||||
use Snowflake\Exception\NotFindClassException;
|
||||
use Snowflake\Snowflake;
|
||||
use Swoole\Error;
|
||||
use Swoole\Http\Request;
|
||||
use Swoole\Http\Response;
|
||||
use Exception;
|
||||
use Swoole\Http\Server;
|
||||
use Swoole\Process\Pool;
|
||||
|
||||
class Http extends Server
|
||||
/**
|
||||
* Class OnRequest
|
||||
* @package HttpServer\Events
|
||||
*/
|
||||
class OnRequest extends Callback
|
||||
{
|
||||
|
||||
/** @var Application */
|
||||
protected $application;
|
||||
|
||||
|
||||
/**
|
||||
* Receive constructor.
|
||||
* @param $application
|
||||
* @param $host
|
||||
* @param null $port
|
||||
* @param null $mode
|
||||
* @param null $sock_type
|
||||
*/
|
||||
public function __construct($application, $host, $port = null, $mode = null, $sock_type = null)
|
||||
{
|
||||
$application->set(Http::class, $this);
|
||||
$this->application = $application;
|
||||
parent::__construct($host, $port, $mode, $sock_type);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $settings
|
||||
* @param null $pool
|
||||
* @param array $events
|
||||
* @param array $config
|
||||
* @return mixed|void
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function set(array $settings, $pool = null, $events = [], $config = [])
|
||||
{
|
||||
parent::set($settings);
|
||||
ServerManager::set($this, $settings, $this->application, $events, $config);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param Response $response
|
||||
* @throws \Exception
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onHandler(Request $request, Response $response)
|
||||
{
|
||||
@@ -79,7 +44,7 @@ class Http extends Server
|
||||
}
|
||||
} finally {
|
||||
$dividing_line = str_pad('', 100, '-');
|
||||
$this->application->debug($dividing_line, 'app');
|
||||
$this->debug($dividing_line, 'app');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,7 +61,7 @@ class Http extends Server
|
||||
'file' => $exception->getFile(),
|
||||
'line' => $exception->getLine()
|
||||
];
|
||||
$this->application->error(var_export($errorInfo, true));
|
||||
$this->error(var_export($errorInfo, true));
|
||||
|
||||
$code = $exception->getCode() ?? 500;
|
||||
$trance = array_slice($exception->getTrace(), 0, 10);
|
||||
@@ -119,4 +84,5 @@ class Http extends Server
|
||||
return [$request, $response];
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace HttpServer\Events;
|
||||
|
||||
|
||||
use Exception;
|
||||
use HttpServer\Events\Abstracts\Callback;
|
||||
use PHPMailer\PHPMailer\PHPMailer;
|
||||
use PHPMailer\PHPMailer\SMTP;
|
||||
use Snowflake\Config;
|
||||
use Snowflake\Core\JSON;
|
||||
use Snowflake\Error\Logger;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Exception\ConfigException;
|
||||
use Snowflake\Snowflake;
|
||||
use Swoole\Server;
|
||||
use Closure;
|
||||
|
||||
/**
|
||||
* Class OnShutdown
|
||||
* @package HttpServer\Events
|
||||
*/
|
||||
class OnShutdown extends Callback
|
||||
{
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function onHandler(Server $server)
|
||||
{
|
||||
$email = Config::get('email');
|
||||
$nickname = Config::get('nickname');
|
||||
|
||||
$this->system_mail($email, $nickname);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $email
|
||||
* @param $nickname
|
||||
*/
|
||||
protected function system_mail($email, $nickname)
|
||||
{
|
||||
try {
|
||||
$mail = new PHPMailer(true);
|
||||
//Server settings
|
||||
$mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output
|
||||
$mail->isSMTP(); // Send using SMTP
|
||||
$mail->Host = 'smtp1.example.com'; // Set the SMTP server to send through
|
||||
$mail->SMTPAuth = true; // Enable SMTP authentication
|
||||
$mail->Username = 'user@example.com'; // SMTP username
|
||||
$mail->Password = 'secret'; // SMTP password
|
||||
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
|
||||
$mail->Port = 587; // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
|
||||
|
||||
//Recipients
|
||||
$mail->setFrom('system@example.com', '系统管理员');
|
||||
$mail->addAddress($email, $nickname); // Add a recipient
|
||||
|
||||
// Attachments
|
||||
// $mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
|
||||
// $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
|
||||
|
||||
// Content
|
||||
$mail->isHTML(true); // Set email format to HTML
|
||||
$mail->Subject = 'Here is the subject';
|
||||
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
|
||||
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
|
||||
|
||||
$mail->send();
|
||||
echo 'Message has been sent';
|
||||
} catch (Exception $e) {
|
||||
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace HttpServer\Events\Trigger;
|
||||
namespace HttpServer\Events;
|
||||
|
||||
|
||||
use HttpServer\Events\Callback;
|
||||
use HttpServer\Events\Abstracts\Callback;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Snowflake;
|
||||
use Swoole\Server;
|
||||
@@ -1,10 +1,10 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace HttpServer\Events\Trigger\Trigger;
|
||||
namespace HttpServer\Events;
|
||||
|
||||
|
||||
use HttpServer\Events\Callback;
|
||||
use HttpServer\Events\Abstracts\Callback;
|
||||
use HttpServer\IInterface\Task as ITask;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Snowflake;
|
||||
@@ -106,8 +106,6 @@ class OnTask extends Callback
|
||||
} finally {
|
||||
$event = Snowflake::get()->event;
|
||||
$event->trigger(Event::RELEASE_ALL);
|
||||
|
||||
$this->endCoroutine();
|
||||
Timer::clearAll();
|
||||
}
|
||||
return $finish;
|
||||
@@ -1,10 +1,10 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace HttpServer\Events\Trigger;
|
||||
namespace HttpServer\Events;
|
||||
|
||||
|
||||
use HttpServer\Events\Callback;
|
||||
use HttpServer\Events\Abstracts\Callback;
|
||||
|
||||
class OnWorkerError extends Callback
|
||||
{
|
||||
@@ -1,10 +1,10 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace HttpServer\Events\Trigger;
|
||||
namespace HttpServer\Events;
|
||||
|
||||
|
||||
use HttpServer\Events\Callback;
|
||||
use HttpServer\Events\Abstracts\Callback;
|
||||
|
||||
class OnWorkerExit extends Callback
|
||||
{
|
||||
@@ -1,11 +1,11 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace HttpServer\Events\Trigger;
|
||||
namespace HttpServer\Events;
|
||||
|
||||
|
||||
use Exception;
|
||||
use HttpServer\Events\Callback;
|
||||
use HttpServer\Events\Abstracts\Callback;
|
||||
use HttpServer\Events\Http;
|
||||
use HttpServer\Events\WebSocket;
|
||||
use HttpServer\Route\Annotation\Websocket as AWebsocket;
|
||||
@@ -16,7 +16,7 @@ use Swoole\Server;
|
||||
|
||||
/**
|
||||
* Class OnWorkerStart
|
||||
* @package HttpServer\Events\Trigger
|
||||
* @package HttpServer\Events
|
||||
*/
|
||||
class OnWorkerStart extends Callback
|
||||
{
|
||||
@@ -31,8 +31,6 @@ class OnWorkerStart extends Callback
|
||||
*/
|
||||
public function onHandler(Server $server, $worker_id)
|
||||
{
|
||||
Logger::$worker_id = $worker_id;
|
||||
|
||||
Snowflake::setProcessId($server->worker_pid);
|
||||
|
||||
$get_name = $this->get_process_name($server, $worker_id);
|
||||
@@ -69,7 +67,7 @@ class OnWorkerStart extends Callback
|
||||
}
|
||||
$event->trigger(Event::SERVER_WORKER_START);
|
||||
} catch (\Throwable $exception) {
|
||||
Logger::write($exception->getMessage(), 'worker');
|
||||
Snowflake::get()->getLogger()->write($exception->getMessage(), 'worker');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace HttpServer\Events\Trigger;
|
||||
namespace HttpServer\Events;
|
||||
|
||||
|
||||
use HttpServer\Events\Callback;
|
||||
use HttpServer\Events\Abstracts\Callback;
|
||||
|
||||
class OnWorkerStop extends Callback
|
||||
{
|
||||
@@ -1,129 +0,0 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace HttpServer\Events;
|
||||
|
||||
|
||||
use Exception;
|
||||
use HttpServer\ServerManager;
|
||||
use ReflectionException;
|
||||
use Snowflake\Application;
|
||||
use Snowflake\Core\JSON;
|
||||
use Snowflake\Exception\NotFindClassException;
|
||||
use Snowflake\Snowflake;
|
||||
use Swoole\Server;
|
||||
use Closure;
|
||||
|
||||
/**
|
||||
* Class Service
|
||||
* @package HttpServer\Events
|
||||
*/
|
||||
abstract class Service extends Server
|
||||
{
|
||||
|
||||
/** @var Application */
|
||||
protected $application;
|
||||
|
||||
|
||||
/** @var Closure|array */
|
||||
public $unpack;
|
||||
|
||||
|
||||
/** @var Closure|array */
|
||||
public $pack;
|
||||
|
||||
|
||||
/**
|
||||
* Receive constructor.
|
||||
* @param $application
|
||||
* @param $host
|
||||
* @param null $port
|
||||
* @param null $mode
|
||||
* @param null $sock_type
|
||||
*/
|
||||
public function __construct($application, $host, $port = null, $mode = null, $sock_type = null)
|
||||
{
|
||||
$application->set($host . ':' . $port, $this);
|
||||
$this->application = $application;
|
||||
parent::__construct($host, $port, $mode, $sock_type);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $settings
|
||||
* @param null $pool
|
||||
* @param array $events
|
||||
* @param array $config
|
||||
* @return mixed|void
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function set(array $settings, $pool = null, $events = [], $config = [])
|
||||
{
|
||||
parent::set($settings);
|
||||
ServerManager::set($this, $settings, $this->application, $events, $config);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $callbacks
|
||||
*/
|
||||
protected function bindCallback($callbacks)
|
||||
{
|
||||
if (empty($callbacks) || !is_array($callbacks)) {
|
||||
return;
|
||||
}
|
||||
foreach ($callbacks as $callback) {
|
||||
$this->on($callback[0], [$this, $callback[1][1]]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $eventName
|
||||
* @return array
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function createHandler($eventName)
|
||||
{
|
||||
$classPrefix = 'HttpServer\Events\Trigger\On' . ucfirst($eventName);
|
||||
if (!class_exists($classPrefix)) {
|
||||
throw new Exception('class not found.');
|
||||
}
|
||||
$class = Snowflake::createObject($classPrefix, [Snowflake::get()]);
|
||||
return [$class, 'onHandler'];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function pack($data)
|
||||
{
|
||||
$callback = $this->pack;
|
||||
if (is_callable($callback, true)) {
|
||||
return $callback($data);
|
||||
}
|
||||
return JSON::encode($data);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @return mixed
|
||||
*/
|
||||
public function unpack($data)
|
||||
{
|
||||
$callback = $this->unpack;
|
||||
if (is_callable($callback, true)) {
|
||||
return $callback($data);
|
||||
}
|
||||
return JSON::decode($data);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace HttpServer\Events\Trigger;
|
||||
|
||||
|
||||
use HttpServer\Events\Callback;
|
||||
|
||||
class OnClose extends Callback
|
||||
{
|
||||
|
||||
public function onHandler()
|
||||
{
|
||||
// TODO: Implement onHandler() method.
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace HttpServer\Events\Trigger;
|
||||
|
||||
|
||||
use Exception;
|
||||
use HttpServer\Events\Callback;
|
||||
use Snowflake\Core\JSON;
|
||||
use Snowflake\Error\Logger;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Snowflake;
|
||||
use Swoole\Server;
|
||||
use Closure;
|
||||
|
||||
class OnShutdown extends Callback
|
||||
{
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
*/
|
||||
public function onHandler(Server $server)
|
||||
{
|
||||
var_dump($server);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,174 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/11/8 0008
|
||||
* Time: 18:15
|
||||
*/
|
||||
|
||||
namespace HttpServer\Events;
|
||||
|
||||
use Exception;
|
||||
use HttpServer\ServerManager;
|
||||
use ReflectionException;
|
||||
use Snowflake\Application;
|
||||
use Snowflake\Error\Logger;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Exception\NotFindClassException;
|
||||
use Snowflake\Snowflake;
|
||||
use Swoole\Http\Request as SRequest;
|
||||
use Swoole\Http\Response as SResponse;
|
||||
use Swoole\Process\Pool;
|
||||
use Swoole\WebSocket\Frame;
|
||||
use Swoole\WebSocket\Server;
|
||||
use HttpServer\Route\Annotation\Websocket as AWebsocket;
|
||||
|
||||
/**
|
||||
* Class ServerWebSocket
|
||||
* @package Snowflake\Snowflake\Server
|
||||
*/
|
||||
class WebSocket extends Server
|
||||
{
|
||||
public $namespace = 'App\\Sockets\\';
|
||||
|
||||
public $callback = [];
|
||||
|
||||
|
||||
/** @var Application */
|
||||
public $application;
|
||||
|
||||
|
||||
/**
|
||||
* WebSocket constructor.
|
||||
* @param $application
|
||||
* @param $host
|
||||
* @param null $port
|
||||
* @param null $mode
|
||||
* @param null $sock_type
|
||||
*/
|
||||
public function __construct($application, $host, $port = null, $mode = null, $sock_type = null)
|
||||
{
|
||||
$application->set(WebSocket::class, $this);
|
||||
$this->application = $application;
|
||||
parent::__construct($host, $port, $mode, $sock_type);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $settings
|
||||
* @param null $pool
|
||||
* @param array $events
|
||||
* @param array $config
|
||||
* @return mixed|void
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function set(array $settings, $pool = null, $events = [], $config = [])
|
||||
{
|
||||
parent::set($settings);
|
||||
|
||||
ServerManager::set($this, $settings, $this->application, $events, $config);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param Frame $frame
|
||||
* @throws
|
||||
*/
|
||||
public function onMessage(Server $server, Frame $frame)
|
||||
{
|
||||
try {
|
||||
if ($frame->opcode == 0x08) {
|
||||
return;
|
||||
}
|
||||
|
||||
$event = Snowflake::get()->event;
|
||||
if ($event->exists(Event::SERVER_MESSAGE)) {
|
||||
$event->trigger(Event::SERVER_MESSAGE, [$server, $frame]);
|
||||
} else {
|
||||
$frame->data = json_decode($frame->data, true);
|
||||
}
|
||||
|
||||
/** @var AWebsocket $manager */
|
||||
$manager = Snowflake::get()->annotation->get('websocket');
|
||||
$manager->runWith($manager->getName(AWebsocket::MESSAGE, [null, null, $frame->data['route']]), [$frame, $server]);
|
||||
} catch (Exception $exception) {
|
||||
$this->application->addError($exception->getMessage(), 'websocket');
|
||||
$server->send($frame->fd, $exception->getMessage());
|
||||
} finally {
|
||||
$event = Snowflake::get()->event;
|
||||
$event->trigger(Event::EVENT_AFTER_REQUEST);
|
||||
Logger::insert();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param SRequest $request
|
||||
* @param SResponse $response
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function connect($request, $response)
|
||||
{
|
||||
/** @var AWebsocket $manager */
|
||||
$manager = Snowflake::get()->annotation->get('websocket');
|
||||
$manager->runWith($manager->getName(AWebsocket::HANDSHAKE), [$request, $response]);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param SRequest $request
|
||||
* @param SResponse $response
|
||||
* @return bool|string
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onHandshake(SRequest $request, SResponse $response)
|
||||
{
|
||||
/** @var Server $server */
|
||||
$secWebSocketKey = $request->header['sec-websocket-key'];
|
||||
$patten = '#^[+/0-9A-Za-z]{21}[AQgw]==$#';
|
||||
if (0 === preg_match($patten, $secWebSocketKey) || 16 !== strlen(base64_decode($secWebSocketKey))) {
|
||||
return false;
|
||||
}
|
||||
$key = base64_encode(sha1(
|
||||
$request->header['sec-websocket-key'] . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11',
|
||||
TRUE
|
||||
));
|
||||
$headers = [
|
||||
'Upgrade' => 'websocket',
|
||||
'Connection' => 'Upgrade',
|
||||
'Sec-websocket-Accept' => $key,
|
||||
'Sec-websocket-Version' => '13',
|
||||
];
|
||||
if (isset($request->header['sec-websocket-protocol'])) {
|
||||
$headers['Sec-websocket-Protocol'] = $request->header['sec-websocket-protocol'];
|
||||
}
|
||||
foreach ($headers as $key => $val) {
|
||||
$response->header($key, $val);
|
||||
}
|
||||
return $this->connect($request, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $fd
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onClose(Server $server, int $fd)
|
||||
{
|
||||
try {
|
||||
/** @var AWebsocket $manager */
|
||||
$manager = Snowflake::get()->annotation->get('websocket');
|
||||
$manager->runWith($manager->getName(AWebsocket::CLOSE), [$fd]);
|
||||
} catch (\Throwable $exception) {
|
||||
$this->application->addError($exception->getMessage());
|
||||
} finally {
|
||||
$event = Snowflake::get()->event;
|
||||
$event->trigger(Event::RELEASE_ALL);
|
||||
Logger::insert();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user