Wrapper around fwrite(), handles global request timeout

@param string $data String to be written

@return integer the number of bytes written.

@throws Net_Notifier_Socket_ConnectionException
336406
public function write($data)
    {
        if ($this->deadline) {
            stream_set_timeout($this->socket, max($this->deadline - time(), 1));
        }
        $written = fwrite($this->socket, $data);
        $this->checkTimeout();
        // http://www.php.net/manual/en/function.fwrite.php#96951
        if ($written < mb_strlen($data, '8bit')) {
            throw new Net_Notifier_Socket_ConnectionException(
                'Error writing request'
            );
        }
        return $written;
    }
578251
protected function send(string $data, float $timeout = 0, bool $end = false): \Generator
    {
        $this->buffer->push($data);

        $data = '';

        while (!$this->buffer->isEmpty()) {
            if (0 === $this->length) { // Read chunk length.
                if (false === ($position = $this->buffer->search("\r\n"))) {
                    return yield from parent::send($data, $timeout, $end);
                }

                $length = rtrim($this->buffer->remove($position + 2), "\r\n");

                if ($position = strpos($length, ';')) {
                    $length = substr($length, 0, $position);
                }

                if (!preg_match('/^[a-f0-9]+$/i', $length)) {
                    yield from parent::send('', $timeout, true);
                    throw new MessageException(Response::BAD_REQUEST, 'Invalid chunk length.');
                }

                $this->length = hexdec($length) + 2;

                if (2 === $this->length) { // Termination chunk.
                    $end = true;
                }
            }

            if (2 < $this->length) { // Read chunk.
                $buffer = $this->buffer->remove($this->length - 2);
                $this->length -= strlen($buffer);
                $data .= $buffer;
            }

            if (2 >= $this->length) { // Remove \r\n after chunk.
                $this->length -= strlen($this->buffer->remove($this->length));
            }
        }

        return yield from parent::send($data, $timeout, $end);
    }
783208
public function write($data = null) {
		if (!is_resource($this->_resource)) {
			return false;
		}
		if (!is_object($data)) {
			$data = $this->_instance($this->_classes['request'], (array) $data + $this->_config);
		}
		return fwrite($this->_resource, (string) $data, strlen((string) $data));
	}
930303
def write(data, timeout = @timeout)
      full_size = data.bytesize

      while data.bytesize > 0
        written = socket.write_nonblock(data, exception: false)

        if written == :wait_writable
          socket.wait_writable(timeout) or fail_with_timeout!
        else
          data = data.byteslice(written..-1)
        end
      end

      full_size
    end
928884
public function fwrite(string $data): int
    {
        $len = dechex(strlen($data));
        $written = fwrite($this->peer, "${len}\r\n${data}\r\n");
        if (!$written) {
            $msg = 'Cannot write to socket';
            throw new Exception\RuntimeException($msg);
        }

        return $written;
    }
14561
public function write($data)
    {
        if ($this->isOpen() === false) {
            $msg = "Cannot write in a closed MemoryStream.";
            throw new MemoryStreamException($msg, $this, MemoryStreamException::NOT_OPEN);
        }
        
        if ($this->length - 1 === $this->position) {
            // simply append.
            $this->binary .= $data;
        } elseif ($this->position === 0) {
            // simply prepend.
            $this->binary = ($data . $this->binary);
        } else {
            // we are in the middle of the string.
            $part1 = substr($this->binary, 0, $this->position);
            $part2 = substr($this->binary, $this->position);
            $this->binary = ($part1 . $data . $part2);
        }

        $dataLen = strlen($data);
        $this->incrementPosition($dataLen);
        $this->incrementLength($dataLen);

        return $dataLen;
    }
421340
public function bufferWrite(string $data): Promise
    {
        if ($this->append_after) {
            $this->append_after -= strlen($data);
            if ($this->append_after === 0) {
                $data .= $this->append;
                $this->append = '';
            } elseif ($this->append_after < 0) {
                $this->append_after = 0;
                $this->append = '';

                throw new Exception('Tried to send too much out of frame data, cannot append');
            }
        }

        return $this->write($data);
    }
532611
protected function doRequest($data)
    {
        $this->send(self::MAGIC_HEADER.pack('N', strlen($data)));
        $this->send($data);

        $data = $this->receive();

        $this->checkReceivedDataMagic($data);

        $status = unpack('Nstatus', substr($data, 4, 4))['status'];
        $data = substr($data, 12);

        if ($status != 200) {
            throw new TransportException($data ?: 'Http request failed, status: '.$status,
                TransportException::REMOTE);
        }

        return $data;
    }
