mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-22 16:15:34 +08:00
add context() fetch data mode
enable coroutine override
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
namespace Framework;
|
namespace Framework;
|
||||||
|
|
||||||
|
use Swoole\Runtime;
|
||||||
use ZM\Event\EventHandler;
|
use ZM\Event\EventHandler;
|
||||||
use Exception;
|
use Exception;
|
||||||
use Swoole\WebSocket\Server;
|
use Swoole\WebSocket\Server;
|
||||||
@@ -38,6 +39,7 @@ class FrameworkLoader
|
|||||||
|
|
||||||
chdir(__DIR__ . '/../..');
|
chdir(__DIR__ . '/../..');
|
||||||
define('WORKING_DIR', getcwd());
|
define('WORKING_DIR', getcwd());
|
||||||
|
Runtime::enableCoroutine();
|
||||||
$this->requireGlobalFunctions();
|
$this->requireGlobalFunctions();
|
||||||
$this->registerAutoloader('classLoader');
|
$this->registerAutoloader('classLoader');
|
||||||
self::$settings = new GlobalConfig();
|
self::$settings = new GlobalConfig();
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
use Framework\Console;
|
||||||
use Framework\ZMBuf;
|
use Framework\ZMBuf;
|
||||||
use ZM\Utils\Context;
|
use ZM\Utils\Context;
|
||||||
|
|
||||||
@@ -11,7 +12,7 @@ function classLoader($p) {
|
|||||||
try {
|
try {
|
||||||
require_once $filepath;
|
require_once $filepath;
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
echo "Error when finding class: ".$p.PHP_EOL;
|
echo "Error when finding class: " . $p . PHP_EOL;
|
||||||
die;
|
die;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -148,18 +149,29 @@ function matchArgs($pattern, $context) {
|
|||||||
} else return false;
|
} else return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
function set_coroutine_params($array){
|
function set_coroutine_params($array) {
|
||||||
$cid = Co::getCid();
|
$cid = Co::getCid();
|
||||||
if($cid == -1) die("Cannot set coroutine params at none coroutine mode.");
|
if ($cid == -1) die("Cannot set coroutine params at none coroutine mode.");
|
||||||
ZMBuf::$context[$cid] = $array;
|
ZMBuf::$context[$cid] = $array;
|
||||||
foreach(ZMBuf::$context as $c => $v) {
|
foreach (ZMBuf::$context as $c => $v) {
|
||||||
if(!Co::exists($c)) unset(ZMBuf::$context[$c]);
|
if (!Co::exists($c)) unset(ZMBuf::$context[$c]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function context(){
|
function context() {
|
||||||
$cid = Co::getCid();
|
$cid = Co::getCid();
|
||||||
if(isset(ZMBuf::$context[$cid])) {
|
if (isset(ZMBuf::$context[$cid])) {
|
||||||
return new Context(ZMBuf::$context[$cid], $cid);
|
return new Context(ZMBuf::$context[$cid], $cid);
|
||||||
} else return null;
|
} else {
|
||||||
|
while (($pcid = Co::getPcid($cid)) !== -1) {
|
||||||
|
if (isset(ZMBuf::$context[$cid])) return new Context(ZMBuf::$context[$cid], $cid);
|
||||||
|
else $cid = $pcid;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function debug($msg) {
|
||||||
|
if (ZMBuf::$atomics["info_level"]->get() == 1)
|
||||||
|
Console::log(date("[H:i:s ") . "DEBUG] " . $msg, 'gray');
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,6 +43,9 @@ class EventHandler
|
|||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
/** @var Response $param1 */
|
/** @var Response $param1 */
|
||||||
$param1->status(500);
|
$param1->status(500);
|
||||||
|
Console::info($param0->server["remote_addr"].":".$param0->server["remote_port"].
|
||||||
|
" [".$param1->getStatusCode()."] ".$param0->server["request_uri"]
|
||||||
|
);
|
||||||
if (!$param1->isEnd()) $param1->end("Internal server error: " . $e->getMessage());
|
if (!$param1->isEnd()) $param1->end("Internal server error: " . $e->getMessage());
|
||||||
Console::error("Internal server error (500), caused by uncaught exception.");
|
Console::error("Internal server error (500), caused by uncaught exception.");
|
||||||
Console::log($e->getTraceAsString(), "gray");
|
Console::log($e->getTraceAsString(), "gray");
|
||||||
|
|||||||
@@ -39,17 +39,19 @@ class MessageEvent implements SwooleEvent
|
|||||||
*/
|
*/
|
||||||
public function onActivate() {
|
public function onActivate() {
|
||||||
ZMUtil::checkWait();
|
ZMUtil::checkWait();
|
||||||
|
$conn = ConnectionManager::get($this->frame->fd);
|
||||||
|
set_coroutine_params(["server" => $this->server, "frame" => $this->frame, "connection" => $conn]);
|
||||||
try {
|
try {
|
||||||
if (ConnectionManager::get($this->frame->fd)->getType() == "qq") {
|
if ($conn->getType() == "qq") {
|
||||||
$data = json_decode($this->frame->data, true);
|
$data = json_decode($this->frame->data, true);
|
||||||
if (isset($data["post_type"])) {
|
if (isset($data["post_type"])) {
|
||||||
|
set_coroutine_params(["data" => $data, "connection" => $conn]);
|
||||||
EventHandler::callCQEvent($data, ConnectionManager::get($this->frame->fd), 0);
|
EventHandler::callCQEvent($data, ConnectionManager::get($this->frame->fd), 0);
|
||||||
} else
|
} else
|
||||||
EventHandler::callCQResponse($data);
|
EventHandler::callCQResponse($data);
|
||||||
}
|
}
|
||||||
foreach (ZMBuf::$events[SwooleEventAt::class] ?? [] as $v) {
|
foreach (ZMBuf::$events[SwooleEventAt::class] ?? [] as $v) {
|
||||||
if (strtolower($v->type) == "message" && $this->parseSwooleRule($v)) {
|
if (strtolower($v->type) == "message" && $this->parseSwooleRule($v)) {
|
||||||
$conn = ConnectionManager::get($this->frame->fd);
|
|
||||||
$c = $v->class;
|
$c = $v->class;
|
||||||
/** @var ModBase $class */
|
/** @var ModBase $class */
|
||||||
$class = new $c(["server" => $this->server, "frame" => $this->frame, "connection" => $conn], ModHandleType::SWOOLE_MESSAGE);
|
$class = new $c(["server" => $this->server, "frame" => $this->frame, "connection" => $conn], ModHandleType::SWOOLE_MESSAGE);
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ class WSCloseEvent implements SwooleEvent
|
|||||||
public function onActivate() {
|
public function onActivate() {
|
||||||
ZMUtil::checkWait();
|
ZMUtil::checkWait();
|
||||||
ConnectionManager::close($this->fd);
|
ConnectionManager::close($this->fd);
|
||||||
|
set_coroutine_params(["server" => $this->server, "fd" => $this->fd]);
|
||||||
foreach(ZMBuf::$events[SwooleEventAt::class] ?? [] as $v) {
|
foreach(ZMBuf::$events[SwooleEventAt::class] ?? [] as $v) {
|
||||||
if(strtolower($v->type) == "close" && $this->parseSwooleRule($v)) {
|
if(strtolower($v->type) == "close" && $this->parseSwooleRule($v)) {
|
||||||
$c = $v->class;
|
$c = $v->class;
|
||||||
|
|||||||
@@ -55,6 +55,7 @@ class WSOpenEvent implements SwooleEvent
|
|||||||
$this->conn = new $type_conn($this->server, $this->request->fd);
|
$this->conn = new $type_conn($this->server, $this->request->fd);
|
||||||
}
|
}
|
||||||
ZMBuf::$connect[$this->request->fd] = $this->conn;
|
ZMBuf::$connect[$this->request->fd] = $this->conn;
|
||||||
|
set_coroutine_params(["server" => $this->server, "request" => $this->request, "connection" => $this->conn]);
|
||||||
foreach (ZMBuf::$events[SwooleEventAt::class] ?? [] as $v) {
|
foreach (ZMBuf::$events[SwooleEventAt::class] ?? [] as $v) {
|
||||||
if (strtolower($v->type) == "open" && $this->parseSwooleRule($v) === true) {
|
if (strtolower($v->type) == "open" && $this->parseSwooleRule($v) === true) {
|
||||||
$c = $v->class;
|
$c = $v->class;
|
||||||
|
|||||||
@@ -94,6 +94,7 @@ class WorkerStartEvent implements SwooleEvent
|
|||||||
$class = new $class_name(["server" => $this->server, "worker_id" => $this->worker_id], ModHandleType::SWOOLE_WORKER_START);
|
$class = new $class_name(["server" => $this->server, "worker_id" => $this->worker_id], ModHandleType::SWOOLE_WORKER_START);
|
||||||
call_user_func_array([$class, $v->method], []);
|
call_user_func_array([$class, $v->method], []);
|
||||||
}
|
}
|
||||||
|
set_coroutine_params(["server" => $this->server, "worker_id" => $this->worker_id]);
|
||||||
foreach (ZMBuf::$events[SwooleEventAfter::class] ?? [] as $v) {
|
foreach (ZMBuf::$events[SwooleEventAfter::class] ?? [] as $v) {
|
||||||
/** @var AnnotationBase $v */
|
/** @var AnnotationBase $v */
|
||||||
if (strtolower($v->type) == "workerstart") {
|
if (strtolower($v->type) == "workerstart") {
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ class Response
|
|||||||
*/
|
*/
|
||||||
private $response;
|
private $response;
|
||||||
private $is_end = false;
|
private $is_end = false;
|
||||||
|
private $status_code;
|
||||||
|
|
||||||
public function __construct(\Swoole\Http\Response $response) {
|
public function __construct(\Swoole\Http\Response $response) {
|
||||||
$this->response = $response;
|
$this->response = $response;
|
||||||
@@ -90,9 +91,14 @@ class Response
|
|||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function status($http_code, $reason = null) {
|
public function status($http_code, $reason = null) {
|
||||||
|
$this->status_code = $http_code;
|
||||||
return $this->response->status($http_code, $reason);
|
return $this->response->status($http_code, $reason);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getStatusCode() {
|
||||||
|
return $this->status_code ?? 200;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $http_code
|
* @param $http_code
|
||||||
* @param $reason
|
* @param $reason
|
||||||
|
|||||||
Reference in New Issue
Block a user