11<?php
22
3+ declare (strict_types=1 );
4+
35use flight \adapter \SwooleAsyncRequest ;
46use flight \adapter \SwooleAsyncResponse ;
57use flight \AsyncBridge ;
68use flight \Engine ;
7- use Smf \ConnectionPool \ConnectionPool ;
8- use Smf \ConnectionPool \ConnectionPoolTrait ;
9- use Smf \ConnectionPool \Connectors \CoroutineMySQLConnector ;
109use Swoole \HTTP \Server as SwooleServer ;
1110use Swoole \HTTP \Request as SwooleRequest ;
1211use Swoole \HTTP \Response as SwooleResponse ;
1312use Swoole \Coroutine \MySQL ;
1413
15- class SwooleServerDriver {
16-
17- //use ConnectionPoolTrait;
14+ class SwooleServerDriver
15+ {
16+ //use ConnectionPoolTrait;
1817
19- /** @var SwooleServer */
18+ /** @var SwooleServer */
2019 protected $ Swoole ;
2120
22- /** @var Engine */
23- protected $ app ;
21+ /** @var Engine */
22+ protected $ app ;
2423
25- /** @var Async_Bridge */
26- protected $ Async_Bridge ;
27-
28- public function __construct (string $ host , int $ port , Engine $ app ) {
24+ public function __construct (string $ host , int $ port , Engine $ app )
25+ {
2926 $ this ->Swoole = new SwooleServer ($ host , $ port );
30- $ this ->app = $ app ;
27+ $ this ->app = $ app ;
3128
3229 $ this ->setDefault ();
3330 $ this ->bindWorkerEvents ();
3431 $ this ->bindHttpEvent ();
3532 }
3633
37- protected function setDefault () {
34+ protected function setDefault ()
35+ {
3836 $ this ->Swoole ->set ([
3937 'daemonize ' => false ,
4038 'dispatch_mode ' => 1 ,
@@ -51,81 +49,29 @@ protected function setDefault() {
5149 ]);
5250 }
5351
54- protected function bindHttpEvent () {
55- $ app = $ this ->app ;
56- $ AsyncBridge = new AsyncBridge ($ app );
57- $ this ->Swoole ->on ("Start " , function (SwooleServer $ server ) {
58- echo "Swoole http server is started at http://127.0.0.1:9501 \n" ;
59- });
52+ protected function bindHttpEvent ()
53+ {
54+ $ app = $ this ->app ;
55+ $ AsyncBridge = new AsyncBridge ($ app );
56+ $ this ->Swoole ->on ("Start " , function (SwooleServer $ server ) {
57+ echo "Swoole http server is started at http://127.0.0.1:9501 \n" ;
58+ });
6059
6160 $ this ->Swoole ->on ('Request ' , function (SwooleRequest $ request , SwooleResponse $ response ) use ($ AsyncBridge ) {
62- echo memory_get_peak_usage () . PHP_EOL ;
63- // $pool1 = $this->getConnectionPool('mysql');
64- // // /**@var MySQL $mysql */
65- // $pdo = $pool1->borrow();
66-
67- // $server_vars = $request->server;
68- // $server_vars = array_change_key_case($server_vars, CASE_UPPER);
69- // $_SERVER = $server_vars;
70- // $_GET = $request->get ?? [];
71- // $_POST = $request->post ?? [];
72- // $_COOKIE = $request->cookie ?? [];
73- // $_FILES = $request->files ?? [];
74- // $_REQUEST = array_merge($_GET, $_POST, $_COOKIE);
61+ $ SwooleAsyncRequest = new SwooleAsyncRequest ($ request );
62+ $ SwooleAsyncResponse = new SwooleAsyncResponse ($ response );
63+ $ AsyncBridge ->processRequest ($ SwooleAsyncRequest , $ SwooleAsyncResponse );
64+ $ response ->end ();
7565
76- // $Engine->start();
77-
78- // foreach($Engine->response()->headers() as $header => $value) {
79- // $response->header($header, $value);
80- // }
81-
82- // $response->status($Engine->response()->status());
83- // if($Engine->response()->getBody()) {
84- // $response->write($Engine->response()->getBody());
85- // }
86-
87- // $Engine->unregister('response');
88- // $Engine->unregister('request');
89- // $Engine->register('response', 'flight\net\Response');
90- // $Engine->register('request', 'flight\net\Request');
91-
92- $ SwooleAsyncRequest = new SwooleAsyncRequest ($ request );
93- $ SwooleAsyncResponse = new SwooleAsyncResponse ($ response );
94- $ AsyncBridge ->processRequest ($ SwooleAsyncRequest , $ SwooleAsyncResponse );
95- // $pool1->return($pdo);
96- $ response ->end ();
97-
98- gc_collect_cycles (); // Collect garbage to free memory
66+ gc_collect_cycles (); // Collect garbage to free memory
9967 });
10068 }
10169
102- protected function bindWorkerEvents () {
70+ protected function bindWorkerEvents ()
71+ {
10372 $ createPools = function () {
104-
105- // $database_config = $this->App->getServicesContainer()->Config->database;
106- // // All MySQL connections: [4 workers * 2 = 8, 4 workers * 10 = 40]
107- // $pool1 = new ConnectionPool(
108- // [
109- // 'minActive' => 20,
110- // 'maxActive' => 100,
111- // ],
112- // new CoroutineMySQLConnector,
113- // [
114- // 'host' => $database_config['host'],
115- // 'port' => $database_config['port'],
116- // 'user' => $database_config['username'],
117- // 'password' => $database_config['password'],
118- // 'database' => $database_config['dbname'],
119- // 'timeout' => 10,
120- // 'charset' => 'utf8mb4',
121- // 'strict_type' => true,
122- // 'fetch_mode' => true,
123- // ]);
124- // $pool1->init();
125- // $this->addConnectionPool('mysql', $pool1);
12673 };
12774 $ closePools = function () {
128- // $this->closeConnectionPools();
12975 };
13076 $ this ->Swoole ->on ('WorkerStart ' , $ createPools );
13177 $ this ->Swoole ->on ('WorkerStop ' , $ closePools );
@@ -136,4 +82,4 @@ public function start()
13682 {
13783 $ this ->Swoole ->start ();
13884 }
139- }
85+ }
0 commit comments