add context() fetch data mode

enable coroutine override
This commit is contained in:
whale
2020-04-14 23:46:42 +08:00
parent c1f720a0b3
commit b560246efb
8 changed files with 43 additions and 15 deletions

View File

@@ -3,6 +3,7 @@
namespace Framework;
use Swoole\Runtime;
use ZM\Event\EventHandler;
use Exception;
use Swoole\WebSocket\Server;
@@ -38,6 +39,7 @@ class FrameworkLoader
chdir(__DIR__ . '/../..');
define('WORKING_DIR', getcwd());
Runtime::enableCoroutine();
$this->requireGlobalFunctions();
$this->registerAutoloader('classLoader');
self::$settings = new GlobalConfig();
@@ -108,4 +110,4 @@ class FrameworkLoader
self::$run_time = microtime(true);
EventHandler::callSwooleEvent("WorkerStart", $server, $worker_id);
}
}
}

View File

@@ -1,5 +1,6 @@
<?php
use Framework\Console;
use Framework\ZMBuf;
use ZM\Utils\Context;
@@ -11,7 +12,7 @@ function classLoader($p) {
try {
require_once $filepath;
} catch (Exception $e) {
echo "Error when finding class: ".$p.PHP_EOL;
echo "Error when finding class: " . $p . PHP_EOL;
die;
}
}
@@ -148,18 +149,29 @@ function matchArgs($pattern, $context) {
} else return false;
}
function set_coroutine_params($array){
function set_coroutine_params($array) {
$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;
foreach(ZMBuf::$context as $c => $v) {
if(!Co::exists($c)) unset(ZMBuf::$context[$c]);
foreach (ZMBuf::$context as $c => $v) {
if (!Co::exists($c)) unset(ZMBuf::$context[$c]);
}
}
function context(){
function context() {
$cid = Co::getCid();
if(isset(ZMBuf::$context[$cid])) {
if (isset(ZMBuf::$context[$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');
}

View File

@@ -43,6 +43,9 @@ class EventHandler
} catch (Exception $e) {
/** @var Response $param1 */
$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());
Console::error("Internal server error (500), caused by uncaught exception.");
Console::log($e->getTraceAsString(), "gray");

View File

@@ -39,17 +39,19 @@ class MessageEvent implements SwooleEvent
*/
public function onActivate() {
ZMUtil::checkWait();
$conn = ConnectionManager::get($this->frame->fd);
set_coroutine_params(["server" => $this->server, "frame" => $this->frame, "connection" => $conn]);
try {
if (ConnectionManager::get($this->frame->fd)->getType() == "qq") {
if ($conn->getType() == "qq") {
$data = json_decode($this->frame->data, true);
if (isset($data["post_type"])) {
set_coroutine_params(["data" => $data, "connection" => $conn]);
EventHandler::callCQEvent($data, ConnectionManager::get($this->frame->fd), 0);
} else
EventHandler::callCQResponse($data);
}
foreach (ZMBuf::$events[SwooleEventAt::class] ?? [] as $v) {
if (strtolower($v->type) == "message" && $this->parseSwooleRule($v)) {
$conn = ConnectionManager::get($this->frame->fd);
$c = $v->class;
/** @var ModBase $class */
$class = new $c(["server" => $this->server, "frame" => $this->frame, "connection" => $conn], ModHandleType::SWOOLE_MESSAGE);
@@ -91,4 +93,4 @@ class MessageEvent implements SwooleEvent
}
return true;
}
}
}

View File

@@ -30,6 +30,7 @@ class WSCloseEvent implements SwooleEvent
public function onActivate() {
ZMUtil::checkWait();
ConnectionManager::close($this->fd);
set_coroutine_params(["server" => $this->server, "fd" => $this->fd]);
foreach(ZMBuf::$events[SwooleEventAt::class] ?? [] as $v) {
if(strtolower($v->type) == "close" && $this->parseSwooleRule($v)) {
$c = $v->class;
@@ -60,4 +61,4 @@ class WSCloseEvent implements SwooleEvent
private function parseSwooleRule($v) {
return true;
}
}
}

View File

@@ -55,6 +55,7 @@ class WSOpenEvent implements SwooleEvent
$this->conn = new $type_conn($this->server, $this->request->fd);
}
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) {
if (strtolower($v->type) == "open" && $this->parseSwooleRule($v) === true) {
$c = $v->class;
@@ -90,4 +91,4 @@ class WSOpenEvent implements SwooleEvent
}
return true;
}
}
}

View File

@@ -94,6 +94,7 @@ class WorkerStartEvent implements SwooleEvent
$class = new $class_name(["server" => $this->server, "worker_id" => $this->worker_id], ModHandleType::SWOOLE_WORKER_START);
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) {
/** @var AnnotationBase $v */
if (strtolower($v->type) == "workerstart") {
@@ -156,4 +157,4 @@ class WorkerStartEvent implements SwooleEvent
DataProvider::saveBuffer();
});
}
}
}

View File

@@ -21,6 +21,7 @@ class Response
*/
private $response;
private $is_end = false;
private $status_code;
public function __construct(\Swoole\Http\Response $response) {
$this->response = $response;
@@ -90,9 +91,14 @@ class Response
* @return mixed
*/
public function status($http_code, $reason = null) {
$this->status_code = $http_code;
return $this->response->status($http_code, $reason);
}
public function getStatusCode() {
return $this->status_code ?? 200;
}
/**
* @param $http_code
* @param $reason