forked from tolsen/mongonet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wire_command_reply.go
74 lines (58 loc) · 1.47 KB
/
wire_command_reply.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
package mongonet
func (m *CommandReplyMessage) HasResponse() bool {
return false // because its a response
}
func (m *CommandReplyMessage) IsExhaust() bool {
return false
}
func (m *CommandReplyMessage) Header() MessageHeader {
return m.header
}
func (m *CommandReplyMessage) Serialize() []byte {
size := 16 /* header */
size += int(m.CommandReply.Size)
size += int(m.Metadata.Size)
for _, d := range m.OutputDocs {
size += int(d.Size)
}
m.header.Size = int32(size)
buf := make([]byte, size)
m.header.WriteInto(buf)
loc := 16
m.CommandReply.Copy(&loc, buf)
m.Metadata.Copy(&loc, buf)
for _, d := range m.OutputDocs {
d.Copy(&loc, buf)
}
return buf
}
func parseCommandReplyMessage(header MessageHeader, buf []byte) (Message, error) {
rm := &CommandReplyMessage{}
rm.header = header
var err error
rm.CommandReply, err = parseSimpleBSON(buf)
if err != nil {
return rm, err
}
if len(buf) < int(rm.CommandReply.Size) {
return rm, NewStackErrorf("invalid command message -- message length is too short.")
}
buf = buf[rm.CommandReply.Size:]
rm.Metadata, err = parseSimpleBSON(buf)
if err != nil {
return rm, err
}
if len(buf) < int(rm.Metadata.Size) {
return rm, NewStackErrorf("invalid command message -- message length is too short.")
}
buf = buf[rm.Metadata.Size:]
for len(buf) > 0 {
doc, err := parseSimpleBSON(buf)
if err != nil {
return rm, err
}
buf = buf[doc.Size:]
rm.OutputDocs = append(rm.OutputDocs, doc)
}
return rm, nil
}