Skip to content

Commit e11f6ef

Browse files
committed
implemented timeout correctly for socket class
1 parent adce1eb commit e11f6ef

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

src/connection/Socket.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,7 @@ public function connect(): bool
4242

4343
socket_set_option($this->socket, SOL_TCP, TCP_NODELAY, 1);
4444
socket_set_option($this->socket, SOL_SOCKET, SO_KEEPALIVE, 1);
45-
$timeoutSeconds = floor($this->timeout);
46-
$microSeconds = floor(($this->timeout - $timeoutSeconds) * 1000000);
47-
$timeoutOption = ['sec' => $timeoutSeconds, 'usec' => $microSeconds];
48-
socket_set_option($this->socket, SOL_SOCKET, SO_RCVTIMEO, $timeoutOption);
49-
socket_set_option($this->socket, SOL_SOCKET, SO_SNDTIMEO, $timeoutOption);
45+
$this->configureTimeout();
5046

5147
$conn = @socket_connect($this->socket, $this->ip, $this->port);
5248
if (!$conn) {
@@ -125,4 +121,22 @@ public function disconnect()
125121
@socket_close($this->socket);
126122
}
127123
}
124+
125+
public function setTimeout(float $timeout): void
126+
{
127+
parent::setTimeout($timeout);
128+
$this->configureTimeout();
129+
}
130+
131+
/**
132+
* @return void
133+
*/
134+
private function configureTimeout(): void
135+
{
136+
$timeoutSeconds = floor($this->timeout);
137+
$microSeconds = floor(($this->timeout - $timeoutSeconds) * 1000000);
138+
$timeoutOption = ['sec' => $timeoutSeconds, 'usec' => $microSeconds];
139+
socket_set_option($this->socket, SOL_SOCKET, SO_RCVTIMEO, $timeoutOption);
140+
socket_set_option($this->socket, SOL_SOCKET, SO_SNDTIMEO, $timeoutOption);
141+
}
128142
}

0 commit comments

Comments
 (0)