1515 */
1616class Socket extends AConnection
1717{
18-
1918 /**
2019 * @var resource|object|bool
2120 */
2221 private $ socket = false ;
2322
2423 private const POSSIBLE_TIMEOUTS_CODES = [11 , 10060 ];
25- /** @var float|null */
26- private $ timetAtTimeoutConfiguration ;
2724
2825 /**
2926 * Create socket connection
@@ -69,18 +66,14 @@ public function write(string $buffer)
6966 throw new ConnectException ('Not initialized socket ' );
7067 }
7168
72- $ size = mb_strlen ($ buffer , '8bit ' );
73- $ sent = 0 ;
74-
7569 if (Bolt::$ debug )
7670 $ this ->printHex ($ buffer );
7771
72+ $ size = mb_strlen ($ buffer , '8bit ' );
7873 while (0 < $ size ) {
7974 $ sent = @socket_write ($ this ->socket , $ buffer , $ size );
80- if ($ sent === false ) {
75+ if ($ sent === false )
8176 $ this ->throwConnectException ();
82- }
83-
8477 $ buffer = mb_strcut ($ buffer , $ sent , null , '8bit ' );
8578 $ size -= $ sent ;
8679 }
@@ -94,22 +87,19 @@ public function write(string $buffer)
9487 */
9588 public function read (int $ length = 2048 ): string
9689 {
97- $ output = '' ;
98-
99- if ($ this ->socket === false ) {
90+ if ($ this ->socket === false )
10091 throw new ConnectException ('Not initialized socket ' );
101- }
10292
93+ $ output = '' ;
10394 do {
10495 $ readed = @socket_read ($ this ->socket , $ length - mb_strlen ($ output , '8bit ' ), PHP_BINARY_READ );
105- if ($ readed === false ) {
96+ if ($ readed === false )
10697 $ this ->throwConnectException ();
107- }
10898 $ output .= $ readed ;
10999 } while (mb_strlen ($ output , '8bit ' ) < $ length );
110100
111101 if (Bolt::$ debug )
112- $ this ->printHex ($ output , false );
102+ $ this ->printHex ($ output , ' S: ' );
113103
114104 return $ output ;
115105 }
@@ -125,20 +115,21 @@ public function disconnect()
125115 }
126116 }
127117
128- public function setTimeout (float $ timeout ): void
118+ public function setTimeout (float $ timeout )
129119 {
130120 parent ::setTimeout ($ timeout );
131121 $ this ->configureTimeout ();
132122 }
133123
134- private function configureTimeout (): void
124+ private function configureTimeout ()
135125 {
126+ if ($ this ->socket === false )
127+ return ;
136128 $ timeoutSeconds = floor ($ this ->timeout );
137129 $ microSeconds = floor (($ this ->timeout - $ timeoutSeconds ) * 1000000 );
138130 $ timeoutOption = ['sec ' => $ timeoutSeconds , 'usec ' => $ microSeconds ];
139131 socket_set_option ($ this ->socket , SOL_SOCKET , SO_RCVTIMEO , $ timeoutOption );
140132 socket_set_option ($ this ->socket , SOL_SOCKET , SO_SNDTIMEO , $ timeoutOption );
141- $ this ->timetAtTimeoutConfiguration = microtime (true );
142133 }
143134
144135 /**
@@ -149,11 +140,8 @@ private function throwConnectException(): void
149140 {
150141 $ code = socket_last_error ($ this ->socket );
151142 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 ) {
143+ throw new ConnectionTimeoutException ('Connection timeout reached after ' . $ this ->timeout . ' seconds. ' );
144+ } elseif ($ code !== 0 ) {
157145 throw new ConnectException (socket_strerror ($ code ), $ code );
158146 }
159147 }
0 commit comments