@@ -22,8 +22,6 @@ class Socket extends AConnection
2222 private $ socket = false ;
2323
2424 private const POSSIBLE_TIMEOUTS_CODES = [11 , 10060 ];
25- /** @var float|null */
26- private $ timetAtTimeoutConfiguration ;
2725
2826 /**
2927 * Create socket connection
@@ -69,18 +67,14 @@ public function write(string $buffer)
6967 throw new ConnectException ('Not initialized socket ' );
7068 }
7169
72- $ size = mb_strlen ($ buffer , '8bit ' );
73- $ sent = 0 ;
74-
7570 if (Bolt::$ debug )
7671 $ this ->printHex ($ buffer );
7772
73+ $ size = mb_strlen ($ buffer , '8bit ' );
7874 while (0 < $ size ) {
7975 $ sent = @socket_write ($ this ->socket , $ buffer , $ size );
80- if ($ sent === false ) {
76+ if ($ sent === false )
8177 $ this ->throwConnectException ();
82- }
83-
8478 $ buffer = mb_strcut ($ buffer , $ sent , null , '8bit ' );
8579 $ size -= $ sent ;
8680 }
@@ -94,17 +88,14 @@ public function write(string $buffer)
9488 */
9589 public function read (int $ length = 2048 ): string
9690 {
97- $ output = '' ;
98-
99- if ($ this ->socket === false ) {
91+ if ($ this ->socket === false )
10092 throw new ConnectException ('Not initialized socket ' );
101- }
10293
94+ $ output = '' ;
10395 do {
10496 $ readed = @socket_read ($ this ->socket , $ length - mb_strlen ($ output , '8bit ' ), PHP_BINARY_READ );
105- if ($ readed === false ) {
97+ if ($ readed === false )
10698 $ this ->throwConnectException ();
107- }
10899 $ output .= $ readed ;
109100 } while (mb_strlen ($ output , '8bit ' ) < $ length );
110101
@@ -125,20 +116,21 @@ public function disconnect()
125116 }
126117 }
127118
128- public function setTimeout (float $ timeout ): void
119+ public function setTimeout (float $ timeout )
129120 {
130121 parent ::setTimeout ($ timeout );
131122 $ this ->configureTimeout ();
132123 }
133124
134- private function configureTimeout (): void
125+ private function configureTimeout ()
135126 {
127+ if ($ this ->socket === false )
128+ return ;
136129 $ timeoutSeconds = floor ($ this ->timeout );
137130 $ microSeconds = floor (($ this ->timeout - $ timeoutSeconds ) * 1000000 );
138131 $ timeoutOption = ['sec ' => $ timeoutSeconds , 'usec ' => $ microSeconds ];
139132 socket_set_option ($ this ->socket , SOL_SOCKET , SO_RCVTIMEO , $ timeoutOption );
140133 socket_set_option ($ this ->socket , SOL_SOCKET , SO_SNDTIMEO , $ timeoutOption );
141- $ this ->timetAtTimeoutConfiguration = microtime (true );
142134 }
143135
144136 /**
@@ -149,11 +141,8 @@ private function throwConnectException(): void
149141 {
150142 $ code = socket_last_error ($ this ->socket );
151143 if (in_array ($ code , self ::POSSIBLE_TIMEOUTS_CODES )) {
152- $ timediff = microtime (true ) - $ this ->timetAtTimeoutConfiguration ;
153- if ($ timediff >= $ this ->timeout ) {
154- throw ConnectionTimeoutException::createFromTimeout ($ this ->timeout );
155- }
156- } else if ($ code !== 0 ) {
144+ throw new ConnectionTimeoutException ('Connection timeout reached after ' . $ this ->timeout . ' seconds. ' );
145+ } elseif ($ code !== 0 ) {
157146 throw new ConnectException (socket_strerror ($ code ), $ code );
158147 }
159148 }
0 commit comments