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
14
15
16
17
18
19
20 func sendFile(c *netFD, r io.Reader) (written int64, err error, handled bool) {
21 var remain int64 = 1 << 62
22
23 lr, ok := r.(*io.LimitedReader)
24 if ok {
25 remain, r = lr.N, lr.R
26 if remain <= 0 {
27 return 0, nil, true
28 }
29 }
30 f, ok := r.(*os.File)
31 if !ok {
32 return 0, nil, false
33 }
34
35 sc, err := f.SyscallConn()
36 if err != nil {
37 return 0, nil, false
38 }
39
40 var werr error
41 err = sc.Read(func(fd uintptr) bool {
42 written, werr = poll.SendFile(&c.pfd, int(fd), remain)
43 return true
44 })
45 if err == nil {
46 err = werr
47 }
48
49 if lr != nil {
50 lr.N = remain - written
51 }
52 return written, wrapSyscallError("sendfile", err), written > 0
53 }
54
View as plain text