/
send.go
66 lines (55 loc) · 1.62 KB
/
send.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 node
import (
"fmt"
"github.com/ipfs/go-cid"
nodeSym "github.com/taubyte/go-sdk-symbols/p2p/node"
"github.com/taubyte/go-sdk/utils/codec"
)
// Send returns the command response and an error,
// used to send data over p2p on given protocol and command.
func (c Command) Send(body []byte) ([]byte, error) {
var dataPtr *byte
if len(body) != 0 {
dataPtr = &body[0]
}
var responseSize uint32
err := nodeSym.SendCommand(uint32(c), dataPtr, uint32(len(body)), &responseSize)
if err != 0 {
return nil, fmt.Errorf("Send command failed with %s", err)
}
if responseSize == 0 {
return nil, nil
}
response := make([]byte, responseSize)
err = nodeSym.ReadCommandResponse(uint32(c), &response[0], responseSize)
if err != 0 {
return nil, fmt.Errorf("Reading command response failed with %s", err)
}
return response, nil
}
// SendTo returns the command response and an error,
// used to send data over p2p on given protocol and command.
func (c Command) SendTo(body []byte, cid cid.Cid) ([]byte, error) {
var dataPtr *byte
if len(body) != 0 {
dataPtr = &body[0]
}
writer, err := codec.CidWriter(cid)
if err != nil {
return nil, err
}
var responseSize uint32
err0 := nodeSym.SendCommandTo(uint32(c), writer.Ptr(), dataPtr, uint32(len(body)), &responseSize)
if err0 != 0 {
return nil, fmt.Errorf("Send command failed with %s", err0)
}
if responseSize == 0 {
return nil, nil
}
response := make([]byte, responseSize)
err0 = nodeSym.ReadCommandResponse(uint32(c), &response[0], responseSize)
if err0 != 0 {
return nil, fmt.Errorf("Reading command response failed with %s", err0)
}
return response, nil
}