diff --git a/src/Socket/Client.php b/src/Socket/Client.php index 87a6ec1..a41ed3e 100644 --- a/src/Socket/Client.php +++ b/src/Socket/Client.php @@ -161,8 +161,23 @@ protected function processPayload(string $payload): void private function writeQueue(): void { foreach ($this->writeQueue as $data) { - \socket_write($this->socket, $data); - // Logger::debug('Respond %d bytes', $x); + $length = \strlen($data); + $written = 0; + while ($written < $length) { + $result = \socket_write($this->socket, \substr($data, $written)); + if ($result === false) { + $errno = \socket_last_error($this->socket); + // If the send buffer is temporarily full, suspend the fiber and retry + if ($errno === \SOCKET_EAGAIN || $errno === \SOCKET_EWOULDBLOCK) { + \socket_clear_error($this->socket); + \Fiber::suspend(); + continue; + } + // Unrecoverable error, stop writing + break 2; + } + $written += $result; + } } \socket_set_nonblock($this->socket);