1111
1212use Exception ;
1313use Symfony \Component \Console \Helper \Table ;
14- use Symfony \Component \Console \Output \ConsoleOutput as Output ;
14+ use Symfony \Component \Console \Output \ConsoleOutput ;
15+ use Symfony \Component \Console \Output \OutputInterface ;
1516use FastD \Swoole \Support \Watcher ;
1617use swoole_process ;
1718use swoole_server ;
1819use swoole_server_port ;
1920use swoole_websocket_server ;
2021use swoole_http_server ;
21- use Symfony \Component \Console \Output \OutputInterface ;
2222
2323/**
2424 * Class Server
2525 * @package FastD\Swoole
2626 */
2727abstract class Server
2828{
29- const VERSION = '2.0 .0 ' ;
29+ const VERSION = '2.1 .0 ' ;
3030
3131 /**
3232 * @var $name
3333 */
3434 protected $ name ;
3535
3636 /**
37- * @var Output
37+ * @var OutputInterface
3838 */
3939 protected $ output ;
4040
@@ -55,10 +55,7 @@ abstract class Server
5555 'open_cpu_affinity ' => true ,
5656 ];
5757
58- /**
59- * @var string
60- */
61- protected $ scheme = 'tcp ' ;
58+ const SCHEME = 'tcp ' ;
6259
6360 /**
6461 * @var string
@@ -73,6 +70,11 @@ abstract class Server
7370 /**
7471 * @var string
7572 */
73+ protected $ pidFile ;
74+
75+ /**
76+ * @var int
77+ */
7678 protected $ pid ;
7779
7880 /**
@@ -113,20 +115,14 @@ public function __construct($name, $address = null, array $config = [], OutputIn
113115 {
114116 $ this ->name = $ name ;
115117
116- if (null === $ address ) {
117- $ address = sprintf ('%s://%s:%s ' , $ this ->scheme , get_local_ip (), $ this ->port );
118- }
119-
120- $ info = parse_url ($ address );
121-
122- $ this ->host = $ info ['host ' ];
123- $ this ->port = $ info ['port ' ];
118+ if (null !== $ address ) {
119+ $ info = parse_url ($ address );
124120
125- if ( null === $ output ) {
126- $ output = new Output () ;
121+ $ this -> host = $ info [ ' host ' ];
122+ $ this -> port = $ info [ ' port ' ] ;
127123 }
128124
129- $ this ->output = $ output ;
125+ $ this ->output = null === $ output ? new ConsoleOutput () : $ output ;
130126
131127 $ this ->configure ($ config );
132128 }
@@ -137,15 +133,15 @@ public function __construct($name, $address = null, array $config = [], OutputIn
137133 */
138134 public function configure (array $ config )
139135 {
140- $ this ->config = array_merge ($ this ->config , ( array ) $ config );
136+ $ this ->config = array_merge ($ this ->config , $ config );
141137
142138 if (isset ($ this ->config ['pid_file ' ])) {
143- $ this ->pid = $ this ->config ['pid_file ' ];
139+ $ this ->pidFile = $ this ->config ['pid_file ' ];
144140 }
145141
146- if (empty ($ this ->pid )) {
147- $ this ->pid = '/tmp/ ' . $ this ->name . '.pid ' ;
148- $ this ->config ['pid_file ' ] = $ this ->pid ;
142+ if (empty ($ this ->pidFile )) {
143+ $ this ->pidFile = '/tmp/ ' . str_replace ( ' ' , ' - ' , $ this ->name ) . '.pid ' ;
144+ $ this ->config ['pid_file ' ] = $ this ->pidFile ;
149145 }
150146
151147 return $ this ;
@@ -176,7 +172,7 @@ public function daemon()
176172 */
177173 public function getScheme ()
178174 {
179- return $ this -> scheme ;
175+ return static :: SCHEME ;
180176 }
181177
182178 /**
@@ -196,6 +192,8 @@ public function getPort()
196192 }
197193
198194 /**
195+ * Get client connection server's file descriptor.
196+ *
199197 * @return int
200198 */
201199 public function getFileDescriptor ()
@@ -208,18 +206,16 @@ public function getFileDescriptor()
208206 */
209207 public function getSocketType ()
210208 {
211- switch ($ this ->scheme ) {
212- case 'tcp ' :
213- $ type = SWOOLE_SOCK_TCP ;
214- break ;
209+ switch (static ::SCHEME ) {
215210 case 'udp ' :
216211 $ type = SWOOLE_SOCK_UDP ;
217212 break ;
218213 case 'unix ' :
219214 $ type = SWOOLE_UNIX_STREAM ;
220215 break ;
216+ case 'tcp ' :
221217 default :
222- $ type = '' ;
218+ $ type = SWOOLE_SOCK_TCP ;
223219 }
224220
225221 return $ type ;
@@ -228,6 +224,14 @@ public function getSocketType()
228224 /**
229225 * @return string
230226 */
227+ public function getPidFile ()
228+ {
229+ return $ this ->pidFile ;
230+ }
231+
232+ /**
233+ * @return int
234+ */
231235 public function getPid ()
232236 {
233237 return $ this ->pid ;
@@ -266,7 +270,7 @@ protected function handleCallback()
266270 if ('udp ' === $ this ->getScheme ()) {
267271 $ callbacks = ['onPacket ' ,];
268272 } else {
269- $ callbacks = ['onConnect ' , 'onClose ' , 'onReceive ' ];
273+ $ callbacks = ['onConnect ' , 'onClose ' , 'onReceive ' , ];
270274 }
271275 if (in_array ($ value , $ callbacks )) {
272276 $ this ->swoole ->on (lcfirst (substr ($ value , 2 )), [$ this , $ value ]);
@@ -364,11 +368,11 @@ public static function createServer($name, $address, array $config = [])
364368 public function start ()
365369 {
366370 if ($ this ->isRunning ()) {
367- $ this ->output ->write (sprintf ('Server <info>[%s] %s:%s</info> address already in use ' , $ this ->name , $ this ->host , $ this ->port ) . PHP_EOL );
371+ $ this ->output ->writeln (sprintf ('Server <info>[%s] %s:%s</info> address already in use ' , $ this ->name , $ this ->host , $ this ->port ));
368372 } else {
369373 try {
370374 $ this ->bootstrap ();
371- if (!file_exists ($ dir = dirname ($ this ->pid ))) {
375+ if (!file_exists ($ dir = dirname ($ this ->pidFile ))) {
372376 mkdir ($ dir , 0755 , true );
373377 }
374378 // 多端口监听
@@ -380,6 +384,11 @@ public function start()
380384 foreach ($ this ->processes as $ process ) {
381385 $ this ->swoole ->addProcess ($ process ->getProcess ());
382386 }
387+
388+ $ this ->output ->writeln (sprintf ("Server: <info>%s</info> " , $ this ->name ));
389+ $ this ->output ->writeln (sprintf ('App version: <info>%s</info> ' , Server::VERSION ));
390+ $ this ->output ->writeln (sprintf ('Swoole version: <info>%s</info> ' , SWOOLE_VERSION ));
391+
383392 $ this ->swoole ->start ();
384393 } catch (Exception $ e ) {
385394 $ this ->output ->write ("<error> {$ e ->getMessage ()}</error> \n" );
@@ -395,15 +404,17 @@ public function start()
395404 public function shutdown ()
396405 {
397406 if (!$ this ->isRunning ()) {
398- $ this ->output ->write (sprintf ('Server <info>%s</info> is not running... ' , $ this ->name ) . PHP_EOL );
407+ $ this ->output ->writeln (sprintf ('Server <info>%s</info> is not running... ' , $ this ->name ));
399408 return -1 ;
400409 }
401410
402- $ pid = (int ) @file_get_contents ($ this ->pid );
403-
404- posix_kill ($ pid , SIGTERM );
411+ $ pid = (int ) @file_get_contents ($ this ->getPidFile ());
412+ if (process_kill ($ pid , SIGTERM )) {
413+ unlink ($ this ->pidFile );
414+ }
405415
406- $ this ->output ->write (sprintf ('Server <info>%s</info> [<info>#%s</info>] is shutdown... ' , $ this ->name , $ pid ) . PHP_EOL );
416+ $ this ->output ->writeln (sprintf ('Server <info>%s</info> [<info>#%s</info>] is shutdown... ' , $ this ->name , $ pid ));
417+ $ this ->output ->writeln (sprintf ('PID file %s is unlink ' , $ this ->pidFile ), OutputInterface::VERBOSITY_DEBUG );
407418
408419 return 0 ;
409420 }
@@ -414,15 +425,15 @@ public function shutdown()
414425 public function reload ()
415426 {
416427 if (!$ this ->isRunning ()) {
417- $ this ->output ->write (sprintf ('Server <info>%s</info> is not running... ' , $ this ->name ) . PHP_EOL );
428+ $ this ->output ->writeln (sprintf ('Server <info>%s</info> is not running... ' , $ this ->name ));
418429 return -1 ;
419430 }
420431
421- $ pid = (int )@file_get_contents ($ this ->getPid ());
432+ $ pid = (int )@file_get_contents ($ this ->getPidFile ());
422433
423434 posix_kill ($ pid , SIGUSR1 );
424435
425- $ this ->output ->write (sprintf ('Server <info>%s</info> [<info>%s</info>] is reloading... ' , $ this ->name , $ pid ) . PHP_EOL );
436+ $ this ->output ->writeln (sprintf ('Server <info>%s</info> [<info>%s</info>] is reloading... ' , $ this ->name , $ pid ));
426437
427438 return 0 ;
428439 }
@@ -433,7 +444,6 @@ public function reload()
433444 public function restart ()
434445 {
435446 $ this ->shutdown ();
436- sleep (1 );
437447 return $ this ->start ();
438448 }
439449
@@ -443,7 +453,7 @@ public function restart()
443453 public function status ()
444454 {
445455 if (!$ this ->isRunning ()) {
446- $ this ->output ->write (sprintf ('Server <info>%s</info> is not running... ' , $ this ->name ) . PHP_EOL );
456+ $ this ->output ->writeln (sprintf ('Server <info>%s</info> is not running... ' , $ this ->name ));
447457 return -1 ;
448458 }
449459
@@ -464,16 +474,16 @@ public function status()
464474 $ output [$ key ] = array_combine ($ headers , $ value );
465475 }
466476
467- $ this ->output ->write (sprintf ("Server: <info>%s</info> " , $ this ->name ) . PHP_EOL );
468- $ this ->output ->write (sprintf ('App version <info>%s</info> ' , Server::VERSION ) . PHP_EOL );
469- $ this ->output ->write (sprintf ('Swoole version <info>%s</info> ' , SWOOLE_VERSION ) . PHP_EOL );
470- $ this ->output ->write (sprintf ("PID file: <info>%s</info>, PID: <info>%s</info> " , $ this ->pid , (int ) @file_get_contents ($ this ->pid )) . PHP_EOL );
471477 $ table = new Table ($ this ->output );
472478 $ table
473479 ->setHeaders ($ headers )
474480 ->setRows ($ output )
475481 ;
476482
483+ $ this ->output ->writeln (sprintf ("Server: <info>%s</info> " , $ this ->name ));
484+ $ this ->output ->writeln (sprintf ('App version: <info>%s</info> ' , Server::VERSION ));
485+ $ this ->output ->writeln (sprintf ('Swoole version: <info>%s</info> ' , SWOOLE_VERSION ));
486+ $ this ->output ->writeln (sprintf ("PID file: <info>%s</info>, PID: <info>%s</info> " , $ this ->pidFile , (int ) @file_get_contents ($ this ->pidFile )) . PHP_EOL );
477487 $ table ->render ();
478488
479489 unset($ table , $ headers , $ output );
@@ -497,7 +507,7 @@ public function watch(array $directories = ['.'])
497507 }
498508
499509 foreach ($ directories as $ directory ) {
500- $ this ->output ->write (sprintf ('Watching directory: ["<info>%s</info>"] ' , realpath ($ directory )) . PHP_EOL );
510+ $ this ->output ->writeln (sprintf ('Watching directory: ["<info>%s</info>"] ' , realpath ($ directory )));
501511 }
502512
503513 $ watcher = new Watcher ($ this ->output );
@@ -516,9 +526,10 @@ public function watch(array $directories = ['.'])
516526 */
517527 public function isRunning ()
518528 {
519- if (file_exists ($ this ->config [ ' pid_file ' ] )) {
520- return posix_kill ( file_get_contents ($ this ->config [ ' pid_file ' ] ), 0 );
529+ if (file_exists ($ this ->pidFile )) {
530+ return process_kill (( int ) file_get_contents ($ this ->pidFile ), 0 );
521531 }
532+
522533 return process_is_running ("{$ this ->name } master " );
523534 }
524535
@@ -531,22 +542,19 @@ public function isRunning()
531542 public function onStart (swoole_server $ server )
532543 {
533544 if (version_compare (SWOOLE_VERSION , '1.9.5 ' , '< ' )) {
534- file_put_contents ($ this ->pid , $ server ->master_pid );
545+ file_put_contents ($ this ->pidFile , $ server ->master_pid );
546+ $ this ->pid = $ server ->master_pid ;
535547 }
536548
537- $ this ->output ->write (sprintf ("Server: <info>%s</info> " , $ this ->name ) . PHP_EOL );
538- $ this ->output ->write (sprintf ('App version <info>%s</info> ' , Server::VERSION ) . PHP_EOL );
539- $ this ->output ->write (sprintf ('Swoole version <info>%s</info> ' , SWOOLE_VERSION ) . PHP_EOL );
540- $ this ->output ->write (sprintf ('PID file: <info>%s</info>, PID: <info>%s</info> ' , $ this ->pid , $ server ->master_pid ) . PHP_EOL );
541549 process_rename ($ this ->name . ' master ' );
542550
543- $ this ->output ->write (sprintf ("Server <info>%s://%s:%s</info> " , $ this ->getScheme (), $ this ->getHost (), $ this ->getPort ()) . PHP_EOL );
544-
551+ $ this ->output ->writeln (sprintf ("Listen: <info>%s://%s:%s</info> " , $ this ->getScheme (), $ this ->getHost (), $ this ->getPort ()));
545552 foreach ($ this ->listens as $ listen ) {
546- $ this ->output ->write (sprintf (" <info>➜ </info> Listen <info>%s://%s:%s</info> " , $ listen ->getScheme (), $ listen ->getHost (), $ listen ->getPort ()) . PHP_EOL );
553+ $ this ->output ->writeln (sprintf (" <info> > </info> Listen: <info>%s://%s:%s</info> " , $ listen ->getScheme (), $ listen ->getHost (), $ listen ->getPort ()));
547554 }
548555
549- $ this ->output ->write (sprintf ('Server Master[<info>%s</info>] is started ' , $ server ->master_pid ) . PHP_EOL );
556+ $ this ->output ->writeln (sprintf ('PID file: <info>%s</info>, PID: <info>%s</info> ' , $ this ->pidFile , $ server ->master_pid ));
557+ $ this ->output ->writeln (sprintf ('Server Master[<info>%s</info>] is started ' , $ server ->master_pid ), OutputInterface::VERBOSITY_DEBUG );
550558 }
551559
552560 /**
@@ -557,11 +565,11 @@ public function onStart(swoole_server $server)
557565 */
558566 public function onShutdown (swoole_server $ server )
559567 {
560- if (file_exists ($ this ->pid )) {
561- unlink ($ this ->pid );
568+ if (file_exists ($ this ->pidFile )) {
569+ unlink ($ this ->pidFile );
562570 }
563571
564- $ this ->output ->write (sprintf ('Server <info>%s</info> Master[<info>%s</info>] is shutdown ' , $ this ->name , $ server ->master_pid ) . PHP_EOL );
572+ $ this ->output ->writeln (sprintf ('Server <info>%s</info> Master[<info>%s</info>] is shutdown ' , $ this ->name , $ server ->master_pid ), OutputInterface:: VERBOSITY_DEBUG );
565573 }
566574
567575 /**
@@ -573,7 +581,7 @@ public function onManagerStart(swoole_server $server)
573581 {
574582 process_rename ($ this ->getName () . ' manager ' );
575583
576- $ this ->output ->write (sprintf ('Server Manager[<info>%s</info>] is started ' , $ server ->manager_pid ) . PHP_EOL );
584+ $ this ->output ->writeln (sprintf ('Server Manager[<info>%s</info>] is started ' , $ server ->manager_pid ), OutputInterface:: VERBOSITY_DEBUG );
577585 }
578586
579587 /**
@@ -583,7 +591,7 @@ public function onManagerStart(swoole_server $server)
583591 */
584592 public function onManagerStop (swoole_server $ server )
585593 {
586- $ this ->output ->write (sprintf ('Server <info>%s</info> Manager[<info>%s</info>] is shutdown. ' , $ this ->name , $ server ->manager_pid ) . PHP_EOL );
594+ $ this ->output ->writeln (sprintf ('Server <info>%s</info> Manager[<info>%s</info>] is shutdown. ' , $ this ->name , $ server ->manager_pid ), OutputInterface:: VERBOSITY_DEBUG );
587595 }
588596
589597 /**
@@ -595,7 +603,7 @@ public function onWorkerStart(swoole_server $server, $worker_id)
595603 {
596604 process_rename ($ this ->getName () . ' worker ' );
597605
598- $ this ->output ->write (sprintf ('Server Worker[<info>%s</info>] is started [<info>%s</info>] ' , $ server ->worker_pid , $ worker_id ) . PHP_EOL );
606+ $ this ->output ->writeln (sprintf ('Server Worker[<info>%s</info>] is started [<info>%s</info>] ' , $ server ->worker_pid , $ worker_id ), OutputInterface:: VERBOSITY_DEBUG );
599607 }
600608
601609 /**
@@ -605,7 +613,7 @@ public function onWorkerStart(swoole_server $server, $worker_id)
605613 */
606614 public function onWorkerStop (swoole_server $ server , $ worker_id )
607615 {
608- $ this ->output ->write (sprintf ('Server <info>%s</info> Worker[<info>%s</info>] is shutdown ' , $ this ->name , $ worker_id ) . PHP_EOL );
616+ $ this ->output ->writeln (sprintf ('Server <info>%s</info> Worker[<info>%s</info>] is shutdown ' , $ this ->name , $ worker_id ), OutputInterface:: VERBOSITY_DEBUG );
609617 }
610618
611619 /**
@@ -616,7 +624,7 @@ public function onWorkerStop(swoole_server $server, $worker_id)
616624 */
617625 public function onWorkerError (swoole_server $ server , $ workerId , $ workerPid , $ code )
618626 {
619- $ this ->output ->write (sprintf ('Server <info>%s:%s</info> Worker[<info>%s</info>] error. Exit code: [<question>%s</question>] ' , $ this ->name , $ workerPid , $ workerId , $ code ) . PHP_EOL );
627+ $ this ->output ->writeln (sprintf ('Server <info>%s:%s</info> Worker[<info>%s</info>] error. Exit code: [<question>%s</question>] ' , $ this ->name , $ workerPid , $ workerId , $ code ), OutputInterface:: VERBOSITY_DEBUG );
620628 }
621629
622630 /**
0 commit comments