forked from tolsen/mongonet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wire_kill_cursors.go
66 lines (48 loc) · 1.41 KB
/
wire_kill_cursors.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
package mongonet
func (m *KillCursorsMessage) HasResponse() bool {
return false
}
func (m *KillCursorsMessage) IsExhaust() bool {
return false
}
func (m *KillCursorsMessage) Header() MessageHeader {
return m.header
}
func (m *KillCursorsMessage) Serialize() []byte {
size := 16 /* header */ + 8 /* header */ + (8 * int(m.NumCursors))
m.header.Size = int32(size)
buf := make([]byte, size)
m.header.WriteInto(buf)
writeInt32(0, buf, 16)
writeInt32(m.NumCursors, buf, 20)
loc := 24
for _, c := range m.CursorIds {
writeInt64(c, buf, loc)
loc += 8
}
return buf
}
func parseKillCursorsMessage(header MessageHeader, buf []byte) (Message, error) {
m := &KillCursorsMessage{}
m.header = header
loc := 0
if len(buf) < 8 {
return m, NewStackErrorf("invalid kill cursors message -- message must have length of at least 8 bytes.")
}
m.Reserved = readInt32(buf)
loc += 4
m.NumCursors = readInt32(buf[loc:])
loc += 4
if len(buf[loc:]) < int(m.NumCursors)*8 {
return m, NewStackErrorf("invalid kill cursors message -- NumCursors = %v is larger than number of cursors in message", m.NumCursors)
}
if int(m.NumCursors) < 0 || int(m.NumCursors) > MaxInt32 {
return nil, NewStackErrorf("number of cursors has invalid size (%v).", int(m.NumCursors))
}
m.CursorIds = make([]int64, int(m.NumCursors))
for i := 0; i < int(m.NumCursors); i++ {
m.CursorIds[i] = readInt64(buf[loc:])
loc += 8
}
return m, nil
}