-
Notifications
You must be signed in to change notification settings - Fork 19
/
channel_open.go
65 lines (58 loc) · 1.67 KB
/
channel_open.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package messages
import (
"bytes"
"fmt"
"io"
)
type channelOpen struct {
senderChannel uint32
initialWindowSize uint32
maximumPacketSize uint32
}
const (
defaultInitialWindowSize = 1024 * 1024
defaultMaximumPacketSize = 16 * 1024
)
func newChannelOpen(senderChannel uint32, initialWindowSize uint32, maximumPacketSize uint32) *channelOpen {
if initialWindowSize == 0 {
initialWindowSize = defaultInitialWindowSize
}
if maximumPacketSize == 0 {
maximumPacketSize = defaultMaximumPacketSize
}
return &channelOpen{
senderChannel: senderChannel,
initialWindowSize: initialWindowSize,
maximumPacketSize: maximumPacketSize,
}
}
func (c *channelOpen) marshal() ([]byte, error) {
buf := new(bytes.Buffer)
if err := writeUint32(buf, c.senderChannel); err != nil {
return nil, fmt.Errorf("failed to write sender channel: %w", err)
}
if err := writeUint32(buf, c.initialWindowSize); err != nil {
return nil, fmt.Errorf("failed to write initial window size: %w", err)
}
if err := writeUint32(buf, c.maximumPacketSize); err != nil {
return nil, fmt.Errorf("failed to write maximum packet size: %w", err)
}
return buf.Bytes(), nil
}
func (c *channelOpen) unmarshal(buf io.Reader) (err error) {
c.senderChannel, err = readUint32(buf)
if err != nil {
return fmt.Errorf("failed to read sender channel: %w", err)
}
c.initialWindowSize, err = readUint32(buf)
if err != nil {
return fmt.Errorf("failed to read initial window size: %w", err)
}
c.maximumPacketSize, err = readUint32(buf)
if err != nil {
return fmt.Errorf("failed to read maximum packet size: %w", err)
}
return nil
}