773966
public function write($action, $data)
    {
        $message = http_build_query($data);

        $resource = $this->getResource();
        $length = strlen($message);
        $sent = 0;

        $content = $this->getHeader($action, $length) . $message;

        while ($this->isConnected() && $sent < $length) {
            $chunk = fwrite($resource, substr($content, $sent));

            if (false === $chunk) {
                throw new \RuntimeException('Could not write to socket');
            }

            $sent += $chunk;
        }
    }
736403
public function stream_write($data)
    {
        if (self::READONLY === $this->mode) {
            return 0;
        }

        if ($this->content->isWritable(vfsStream::getCurrentUser(), vfsStream::getCurrentGroup()) === false) {
            return 0;
        }

        return $this->content->write($data);
    }
805681
public function send($data, $newline = true) {
        if (!$this->_sock) { throw new SocketClientException("Connection unexpectedly closed!"); }
        if ($newline) { $data = $data.$this->retcarriage; }
        if (! $wr = fwrite($this->_sock, $data)) {
            throw new SocketClientException('Error while sending data!');
        }
        return $wr;
    }
1205135
public function write($data)
    {

        if (is_resource($data)) {
            return (bool)stream_copy_to_stream($data, $this->resource());
        }

        $isStream = $data instanceof Stream;

        if (!$isStream && Type::isStringable($data)) {
            return (bool)fwrite($this->resource(), $data);
        }

        if (!$isStream) {
            throw new TypeException('Unsupported data type in write(): ' . Type::of($data));
        }

        $dataResource = $data->resource();

        if (is_resource($dataResource)) {
            return $this->write($dataResource);
        }

        $resource = $this->resource();

        $bytes = 0;

        foreach ($data as $chunk) {
            $bytes += fwrite($resource, $chunk);
        }

        return (bool)$bytes;

    }
236247
private function read($bytes)
    {
        $buffer = call_user_func($this->reader, min($bytes, self::READ_BUFFER_LENGTH));
        if ($buffer === false || $buffer === '') {
            throw new ConnectionException('Error while reading buffered string from client');
        }
        return $buffer;
    }
667337
public function data($data)
    {
        if($this->state == Connection::READ_HEADERS) {
            if(!$this->start_time) $this->start_time = microtime(true);
            $this->header_buffer .= $data;

            $end = strpos($this->header_buffer, "\r\n\r\n", 4);
            if($end === false) return;

            // parse HTTP request
            $this->parse_request(substr($this->header_buffer, 0, $end));

            // TODO: check for Transfer-Encoding: chunked

            $this->content_length = (int)$this->get_header('Content-Length');

            $body_start = $end + 4;
            $data = substr($this->header_buffer, $body_start);

            $this->state = Connection::READ_CONTENT;
        }

        if($this->state == Connection::READ_CONTENT) {
            if($this->is_chunked) {
                // TODO: Handle chunked data
            } else {
                fwrite($this->body, $data);
                $this->bytes_read += strlen($data);

                if($this->content_length - $this->bytes_read <= 0) $this->state = Connection::READ_COMPLETE;
            }
        }

        if($this->state == Connection::READ_COMPLETE) fseek($this->body, 0);
    }
1305189
function send($data)
    {
        $length = strlen($data);
        $written = 0;
        $t1 = microtime(true);
        //总超时，for循环中计时
        while ($written < $length)
        {
            $n = socket_send($this->sock, substr($data, $written), $length - $written, null);
            //超过总时间
            if (microtime(true) > $this->timeout_send + $t1)
            {
                return false;
            }
            if ($n === false)
            {
                $errno = socket_last_error($this->sock);
                //判断错误信息，EAGAIN EINTR，重写一次
                if ($errno == 11 or $errno == 4)
                {
                    continue;
                }
                else
                {
                    return false;
                }
            }
            $written += $n;
        }
        return $written;
    }
274841
public function stream_write(string $data): int
    {
        if (self::READONLY === $this->mode) {
            return 0;
        }

        if ($this->content->isWritable(vfsStream::getCurrentUser(), vfsStream::getCurrentGroup()) === false) {
            return 0;
        }

        if (self::$quota->isLimited()) {
            $data = substr($data, 0, self::$quota->spaceLeft(self::$root->sizeSummarized()));
        }

        return $this->content->write($data);
    }
30638
public function write(string $data): int
    {
        $this->lastModified = time();
        return $this->content->write($data);
    }