@@ -20,6 +20,16 @@ class Server
20
20
*/
21
21
protected $ master ;
22
22
23
+ /**
24
+ * @var string $host Host the server will be bound to.
25
+ */
26
+ private string $ host = '' ;
27
+
28
+ /**
29
+ * @var int $port Port the server will listen on.
30
+ */
31
+ private int $ port = 0 ;
32
+
23
33
/**
24
34
* @var resource $icpSocket
25
35
*/
@@ -30,6 +40,21 @@ class Server
30
40
*/
31
41
private string $ ipcSocketPath ;
32
42
43
+ /**
44
+ * @var string $ipcOwner If set, owner of the ipc socket will be changed to this value.
45
+ */
46
+ private string $ ipcOwner = '' ;
47
+
48
+ /**
49
+ * @var string $ipcGroup If set, group of the ipc socket will be changed to this value.
50
+ */
51
+ private string $ ipcGroup = '' ;
52
+
53
+ /**
54
+ * @var int $ipcMode If set, chmod of the ipc socket will be changed to this value.
55
+ */
56
+ private int $ ipcMode = 0 ;
57
+
33
58
/**
34
59
* @var array Holds all connected sockets
35
60
*/
@@ -90,11 +115,9 @@ public function __construct(
90
115
int $ port = 8000 ,
91
116
string $ ipcSocketPath = '/tmp/phpwss.sock '
92
117
) {
93
- ob_implicit_flush (); // php7.x -> int, php8.x => bool, default 1 or true
94
- $ this ->createSocket ($ host , $ port );
95
- $ this ->openIPCSocket ($ ipcSocketPath );
96
- $ this ->timers = new TimerCollection ();
97
- $ this ->log ('Server created ' );
118
+ $ this ->host = $ host ;
119
+ $ this ->port = $ port ;
120
+ $ this ->ipcSocketPath = $ ipcSocketPath ;
98
121
}
99
122
100
123
/**
@@ -105,6 +128,12 @@ public function __construct(
105
128
*/
106
129
public function run (): void
107
130
{
131
+ ob_implicit_flush ();
132
+ $ this ->createSocket ($ this ->host , $ this ->port );
133
+ $ this ->openIPCSocket ($ this ->ipcSocketPath );
134
+ $ this ->timers = new TimerCollection ();
135
+ $ this ->log ('Server created ' );
136
+
108
137
while (true ) {
109
138
$ this ->timers ->runAll ();
110
139
@@ -562,7 +591,15 @@ private function openIPCSocket(string $ipcSocketPath): void
562
591
if (socket_bind ($ this ->icpSocket , $ ipcSocketPath ) === false ) {
563
592
throw new \RuntimeException ('Could not bind to ipc socket. ' );
564
593
}
565
- $ this ->ipcSocketPath = $ ipcSocketPath ;
594
+ if ($ this ->ipcOwner !== '' ) {
595
+ chown ($ ipcSocketPath , $ this ->ipcOwner );
596
+ }
597
+ if ($ this ->ipcGroup !== '' ) {
598
+ chgrp ($ ipcSocketPath , $ this ->ipcGroup );
599
+ }
600
+ if ($ this ->ipcMode !== 0 ) {
601
+ chmod ($ ipcSocketPath , $ this ->ipcMode );
602
+ }
566
603
}
567
604
568
605
/**
@@ -594,4 +631,37 @@ private function handleIPC(): void
594
631
throw new \RuntimeException ('Invalid IPC message received. ' );
595
632
}
596
633
}
634
+
635
+ /**
636
+ * Sets the icpOwner value.
637
+ *
638
+ * @param string $owner
639
+ * @return void
640
+ */
641
+ public function setIPCOwner (string $ owner ): void
642
+ {
643
+ $ this ->ipcOwner = $ owner ;
644
+ }
645
+
646
+ /**
647
+ * Sets the ipcGroup value.
648
+ *
649
+ * @param string $group
650
+ * @return void
651
+ */
652
+ public function setIPCGroup (string $ group ): void
653
+ {
654
+ $ this ->ipcGroup = $ group ;
655
+ }
656
+
657
+ /**
658
+ * Sets the ipcMode value.
659
+ *
660
+ * @param int $mode
661
+ * @return void
662
+ */
663
+ public function setIPCMode (int $ mode ): void
664
+ {
665
+ $ this ->ipcMode = $ mode ;
666
+ }
597
667
}
0 commit comments