Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(66)

Delta Between Two Patch Sets: src/pkg/net/sendfile_linux.go

Issue 4538088: code review 4538088: http,io,net: sendfile support (Closed)
Left Patch Set: diff -r 58102fac10c6 https://go.googlecode.com/hg Created 13 years, 10 months ago
Right Patch Set: diff -r 58102fac10c6 https://go.googlecode.com/hg Created 13 years, 10 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
1 // Copyright 2011 The Go Authors. All rights reserved. 1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style 2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file. 3 // license that can be found in the LICENSE file.
4 4
5 package net 5 package net
6 6
7 import ( 7 import (
8 "fmt" 8 "fmt"
9 "io" 9 "io"
10 "os" 10 "os"
11 "syscall" 11 "syscall"
12 "unsafe" 12 "unsafe"
13 ) 13 )
14 14
15 const maxSendfileSize int64 = 4 << 20 // arbitrary size that fits in an int 15 const maxSendfileSize int64 = 4 << 20 // arbitrary size that fits in an int
16 16
17 // SendFile sends f to c using sendfile(2), bypassing userspace copies, 17 // SendFile copies the contents of f to c, avoiding unnecessary copies
18 // if possible. If n >= 0, it specifies the max number of bytes to copy. 18 // where possible.
19 // If n < 0, it means until EOF. SendFile returns the number of bytes copied 19 //
20 // and any error, if not EOF. 20 // If n >= 0, n specifies the maximum number of bytes to copy.
21 // If n < 0, Sendfile copies until EOF
22
23 // SendFile returns the number of bytes copied and any error, if not
24 // EOF.
21 func (c *TCPConn) SendFile(f *os.File, n int64) (written int64, err os.Error) { 25 func (c *TCPConn) SendFile(f *os.File, n int64) (written int64, err os.Error) {
22 destfd := c.fd.sysfd 26 destfd := c.fd.sysfd
23 srcfd := f.Fd() 27 srcfd := f.Fd()
24 for { 28 for {
25 toCopy := maxSendfileSize 29 toCopy := maxSendfileSize
26 if n >= 0 { 30 if n >= 0 {
27 toCopy = n - written 31 toCopy = n - written
28 if toCopy > maxSendfileSize { 32 if toCopy > maxSendfileSize {
29 toCopy = maxSendfileSize 33 toCopy = maxSendfileSize
30 } 34 }
31 } 35 }
32 if toCopy <= 0 { 36 if toCopy <= 0 {
33 return 37 return
34 } 38 }
35 » » sn, err := syscall.Sendfile(destfd, srcfd, (*int64)(unsafe.Point er(nil)), int(toCopy)) 39 » » sn, errno := syscall.Sendfile(destfd, srcfd, (*int64)(unsafe.Poi nter(nil)), int(toCopy))
36 » » switch err { 40 » » switch errno {
37 case 0: 41 case 0:
38 if n == 0 { 42 if n == 0 {
39 return written, nil 43 return written, nil
40 } 44 }
41 written += int64(sn) 45 written += int64(sn)
42 case syscall.EAGAIN: 46 case syscall.EAGAIN:
43 pollserver.WaitWrite(c.fd) 47 pollserver.WaitWrite(c.fd)
44 default: 48 default:
45 return written, fmt.Errorf("io: sendfile error %d", err) 49 return written, fmt.Errorf("io: sendfile error %d", err)
46 } 50 }
(...skipping 27 matching lines...) Expand all
74 return written, nil 78 return written, nil
75 } 79 }
76 if written != 0 { 80 if written != 0 {
77 return written, err 81 return written, err
78 } 82 }
79 // if sendfile failed, but didn't copy any bytes, fall through.. . 83 // if sendfile failed, but didn't copy any bytes, fall through.. .
80 } 84 }
81 85
82 return io.BufferedCopyn(c, r, n) 86 return io.BufferedCopyn(c, r, n)
83 } 87 }
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b