-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmsg_create_stream.go
More file actions
92 lines (74 loc) · 2.66 KB
/
msg_create_stream.go
File metadata and controls
92 lines (74 loc) · 2.66 KB
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
package wire
import (
"fmt"
"io"
)
// MsgCreateStream implements the Message interface and represents a bitcoin
// createstream message. It is sent as the first message on a new TCP connection
// to associate it with an existing peer connection as an additional stream.
type MsgCreateStream struct {
AssociationID []byte
StreamType StreamType
StreamPolicyName string
}
// Bsvdecode decodes r using the bitcoin protocol encoding into the receiver.
// This is part of the Message interface implementation.
func (msg *MsgCreateStream) Bsvdecode(r io.Reader, pver uint32, _ MessageEncoding) error {
var err error
msg.AssociationID, err = ReadVarBytes(r, pver, MaxAssociationIDLen, "AssociationID")
if err != nil {
return err
}
if len(msg.AssociationID) == 0 {
return messageError("MsgCreateStream.Bsvdecode", "association ID must not be empty")
}
var streamType uint8
if err = readElement(r, &streamType); err != nil {
return err
}
msg.StreamType = StreamType(streamType)
msg.StreamPolicyName, err = ReadVarString(r, pver)
if err != nil {
return err
}
return nil
}
// BsvEncode encodes the receiver to w using the bitcoin protocol encoding.
// This is part of the Message interface implementation.
func (msg *MsgCreateStream) BsvEncode(w io.Writer, pver uint32, _ MessageEncoding) error {
if len(msg.AssociationID) == 0 {
return messageError("MsgCreateStream.BsvEncode", "association ID must not be empty")
}
if len(msg.AssociationID) > MaxAssociationIDLen {
str := fmt.Sprintf("association ID too long [len %v, max %v]",
len(msg.AssociationID), MaxAssociationIDLen)
return messageError("MsgCreateStream.BsvEncode", str)
}
if err := WriteVarBytes(w, pver, msg.AssociationID); err != nil {
return err
}
if err := writeElement(w, uint8(msg.StreamType)); err != nil {
return err
}
if err := WriteVarString(w, pver, msg.StreamPolicyName); err != nil {
return err
}
return nil
}
// Command returns the protocol command string for the message.
func (msg *MsgCreateStream) Command() string {
return CmdCreateStream
}
// MaxPayloadLength returns the maximum length the payload can be for the receiver.
func (msg *MsgCreateStream) MaxPayloadLength(_ uint32) uint64 {
// varint(association_id_len) + association_id + stream_type(1) + varint(policy_len) + policy_string
return MaxVarIntPayload + MaxAssociationIDLen + 1 + MaxVarIntPayload + MaxUserAgentLen
}
// NewMsgCreateStream returns a new createstream message.
func NewMsgCreateStream(associationID []byte, streamType StreamType, policyName string) *MsgCreateStream {
return &MsgCreateStream{
AssociationID: associationID,
StreamType: streamType,
StreamPolicyName: policyName,
}
}