Source file
src/net/sendfile_linux.go
1
2
3
4
5 package net
6
7 import (
8 "internal/poll"
9 "io"
10 "os"
11 )
12
13 const supportsSendfile = true
14
15
16
17
18
19
20
21
22 func sendFile(c *netFD, r io.Reader) (written int64, err error, handled bool) {
23 var remain int64 = 1<<63 - 1
24
25 lr, ok := r.(*io.LimitedReader)
26 if ok {
27 remain, r = lr.N, lr.R
28 if remain <= 0 {
29 return 0, nil, true
30 }
31 }
32 f, ok := r.(*os.File)
33 if !ok {
34 return 0, nil, false
35 }
36
37 sc, err := f.SyscallConn()
38 if err != nil {
39 return 0, nil, false
40 }
41
42 var werr error
43 err = sc.Read(func(fd uintptr) bool {
44 written, werr, handled = poll.SendFile(&c.pfd, int(fd), remain)
45 return true
46 })
47 if err == nil {
48 err = werr
49 }
50
51 if lr != nil {
52 lr.N = remain - written
53 }
54 return written, wrapSyscallError("sendfile", err), handled
55 }
56
View as plain text