LEFT | RIGHT |
(no file at all) | |
| 1 // Copyright 2011 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 "io" |
| 9 "os" |
| 10 "syscall" |
| 11 ) |
| 12 |
| 13 type sendfileOp struct { |
| 14 anOp |
| 15 src int32 // source |
| 16 n uint32 |
| 17 } |
| 18 |
| 19 func (o *sendfileOp) Submit() (errno int) { |
| 20 return syscall.TransmitFile(int32(o.fd.sysfd), o.src, o.n, 0, &o.o, nil,
syscall.TF_WRITE_BEHIND) |
| 21 } |
| 22 |
| 23 func (o *sendfileOp) Name() string { |
| 24 return "TransmitFile" |
| 25 } |
| 26 |
| 27 // sendFile copies the contents of r to c using the TransmitFile |
| 28 // system call to minimize copies. |
| 29 // |
| 30 // if handled == true, sendFile returns the number of bytes copied and any |
| 31 // non-EOF error. |
| 32 // |
| 33 // if handled == false, sendFile performed no work. |
| 34 // |
| 35 // Note that sendfile for windows does not suppport >2GB file. |
| 36 func sendFile(c *netFD, r io.Reader) (written int64, err os.Error, handled bool)
{ |
| 37 var n int64 = 0 // by default, copy until EOF |
| 38 |
| 39 lr, ok := r.(*io.LimitedReader) |
| 40 if ok { |
| 41 n, r = lr.N, lr.R |
| 42 if n <= 0 { |
| 43 return 0, nil, true |
| 44 } |
| 45 } |
| 46 f, ok := r.(*os.File) |
| 47 if !ok { |
| 48 return 0, nil, false |
| 49 } |
| 50 |
| 51 c.wio.Lock() |
| 52 defer c.wio.Unlock() |
| 53 c.incref() |
| 54 defer c.decref() |
| 55 |
| 56 var o sendfileOp |
| 57 o.Init(c) |
| 58 o.n = uint32(n) |
| 59 o.src = int32(f.Fd()) |
| 60 done, err := iosrv.ExecIO(&o, 0) |
| 61 if err != nil { |
| 62 return 0, err, false |
| 63 } |
| 64 if lr != nil { |
| 65 lr.N -= int64(done) |
| 66 } |
| 67 return int64(done), nil, true |
| 68 } |
LEFT | RIGHT |