@@ -35,21 +35,25 @@ public static function initialize(string $ip, int $port, int $timeout)
3535 return ;
3636 }
3737
38- self ::$ socket = socket_create (AF_INET , SOCK_STREAM , SOL_TCP );
38+ self ::$ socket = @ socket_create (AF_INET , SOCK_STREAM , SOL_TCP );
3939 if (!is_resource (self ::$ socket )) {
4040 Bolt::error ('Cannot create socket ' );
4141 return ;
4242 }
4343
44- socket_set_block (Socket::$ socket );
45- socket_set_option (Socket::$ socket , SOL_TCP , TCP_NODELAY , 1 );
46- socket_set_option (Socket::$ socket , SOL_SOCKET , SO_KEEPALIVE , 1 );
47- socket_set_option (Socket::$ socket , SOL_SOCKET , SO_RCVTIMEO , ['sec ' => $ timeout , 'usec ' => 0 ]);
48- socket_set_option (Socket::$ socket , SOL_SOCKET , SO_SNDTIMEO , ['sec ' => $ timeout , 'usec ' => 0 ]);
44+ if (socket_set_block (self ::$ socket ) === false ) {
45+ Bolt::error ('Cannot set socket into blocking mode ' );
46+ return ;
47+ }
48+
49+ socket_set_option (self ::$ socket , SOL_TCP , TCP_NODELAY , 1 );
50+ socket_set_option (self ::$ socket , SOL_SOCKET , SO_KEEPALIVE , 1 );
51+ socket_set_option (self ::$ socket , SOL_SOCKET , SO_RCVTIMEO , ['sec ' => $ timeout , 'usec ' => 0 ]);
52+ socket_set_option (self ::$ socket , SOL_SOCKET , SO_SNDTIMEO , ['sec ' => $ timeout , 'usec ' => 0 ]);
4953
5054 $ conn = socket_connect (self ::$ socket , $ ip , $ port );
5155 if (!$ conn ) {
52- $ code = socket_last_error (Socket ::$ socket );
56+ $ code = socket_last_error (self ::$ socket );
5357 Bolt::error (socket_strerror ($ code ), $ code );
5458 return ;
5559 }
@@ -128,12 +132,19 @@ public static function read(IUnpacker $unpacker)
128132 * Read buffer from socket
129133 * @param int $length
130134 * @return string
135+ * @throws Exception
131136 */
132137 public static function readBuffer (int $ length = 2048 ): string
133138 {
134139 $ output = '' ;
135140 do {
136- $ output .= socket_read (self ::$ socket , $ length - mb_strlen ($ output , '8bit ' ), PHP_BINARY_READ );
141+ $ readed = socket_read (self ::$ socket , $ length - mb_strlen ($ output , '8bit ' ), PHP_BINARY_READ );
142+ if ($ readed === false ) {
143+ $ code = socket_last_error (self ::$ socket );
144+ Bolt::error (socket_strerror ($ code ), $ code );
145+ } else {
146+ $ output .= $ readed ;
147+ }
137148 } while (mb_strlen ($ output , '8bit ' ) < $ length );
138149 return $ output ;
139150 }
0 commit comments