src/Datagram/BasicDatagram.php
public function send(string $address, int $port, string $data): \Generator
    {
        if (!$this->isOpen()) {
            throw new UnavailableException('The datagram is no longer writable.');
        }
        
        $data = (string) $data;
        $length = strlen($data);
        $written = 0;
        $peer = Socket\makeName($address, $port);
        
        if ($this->writeQueue->isEmpty()) {
            if (0 === $length) {
                return $written;
            }

            $written = stream_socket_sendto($this->getResource(), substr($data, 0, self::MAX_PACKET_SIZE), 0, $peer);

            // Having difficulty finding a test to cover this scenario, but the check seems appropriate.
            if (false === $written || -1 === $written) {
                $message = 'Failed to write to datagram.';
                if ($error = error_get_last()) {
                    $message .= sprintf(' Errno: %d; %s', $error['type'], $error['message']);
                }
                throw new FailureException($message);
            }

            if ($length <= $written) {
                return $written;
            }
            
            $data = substr($data, $written);
        }

        if (null === $this->await) {
            $this->await = $this->createAwait($this->getResource(), $this->writeQueue);
            $this->await->listen();
        } elseif (!$this->await->isPending()) {
            $this->await->listen();
        }

        $delayed = new Delayed($this->onSendCancelled);
        $this->writeQueue->push([$data, $written, $peer, $delayed]);

        try {
            return yield $delayed;
        } catch (Throwable $exception) {
            $this->free($exception);
            throw $exception;
        }
    }
public function send(string $address, int $port, string $data): \Generator
    {
        if (!$this->isOpen()) {
            throw new UnavailableException('The datagram is no longer writable.');
        }
        
        $data = (string) $data;
        $length = strlen($data);
        $written = 0;
        $peer = Socket\makeName($address, $port);
        
        if ($this->writeQueue->isEmpty()) {
            if (0 === $length) {
                return $written;
            }

            $written = stream_socket_sendto($this->getResource(), substr($data, 0, self::MAX_PACKET_SIZE), 0, $peer);

            // Having difficulty finding a test to cover this scenario, but the check seems appropriate.
            if (false === $written || -1 === $written) {
                $message = 'Failed to write to datagram.';
                if ($error = error_get_last()) {
                    $message .= sprintf(' Errno: %d; %s', $error['type'], $error['message']);
                }
                throw new FailureException($message);
            }

            if ($length <= $written) {
                return $written;
            }
            
            $data = substr($data, $written);
        }

        if (null === $this->await) {
            $this->await = $this->createAwait($this->getResource(), $this->writeQueue);
            $this->await->listen();
        } elseif (!$this->await->isPending()) {
            $this->await->listen();
        }

        $delayed = new Delayed($this->onSendCancelled);
        $this->writeQueue->push([$data, $written, $peer, $delayed]);

        try {
            return yield $delayed;
        } catch (Throwable $exception) {
            $this->free($exception);
            throw $exception;
        }
    }
{@inheritdoc}