private function write(string $data, bool $close = false): Promise
    {
        if ($this->status & self::CLOSED_WR) {
            return new Failure(new ClientException("The client disconnected"));
        }

        if (!$this->writeDeferred) {
            $bytesWritten = @\fwrite($this->socket, $data);

            if ($bytesWritten === false) {
                $this->close();
                return new Failure(new ClientException("The client disconnected"));
            }

            if ($bytesWritten !== 0) {
                $this->timeoutCache->renew($this->id);
            }

            if ($bytesWritten === \strlen($data)) {
                if ($close) {
                    $this->close();
                }

                return new Success;
            }

            $this->writeDeferred = new Deferred;
            $data = \substr($data, $bytesWritten);
        }

        $this->writeBuffer .= $data;

        Loop::enable($this->writeWatcher);

        $promise = $this->writeDeferred->promise();

        if ($close) {
            Loop::cancel($this->readWatcher);
            $this->status |= self::CLOSED_WR;
            $promise->onResolve([$this, "close"]);
        }

        return $promise;
    }
private function write(string $data, bool $close = false): Promise
    {
        if ($this->status & self::CLOSED_WR) {
            return new Failure(new ClientException("The client disconnected"));
        }

        if (!$this->writeDeferred) {
            $bytesWritten = @\fwrite($this->socket, $data);

            if ($bytesWritten === false) {
                $this->close();
                return new Failure(new ClientException("The client disconnected"));
            }

            if ($bytesWritten !== 0) {
                $this->timeoutCache->renew($this->id);
            }

            if ($bytesWritten === \strlen($data)) {
                if ($close) {
                    $this->close();
                }

                return new Success;
            }

            $this->writeDeferred = new Deferred;
            $data = \substr($data, $bytesWritten);
        }

        $this->writeBuffer .= $data;

        Loop::enable($this->writeWatcher);

        $promise = $this->writeDeferred->promise();

        if ($close) {
            Loop::cancel($this->readWatcher);
            $this->status |= self::CLOSED_WR;
            $promise->onResolve([$this, "close"]);
        }

        return $promise;
    }
private function write ( string $ data , bool $ close = false ) : Promise { if ( $ this -> status & self :: CLOSED_WR ) { return new Failure ( new ClientException ( "The client disconnected" ) ) ; } if ( ! $ this -> writeDeferred ) { $ bytesWritten = @ \ fwrite ( $ this -> socket , $ data ) ; if ( $ bytesWritten === false ) { $ this -> close ( ) ; return new Failure ( new ClientException ( "The client disconnected" ) ) ; } if ( $ bytesWritten !== 0 ) { $ this -> timeoutCache -> renew ( $ this -> id ) ; } if ( $ bytesWritten === \ strlen ( $ data ) ) { if ( $ close ) { $ this -> close ( ) ; } return new Success ; } $ this -> writeDeferred = new Deferred ; $ data = \ substr ( $ data , $ bytesWritten ) ; } $ this -> writeBuffer .= $ data ; Loop :: enable ( $ this -> writeWatcher ) ; $ promise = $ this -> writeDeferred -> promise ( ) ; if ( $ close ) { Loop :: cancel ( $ this -> readWatcher ) ; $ this -> status |= self :: CLOSED_WR ; $ promise -> onResolve ( [ $ this , "close" ] ) ; } return $ promise ; }
Adds the given data to the buffer of data to be written to the client socket. Returns a promise that resolves
once the client write buffer has emptied.

@param string $data The data to write.
@param bool $close If true, close the client after the given chunk of data has been written.

@return \Amp\Promise
Adds the given data to the buffer of data to be written to the client socket . Returns a promise that resolves once the client write buffer has emptied .