Retro68/gcc/libgo/go/net/net.go

698 lines
20 KiB
Go
Raw Normal View History

2012-03-27 23:13:14 +00:00
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
/*
Package net provides a portable interface for network I/O, including
TCP/IP, UDP, domain name resolution, and Unix domain sockets.
Although the package provides access to low-level networking
primitives, most clients will need only the basic interface provided
by the Dial, Listen, and Accept functions and the associated
Conn and Listener interfaces. The crypto/tls package uses
the same interfaces and similar Dial and Listen functions.
The Dial function connects to a server:
conn, err := net.Dial("tcp", "golang.org:80")
2012-03-27 23:13:14 +00:00
if err != nil {
// handle error
}
fmt.Fprintf(conn, "GET / HTTP/1.0\r\n\r\n")
status, err := bufio.NewReader(conn).ReadString('\n')
// ...
The Listen function creates servers:
ln, err := net.Listen("tcp", ":8080")
if err != nil {
// handle error
}
for {
conn, err := ln.Accept()
if err != nil {
// handle error
}
go handleConnection(conn)
}
2017-04-10 11:32:00 +00:00
Name Resolution
The method for resolving domain names, whether indirectly with functions like Dial
or directly with functions like LookupHost and LookupAddr, varies by operating system.
On Unix systems, the resolver has two options for resolving names.
It can use a pure Go resolver that sends DNS requests directly to the servers
listed in /etc/resolv.conf, or it can use a cgo-based resolver that calls C
library routines such as getaddrinfo and getnameinfo.
By default the pure Go resolver is used, because a blocked DNS request consumes
only a goroutine, while a blocked C call consumes an operating system thread.
When cgo is available, the cgo-based resolver is used instead under a variety of
conditions: on systems that do not let programs make direct DNS requests (OS X),
when the LOCALDOMAIN environment variable is present (even if empty),
when the RES_OPTIONS or HOSTALIASES environment variable is non-empty,
when the ASR_CONFIG environment variable is non-empty (OpenBSD only),
when /etc/resolv.conf or /etc/nsswitch.conf specify the use of features that the
Go resolver does not implement, and when the name being looked up ends in .local
or is an mDNS name.
The resolver decision can be overridden by setting the netdns value of the
GODEBUG environment variable (see package runtime) to go or cgo, as in:
export GODEBUG=netdns=go # force pure Go resolver
export GODEBUG=netdns=cgo # force cgo resolver
The decision can also be forced while building the Go source tree
by setting the netgo or netcgo build tag.
A numeric netdns setting, as in GODEBUG=netdns=1, causes the resolver
to print debugging information about its decisions.
To force a particular resolver while also printing debugging information,
join the two settings by a plus sign, as in GODEBUG=netdns=go+1.
On Plan 9, the resolver always accesses /net/cs and /net/dns.
On Windows, the resolver always uses C library functions, such as GetAddrInfo and DnsQuery.
2012-03-27 23:13:14 +00:00
*/
package net
import (
"context"
2012-03-27 23:13:14 +00:00
"errors"
2018-12-28 15:30:48 +00:00
"internal/poll"
2014-09-21 17:33:12 +00:00
"io"
"os"
2019-06-02 15:48:37 +00:00
"sync"
2014-09-21 17:33:12 +00:00
"syscall"
2012-03-27 23:13:14 +00:00
"time"
)
2017-04-10 11:32:00 +00:00
// netGo and netCgo contain the state of the build tags used
// to build this binary, and whether cgo is available.
// conf.go mirrors these into conf for easier testing.
var (
netGo bool // set true in cgo_stub.go for build tag "netgo" (or no cgo)
netCgo bool // set true in conf_netcgo.go for build tag "netcgo"
)
2012-03-27 23:13:14 +00:00
// Addr represents a network end point address.
//
// The two methods Network and String conventionally return strings
// that can be passed as the arguments to Dial, but the exact form
// and meaning of the strings is up to the implementation.
2012-03-27 23:13:14 +00:00
type Addr interface {
Network() string // name of the network (for example, "tcp", "udp")
String() string // string form of address (for example, "192.0.2.1:25", "[2001:db8::1]:80")
2012-03-27 23:13:14 +00:00
}
// Conn is a generic stream-oriented network connection.
2014-09-21 17:33:12 +00:00
//
// Multiple goroutines may invoke methods on a Conn simultaneously.
2012-03-27 23:13:14 +00:00
type Conn interface {
// Read reads data from the connection.
// Read can be made to time out and return an Error with Timeout() == true
2012-03-27 23:13:14 +00:00
// after a fixed time limit; see SetDeadline and SetReadDeadline.
Read(b []byte) (n int, err error)
// Write writes data to the connection.
// Write can be made to time out and return an Error with Timeout() == true
2012-03-27 23:13:14 +00:00
// after a fixed time limit; see SetDeadline and SetWriteDeadline.
Write(b []byte) (n int, err error)
// Close closes the connection.
2014-09-21 17:33:12 +00:00
// Any blocked Read or Write operations will be unblocked and return errors.
2012-03-27 23:13:14 +00:00
Close() error
// LocalAddr returns the local network address.
LocalAddr() Addr
// RemoteAddr returns the remote network address.
RemoteAddr() Addr
// SetDeadline sets the read and write deadlines associated
// with the connection. It is equivalent to calling both
// SetReadDeadline and SetWriteDeadline.
//
// A deadline is an absolute time after which I/O operations
// fail with a timeout (see type Error) instead of
// blocking. The deadline applies to all future and pending
// I/O, not just the immediately following call to Read or
// Write. After a deadline has been exceeded, the connection
// can be refreshed by setting a deadline in the future.
2012-03-27 23:13:14 +00:00
//
// An idle timeout can be implemented by repeatedly extending
// the deadline after successful Read or Write calls.
//
// A zero value for t means I/O operations will not time out.
SetDeadline(t time.Time) error
// SetReadDeadline sets the deadline for future Read calls
// and any currently-blocked Read call.
2012-03-27 23:13:14 +00:00
// A zero value for t means Read will not time out.
SetReadDeadline(t time.Time) error
// SetWriteDeadline sets the deadline for future Write calls
// and any currently-blocked Write call.
2012-03-27 23:13:14 +00:00
// Even if write times out, it may return n > 0, indicating that
// some of the data was successfully written.
// A zero value for t means Write will not time out.
SetWriteDeadline(t time.Time) error
}
2014-09-21 17:33:12 +00:00
type conn struct {
fd *netFD
}
func (c *conn) ok() bool { return c != nil && c.fd != nil }
// Implementation of the Conn interface.
// Read implements the Conn Read method.
func (c *conn) Read(b []byte) (int, error) {
if !c.ok() {
return 0, syscall.EINVAL
}
2017-04-10 11:32:00 +00:00
n, err := c.fd.Read(b)
if err != nil && err != io.EOF {
err = &OpError{Op: "read", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
}
return n, err
2014-09-21 17:33:12 +00:00
}
// Write implements the Conn Write method.
func (c *conn) Write(b []byte) (int, error) {
if !c.ok() {
return 0, syscall.EINVAL
}
2017-04-10 11:32:00 +00:00
n, err := c.fd.Write(b)
if err != nil {
err = &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
}
return n, err
2014-09-21 17:33:12 +00:00
}
// Close closes the connection.
func (c *conn) Close() error {
if !c.ok() {
return syscall.EINVAL
}
2017-04-10 11:32:00 +00:00
err := c.fd.Close()
if err != nil {
err = &OpError{Op: "close", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
}
return err
2014-09-21 17:33:12 +00:00
}
// LocalAddr returns the local network address.
2017-04-10 11:32:00 +00:00
// The Addr returned is shared by all invocations of LocalAddr, so
// do not modify it.
2014-09-21 17:33:12 +00:00
func (c *conn) LocalAddr() Addr {
if !c.ok() {
return nil
}
return c.fd.laddr
}
// RemoteAddr returns the remote network address.
2017-04-10 11:32:00 +00:00
// The Addr returned is shared by all invocations of RemoteAddr, so
// do not modify it.
2014-09-21 17:33:12 +00:00
func (c *conn) RemoteAddr() Addr {
if !c.ok() {
return nil
}
return c.fd.raddr
}
// SetDeadline implements the Conn SetDeadline method.
func (c *conn) SetDeadline(t time.Time) error {
if !c.ok() {
return syscall.EINVAL
}
2019-06-02 15:48:37 +00:00
if err := c.fd.SetDeadline(t); err != nil {
2017-04-10 11:32:00 +00:00
return &OpError{Op: "set", Net: c.fd.net, Source: nil, Addr: c.fd.laddr, Err: err}
}
return nil
2014-09-21 17:33:12 +00:00
}
// SetReadDeadline implements the Conn SetReadDeadline method.
func (c *conn) SetReadDeadline(t time.Time) error {
if !c.ok() {
return syscall.EINVAL
}
2019-06-02 15:48:37 +00:00
if err := c.fd.SetReadDeadline(t); err != nil {
2017-04-10 11:32:00 +00:00
return &OpError{Op: "set", Net: c.fd.net, Source: nil, Addr: c.fd.laddr, Err: err}
}
return nil
2014-09-21 17:33:12 +00:00
}
// SetWriteDeadline implements the Conn SetWriteDeadline method.
func (c *conn) SetWriteDeadline(t time.Time) error {
if !c.ok() {
return syscall.EINVAL
}
2019-06-02 15:48:37 +00:00
if err := c.fd.SetWriteDeadline(t); err != nil {
2017-04-10 11:32:00 +00:00
return &OpError{Op: "set", Net: c.fd.net, Source: nil, Addr: c.fd.laddr, Err: err}
}
return nil
2014-09-21 17:33:12 +00:00
}
// SetReadBuffer sets the size of the operating system's
// receive buffer associated with the connection.
func (c *conn) SetReadBuffer(bytes int) error {
if !c.ok() {
return syscall.EINVAL
}
2017-04-10 11:32:00 +00:00
if err := setReadBuffer(c.fd, bytes); err != nil {
return &OpError{Op: "set", Net: c.fd.net, Source: nil, Addr: c.fd.laddr, Err: err}
}
return nil
2014-09-21 17:33:12 +00:00
}
// SetWriteBuffer sets the size of the operating system's
// transmit buffer associated with the connection.
func (c *conn) SetWriteBuffer(bytes int) error {
if !c.ok() {
return syscall.EINVAL
}
2017-04-10 11:32:00 +00:00
if err := setWriteBuffer(c.fd, bytes); err != nil {
return &OpError{Op: "set", Net: c.fd.net, Source: nil, Addr: c.fd.laddr, Err: err}
}
return nil
2014-09-21 17:33:12 +00:00
}
2019-06-02 15:48:37 +00:00
// File returns a copy of the underlying os.File
2014-09-21 17:33:12 +00:00
// It is the caller's responsibility to close f when finished.
// Closing c does not affect f, and closing f does not affect c.
//
// The returned os.File's file descriptor is different from the connection's.
// Attempting to change properties of the original using this duplicate
// may or may not have the desired effect.
2017-04-10 11:32:00 +00:00
func (c *conn) File() (f *os.File, err error) {
f, err = c.fd.dup()
if err != nil {
err = &OpError{Op: "file", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
}
return
2012-03-27 23:13:14 +00:00
}
// PacketConn is a generic packet-oriented network connection.
2014-09-21 17:33:12 +00:00
//
// Multiple goroutines may invoke methods on a PacketConn simultaneously.
2012-03-27 23:13:14 +00:00
type PacketConn interface {
// ReadFrom reads a packet from the connection,
2019-06-02 15:48:37 +00:00
// copying the payload into p. It returns the number of
// bytes copied into p and the return address that
2012-03-27 23:13:14 +00:00
// was on the packet.
2019-06-02 15:48:37 +00:00
// It returns the number of bytes read (0 <= n <= len(p))
// and any error encountered. Callers should always process
// the n > 0 bytes returned before considering the error err.
2012-03-27 23:13:14 +00:00
// ReadFrom can be made to time out and return
// an Error with Timeout() == true after a fixed time limit;
2012-03-27 23:13:14 +00:00
// see SetDeadline and SetReadDeadline.
2019-06-02 15:48:37 +00:00
ReadFrom(p []byte) (n int, addr Addr, err error)
2012-03-27 23:13:14 +00:00
2019-06-02 15:48:37 +00:00
// WriteTo writes a packet with payload p to addr.
2012-03-27 23:13:14 +00:00
// WriteTo can be made to time out and return
// an Error with Timeout() == true after a fixed time limit;
2012-03-27 23:13:14 +00:00
// see SetDeadline and SetWriteDeadline.
// On packet-oriented connections, write timeouts are rare.
2019-06-02 15:48:37 +00:00
WriteTo(p []byte, addr Addr) (n int, err error)
2012-03-27 23:13:14 +00:00
// Close closes the connection.
2014-09-21 17:33:12 +00:00
// Any blocked ReadFrom or WriteTo operations will be unblocked and return errors.
2012-03-27 23:13:14 +00:00
Close() error
// LocalAddr returns the local network address.
LocalAddr() Addr
// SetDeadline sets the read and write deadlines associated
// with the connection. It is equivalent to calling both
// SetReadDeadline and SetWriteDeadline.
//
// A deadline is an absolute time after which I/O operations
// fail with a timeout (see type Error) instead of
// blocking. The deadline applies to all future and pending
// I/O, not just the immediately following call to ReadFrom or
// WriteTo. After a deadline has been exceeded, the connection
// can be refreshed by setting a deadline in the future.
//
// An idle timeout can be implemented by repeatedly extending
// the deadline after successful ReadFrom or WriteTo calls.
//
// A zero value for t means I/O operations will not time out.
2012-03-27 23:13:14 +00:00
SetDeadline(t time.Time) error
// SetReadDeadline sets the deadline for future ReadFrom calls
// and any currently-blocked ReadFrom call.
// A zero value for t means ReadFrom will not time out.
2012-03-27 23:13:14 +00:00
SetReadDeadline(t time.Time) error
// SetWriteDeadline sets the deadline for future WriteTo calls
// and any currently-blocked WriteTo call.
2012-03-27 23:13:14 +00:00
// Even if write times out, it may return n > 0, indicating that
// some of the data was successfully written.
// A zero value for t means WriteTo will not time out.
2012-03-27 23:13:14 +00:00
SetWriteDeadline(t time.Time) error
}
2019-06-02 15:48:37 +00:00
var listenerBacklogCache struct {
sync.Once
val int
}
// listenerBacklog is a caching wrapper around maxListenerBacklog.
func listenerBacklog() int {
listenerBacklogCache.Do(func() { listenerBacklogCache.val = maxListenerBacklog() })
return listenerBacklogCache.val
}
2014-09-21 17:33:12 +00:00
2012-03-27 23:13:14 +00:00
// A Listener is a generic network listener for stream-oriented protocols.
2014-09-21 17:33:12 +00:00
//
// Multiple goroutines may invoke methods on a Listener simultaneously.
2012-03-27 23:13:14 +00:00
type Listener interface {
// Accept waits for and returns the next connection to the listener.
2017-04-10 11:32:00 +00:00
Accept() (Conn, error)
2012-03-27 23:13:14 +00:00
// Close closes the listener.
2014-09-21 17:33:12 +00:00
// Any blocked Accept operations will be unblocked and return errors.
2012-03-27 23:13:14 +00:00
Close() error
// Addr returns the listener's network address.
Addr() Addr
}
2017-04-10 11:32:00 +00:00
// An Error represents a network error.
type Error interface {
error
Timeout() bool // Is the error a timeout?
Temporary() bool // Is the error temporary?
}
2015-08-28 15:33:40 +00:00
// Various errors contained in OpError.
var (
// For connection setup operations.
errNoSuitableAddress = errors.New("no suitable address found")
2015-08-28 15:33:40 +00:00
// For connection setup and write operations.
errMissingAddress = errors.New("missing address")
// For both read and write operations.
2018-12-28 15:30:48 +00:00
errCanceled = errors.New("operation was canceled")
ErrWriteToConnected = errors.New("use of WriteTo with pre-connected connection")
2015-08-28 15:33:40 +00:00
)
2012-03-27 23:13:14 +00:00
// mapErr maps from the context errors to the historical internal net
// error values.
//
// TODO(bradfitz): get rid of this after adjusting tests and making
// context.DeadlineExceeded implement net.Error?
func mapErr(err error) error {
switch err {
case context.Canceled:
return errCanceled
case context.DeadlineExceeded:
2018-12-28 15:30:48 +00:00
return poll.ErrTimeout
default:
return err
}
}
2014-09-21 17:33:12 +00:00
// OpError is the error type usually returned by functions in the net
// package. It describes the operation, network type, and address of
// an error.
2012-03-27 23:13:14 +00:00
type OpError struct {
2014-09-21 17:33:12 +00:00
// Op is the operation which caused the error, such as
// "read" or "write".
Op string
// Net is the network type on which this error occurred,
// such as "tcp" or "udp6".
Net string
2017-04-10 11:32:00 +00:00
// For operations involving a remote network connection, like
// Dial, Read, or Write, Source is the corresponding local
// network address.
Source Addr
// Addr is the network address for which this error occurred.
// For local operations, like Listen or SetDeadline, Addr is
// the address of the local endpoint being manipulated.
// For operations involving a remote network connection, like
// Dial, Read, or Write, Addr is the remote address of that
// connection.
2012-03-27 23:13:14 +00:00
Addr Addr
2014-09-21 17:33:12 +00:00
// Err is the error that occurred during the operation.
Err error
2012-03-27 23:13:14 +00:00
}
func (e *OpError) Error() string {
if e == nil {
return "<nil>"
}
s := e.Op
if e.Net != "" {
s += " " + e.Net
}
2017-04-10 11:32:00 +00:00
if e.Source != nil {
s += " " + e.Source.String()
}
2012-03-27 23:13:14 +00:00
if e.Addr != nil {
2017-04-10 11:32:00 +00:00
if e.Source != nil {
s += "->"
} else {
s += " "
}
s += e.Addr.String()
2012-03-27 23:13:14 +00:00
}
s += ": " + e.Err.Error()
return s
}
2017-04-10 11:32:00 +00:00
var (
// aLongTimeAgo is a non-zero time, far in the past, used for
// immediate cancelation of dials.
aLongTimeAgo = time.Unix(1, 0)
2017-04-10 11:32:00 +00:00
// nonDeadline and noCancel are just zero values for
// readability with functions taking too many parameters.
noDeadline = time.Time{}
noCancel = (chan struct{})(nil)
)
2014-09-21 17:33:12 +00:00
2012-03-27 23:13:14 +00:00
type timeout interface {
Timeout() bool
}
func (e *OpError) Timeout() bool {
2017-04-10 11:32:00 +00:00
if ne, ok := e.Err.(*os.SyscallError); ok {
t, ok := ne.Err.(timeout)
return ok && t.Timeout()
}
2012-03-27 23:13:14 +00:00
t, ok := e.Err.(timeout)
return ok && t.Timeout()
}
2017-04-10 11:32:00 +00:00
type temporary interface {
Temporary() bool
}
func (e *OpError) Temporary() bool {
2019-06-02 15:48:37 +00:00
// Treat ECONNRESET and ECONNABORTED as temporary errors when
// they come from calling accept. See issue 6163.
if e.Op == "accept" && isConnError(e.Err) {
return true
}
2017-04-10 11:32:00 +00:00
if ne, ok := e.Err.(*os.SyscallError); ok {
t, ok := ne.Err.(temporary)
return ok && t.Temporary()
}
t, ok := e.Err.(temporary)
return ok && t.Temporary()
}
// A ParseError is the error type of literal network address parsers.
type ParseError struct {
// Type is the type of string that was expected, such as
// "IP address", "CIDR address".
Type string
// Text is the malformed text string.
Text string
}
func (e *ParseError) Error() string { return "invalid " + e.Type + ": " + e.Text }
2012-03-27 23:13:14 +00:00
type AddrError struct {
Err string
Addr string
}
func (e *AddrError) Error() string {
if e == nil {
return "<nil>"
}
s := e.Err
if e.Addr != "" {
s = "address " + e.Addr + ": " + s
2012-03-27 23:13:14 +00:00
}
return s
}
2017-04-10 11:32:00 +00:00
func (e *AddrError) Timeout() bool { return false }
func (e *AddrError) Temporary() bool { return false }
2012-03-27 23:13:14 +00:00
type UnknownNetworkError string
func (e UnknownNetworkError) Error() string { return "unknown network " + string(e) }
func (e UnknownNetworkError) Timeout() bool { return false }
2017-04-10 11:32:00 +00:00
func (e UnknownNetworkError) Temporary() bool { return false }
2012-03-27 23:13:14 +00:00
2014-09-21 17:33:12 +00:00
type InvalidAddrError string
func (e InvalidAddrError) Error() string { return string(e) }
func (e InvalidAddrError) Timeout() bool { return false }
func (e InvalidAddrError) Temporary() bool { return false }
2012-03-27 23:13:14 +00:00
// DNSConfigError represents an error reading the machine's DNS configuration.
2017-04-10 11:32:00 +00:00
// (No longer used; kept for compatibility.)
2012-03-27 23:13:14 +00:00
type DNSConfigError struct {
Err error
}
2017-04-10 11:32:00 +00:00
func (e *DNSConfigError) Error() string { return "error reading DNS config: " + e.Err.Error() }
2012-03-27 23:13:14 +00:00
func (e *DNSConfigError) Timeout() bool { return false }
func (e *DNSConfigError) Temporary() bool { return false }
2014-09-21 17:33:12 +00:00
2017-04-10 11:32:00 +00:00
// Various errors contained in DNSError.
var (
errNoSuchHost = errors.New("no such host")
)
// DNSError represents a DNS lookup error.
type DNSError struct {
Err string // description of the error
Name string // name looked for
Server string // server used
IsTimeout bool // if true, timed out; not all timeouts set this
IsTemporary bool // if true, error is temporary; not all errors set this
}
func (e *DNSError) Error() string {
if e == nil {
return "<nil>"
}
s := "lookup " + e.Name
if e.Server != "" {
s += " on " + e.Server
}
s += ": " + e.Err
return s
}
// Timeout reports whether the DNS lookup is known to have timed out.
// This is not always known; a DNS lookup may fail due to a timeout
// and return a DNSError for which Timeout returns false.
func (e *DNSError) Timeout() bool { return e.IsTimeout }
// Temporary reports whether the DNS error is known to be temporary.
// This is not always known; a DNS lookup may fail due to a temporary
// error and return a DNSError for which Temporary returns false.
func (e *DNSError) Temporary() bool { return e.IsTimeout || e.IsTemporary }
2014-09-21 17:33:12 +00:00
type writerOnly struct {
io.Writer
}
// Fallback implementation of io.ReaderFrom's ReadFrom, when sendfile isn't
// applicable.
func genericReadFrom(w io.Writer, r io.Reader) (n int64, err error) {
// Use wrapper to hide existing r.ReadFrom from io.Copy.
return io.Copy(writerOnly{w}, r)
}
// Limit the number of concurrent cgo-using goroutines, because
// each will block an entire operating system thread. The usual culprit
// is resolving many DNS names in separate goroutines but the DNS
// server is not responding. Then the many lookups each use a different
// thread, and the system or the program runs out of threads.
2019-06-02 15:48:37 +00:00
var threadLimit chan struct{}
var threadOnce sync.Once
2014-09-21 17:33:12 +00:00
func acquireThread() {
2019-06-02 15:48:37 +00:00
threadOnce.Do(func() {
threadLimit = make(chan struct{}, concurrentThreadsLimit())
})
2014-09-21 17:33:12 +00:00
threadLimit <- struct{}{}
}
func releaseThread() {
<-threadLimit
}
// buffersWriter is the interface implemented by Conns that support a
// "writev"-like batch write optimization.
// writeBuffers should fully consume and write all chunks from the
// provided Buffers, else it should report a non-nil error.
type buffersWriter interface {
writeBuffers(*Buffers) (int64, error)
}
// Buffers contains zero or more runs of bytes to write.
//
// On certain machines, for certain types of connections, this is
// optimized into an OS-specific batch write operation (such as
// "writev").
type Buffers [][]byte
var (
_ io.WriterTo = (*Buffers)(nil)
_ io.Reader = (*Buffers)(nil)
)
func (v *Buffers) WriteTo(w io.Writer) (n int64, err error) {
if wv, ok := w.(buffersWriter); ok {
return wv.writeBuffers(v)
}
for _, b := range *v {
nb, err := w.Write(b)
n += int64(nb)
if err != nil {
v.consume(n)
return n, err
}
}
v.consume(n)
return n, nil
}
func (v *Buffers) Read(p []byte) (n int, err error) {
for len(p) > 0 && len(*v) > 0 {
n0 := copy(p, (*v)[0])
v.consume(int64(n0))
p = p[n0:]
n += n0
}
if len(*v) == 0 {
err = io.EOF
}
return
}
func (v *Buffers) consume(n int64) {
for len(*v) > 0 {
ln0 := int64(len((*v)[0]))
if ln0 > n {
(*v)[0] = (*v)[0][n:]
return
}
n -= ln0
*v = (*v)[1:]
}
}