forked from 30x/libgozerian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
body.go
59 lines (50 loc) · 943 Bytes
/
body.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"io"
)
/*
#include <stdlib.h>
*/
import "C"
type requestBody struct {
handler commandHandler
started bool
curBuf []byte
}
func (b *requestBody) Read(buf []byte) (int, error) {
if !b.started {
b.handler.StartRead()
// First tell the caller that we need some data.
b.handler.Commands() <- command{id: RBOD}
b.started = true
}
cb := b.curBuf
if cb == nil {
// Will return nil at end of channel.
cb = <-b.handler.Bodies()
}
if cb == nil {
return 0, io.EOF
}
if len(cb) <= len(buf) {
copy(buf, cb)
//copy((*[1<<30]byte)(buf)[:], cb)
b.curBuf = nil
return len(cb), nil
}
copy(buf, cb[:len(buf)])
b.curBuf = cb[len(buf):]
return len(buf), nil
}
func (b *requestBody) Close() error {
if b.started {
// Need to clear the channel.
b.curBuf = nil
drained := <-b.handler.Bodies()
for drained != nil {
drained = <-b.handler.Bodies()
}
b.started = false
}
return nil
}