...
Run Format

Source file src/net/splice_linux.go

Documentation: net

     1  // Copyright 2018 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package net
     6  
     7  import (
     8  	"internal/poll"
     9  	"io"
    10  )
    11  
    12  // splice transfers data from r to c using the splice system call to minimize
    13  // copies from and to userspace. c must be a TCP connection. Currently, splice
    14  // is only enabled if r is also a TCP connection.
    15  //
    16  // If splice returns handled == false, it has performed no work.
    17  func splice(c *netFD, r io.Reader) (written int64, err error, handled bool) {
    18  	var remain int64 = 1 << 62 // by default, copy until EOF
    19  	lr, ok := r.(*io.LimitedReader)
    20  	if ok {
    21  		remain, r = lr.N, lr.R
    22  		if remain <= 0 {
    23  			return 0, nil, true
    24  		}
    25  	}
    26  	s, ok := r.(*TCPConn)
    27  	if !ok {
    28  		return 0, nil, false
    29  	}
    30  	written, handled, sc, err := poll.Splice(&c.pfd, &s.fd.pfd, remain)
    31  	if lr != nil {
    32  		lr.N -= written
    33  	}
    34  	return written, wrapSyscallError(sc, err), handled
    35  }
    36  

View as plain text