-
Notifications
You must be signed in to change notification settings - Fork 171
/
pdu.go
159 lines (141 loc) · 3.28 KB
/
pdu.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Package transport provides long-lived http/tcp connections for
// intra-cluster communications (see README for details and usage example).
/*
* Copyright (c) 2018-2023, NVIDIA CORPORATION. All rights reserved.
*/
package transport
import (
"fmt"
"io"
"github.com/NVIDIA/aistore/cmn/cos"
"github.com/NVIDIA/aistore/cmn/debug"
"github.com/NVIDIA/aistore/memsys"
)
type (
pdu struct {
buf []byte
roff int
woff int
done bool
last bool
}
spdu struct {
pdu
}
rpdu struct {
body io.Reader
pdu
flags uint64
plen int
}
)
/////////
// pdu //
/////////
func (pdu *pdu) plength() int { return pdu.woff - sizeProtoHdr } // just the payload
func (pdu *pdu) slength() int { return pdu.roff - sizeProtoHdr } // payload transmitted/received so far
func (pdu *pdu) rlength() int { return pdu.woff - pdu.roff } // not yet sent/received part of the PDU
func (pdu *pdu) read(b []byte) (n int) {
n = copy(b, pdu.buf[pdu.roff:pdu.woff])
pdu.roff += n
return
}
func (pdu *pdu) free(mm *memsys.MMSA) {
if pdu.buf != nil {
mm.Free(pdu.buf)
}
}
//////////
// spdu //
//////////
func newSendPDU(buf []byte) (p *spdu) {
debug.Assert(len(buf) >= cos.KiB && len(buf) <= maxSizePDU)
p = &spdu{pdu{buf: buf}}
p.reset()
return
}
func (pdu *spdu) readFrom(sendoff *sendoff) (err error) {
var (
obj = &sendoff.obj
b = pdu.buf[pdu.woff:]
n int
)
n, err = obj.Reader.Read(b)
pdu.woff += n
pdu.done = pdu.woff == len(pdu.buf)
if err != nil {
pdu.done, pdu.last = true, true
} else if !obj.IsUnsized() && sendoff.off+int64(pdu.plength()) >= obj.Hdr.ObjAttrs.Size {
pdu.done, pdu.last = true, true
}
return
}
func (pdu *spdu) reset() {
pdu.roff, pdu.woff = 0, sizeProtoHdr
pdu.done, pdu.last = false, false
}
//////////
// rpdu //
//////////
func newRecvPDU(body io.Reader, buf []byte) (p *rpdu) {
p = &rpdu{body: body, pdu: pdu{buf: buf}}
p.reset()
return
}
func (pdu *rpdu) readHdr(loghdr string) (err error) {
const fmterr = "sbrk %s: invalid PDU header [plen=%d, flags=%s]"
var n int
debug.Assert(pdu.woff == 0)
n, err = pdu.body.Read(pdu.buf[:sizeProtoHdr])
if n < sizeProtoHdr {
if err == nil {
err = fmt.Errorf("sbrk %s: failed to receive PDU header (n=%d)", loghdr, n)
}
return
}
pdu.plen, pdu.flags, err = extProtoHdr(pdu.buf, loghdr)
if err != nil {
return
}
if pdu.flags&pduFl == 0 || pdu.plen > maxSizePDU || pdu.plen < 0 {
err = fmt.Errorf(fmterr, loghdr, pdu.plen, fl2s(pdu.flags))
debug.AssertNoErr(err)
return
}
pdu.woff = sizeProtoHdr
pdu.last = pdu.flags&pduLastFl != 0
debug.Assertf(pdu.plen > 0 || (pdu.plen == 0 && pdu.last), fmterr, loghdr, pdu.plen, fl2s(pdu.flags))
return
}
func (pdu *rpdu) reset() {
pdu.roff, pdu.woff = sizeProtoHdr, 0
pdu.done, pdu.last = false, false
}
func (pdu *rpdu) readFrom() (n int, err error) {
n, err = pdu.body.Read(pdu.buf[pdu.woff : sizeProtoHdr+pdu.plen]) // NOTE: maxSizePDU
pdu.woff += n
pdu.done = pdu.plength() == pdu.plen
if err != nil {
pdu.done, pdu.last = true, true
}
return
}
//
// misc
//
func fl2s(flags uint64) (s string) {
if flags&msgFl == 0 && flags&pduFl == 0 {
s += "[obj]"
} else if flags&msgFl != 0 {
s += "[msg]"
} else if flags&pduFl != 0 {
s += "[pdu]"
}
if flags&pduStreamFl != 0 {
s += "[pdu-stream]"
}
if flags&pduLastFl != 0 {
s += "[lst]"
}
return
}