1515
1616namespace InitPHP \Socket \Common ;
1717
18+ use InitPHP \Socket \Server \ServerClient ;
19+ use InitPHP \Socket \Socket ;
1820use InitPHP \Socket \Exception \{SocketConnectionException , SocketException , SocketInvalidArgumentException };
1921
2022use const STREAM_CRYPTO_METHOD_SSLv2_SERVER ;
3234use function ini_get ;
3335use function stream_socket_accept ;
3436use function fclose ;
35- use function fread ;
36- use function fwrite ;
37- use function strlen ;
3837use function stream_set_timeout ;
3938use function stream_set_blocking ;
4039use function stream_socket_enable_crypto ;
@@ -48,10 +47,6 @@ trait StreamServerTrait
4847
4948 protected array $ options = [];
5049
51-
52- /** @var resource */
53- protected $ accept ;
54-
5550 public function __construct (string $ host , int $ port , $ argument )
5651 {
5752 $ this ->setHost ($ host )->setPort ($ port );
@@ -75,49 +70,66 @@ public function connection(): self
7570 throw new SocketConnectionException ('Connection Error : ' . $ errStr );
7671 }
7772 $ this ->socket = $ socket ;
78- $ this ->accept = $ accept ;
73+
74+ $ this ->clients [] = (new ServerClient ([
75+ 'type ' => $ this ->type === 'tls ' ? Socket::TLS : Socket::SSL ,
76+ 'host ' => $ this ->getHost (),
77+ 'port ' => $ this ->getPort (),
78+ ]))->__setSocket ($ accept );
79+
7980 return $ this ;
8081 }
8182
8283 public function disconnect (): bool
8384 {
84- if (isset ($ this ->socket )){
85- fclose ($ this ->socket );
86- }
87- if (isset ($ this ->accept )){
88- fclose ($ this ->accept );
85+ if (!empty ($ this ->clients )) {
86+ foreach ($ this ->clients as $ client ) {
87+ $ client ->close ();
88+ }
8989 }
90- return true ;
91- }
9290
93- public function read (int $ length = 1024 ): ?string
94- {
95- $ read = fread ($ this ->getSocket (), $ length );
96- return $ read === FALSE ? null : $ read ;
97- }
91+ if (!empty ($ this ->socket )){
92+ fclose ($ this ->socket );
93+ }
9894
99- public function write (string $ string ): ?int
100- {
101- $ write = fwrite ($ this ->getSocket (), $ string , strlen ($ string ));
102- return $ write === FALSE ? null : $ write ;
95+ return true ;
10396 }
10497
10598 public function timeout (int $ second ): self
10699 {
107- stream_set_timeout ($ this ->accept , $ second );
100+ if (!empty ($ this ->clients )) {
101+ foreach ($ this ->clients as $ client ) {
102+ stream_set_timeout ($ client ->getSocket (), $ second );
103+ }
104+ ServerClient::__setCallbacks ('stream_set_timeout ' , ['{socket} ' , $ second ]);
105+ }
106+
108107 return $ this ;
109108 }
110109
111110 public function blocking (bool $ mode = true ): self
112111 {
113- stream_set_blocking ($ this ->accept , $ mode );
112+ if (!empty ($ this ->clients )) {
113+ foreach ($ this ->clients as $ client ) {
114+ stream_set_blocking ($ client ->getSocket (), $ mode );
115+ }
116+ ServerClient::__setCallbacks ('stream_set_blocking ' , ['{socket} ' , $ mode ]);
117+ }
118+
119+
114120 return $ this ;
115121 }
116122
117123 public function crypto (?string $ method = null ): self
118124 {
119125 if (empty ($ method )){
120- stream_socket_enable_crypto ($ this ->accept , false );
126+ if (!empty ($ this ->clients )) {
127+ foreach ($ this ->clients as $ client ) {
128+ stream_socket_enable_crypto ($ client ->getSocket (), false );
129+ }
130+ ServerClient::__setCallbacks ('stream_socket_enable_crypto ' , ['{socket} ' , false ]);
131+ }
132+
121133 return $ this ;
122134 }
123135 $ method = strtolower ($ method );
@@ -134,7 +146,14 @@ public function crypto(?string $method = null): self
134146 if (!isset ($ algos [$ method ])){
135147 throw new SocketException ('Unsupported crypto method. This library supports: ' . implode (', ' , array_keys ($ algos )));
136148 }
137- stream_socket_enable_crypto ($ this ->accept , true , $ algos [$ method ]);
149+
150+ if (!empty ($ this ->clients )) {
151+ foreach ($ this ->clients as $ client ) {
152+ stream_socket_enable_crypto ($ client ->getSocket (), true , $ algos [$ method ]);
153+ }
154+ ServerClient::__setCallbacks ('stream_socket_enable_crypto ' , ['{socket} ' , true , $ algos [$ method ]]);
155+ }
156+
138157 return $ this ;
139158 }
140159
0 commit comments