Skip to content

Commit 3f12379

Browse files
committed
gbn: split out config values into a config struct
1 parent ad357ad commit 3f12379

File tree

6 files changed

+132
-106
lines changed

6 files changed

+132
-106
lines changed

gbn/config.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package gbn
2+
3+
import "time"
4+
5+
// config holds the configuration values for an instance of GoBackNConn.
6+
type config struct {
7+
// n is the window size. The sender can send a maximum of n packets
8+
// before requiring an ack from the receiver for the first packet in
9+
// the window. The value of n is chosen by the client during the
10+
// GoBN handshake.
11+
n uint8
12+
13+
// s is the maximum sequence number used to label packets. Packets
14+
// are labelled with incrementing sequence numbers modulo s.
15+
// s must be strictly larger than the window size, n. This
16+
// is so that the receiver can tell if the sender is resending the
17+
// previous window (maybe the sender did not receive the acks) or if
18+
// they are sending the next window. If s <= n then there would be
19+
// no way to tell.
20+
s uint8
21+
22+
// maxChunkSize is the maximum payload size in bytes allowed per
23+
// message. If the payload to be sent is larger than maxChunkSize then
24+
// the payload will be split between multiple packets.
25+
// If maxChunkSize is zero then it is disabled and data won't be split
26+
// between packets.
27+
maxChunkSize int
28+
29+
// resendTimeout is the duration that will be waited before resending
30+
// the packets in the current queue.
31+
resendTimeout time.Duration
32+
33+
// recvFromStream is the function that will be used to acquire the next
34+
// available packet.
35+
recvFromStream recvBytesFunc
36+
37+
// sendToStream is the function that will be used to send over our next
38+
// packet.
39+
sendToStream sendBytesFunc
40+
41+
// handshakeTimeout is the time after which the server or client
42+
// will abort and restart the handshake if the expected response is
43+
// not received from the peer.
44+
handshakeTimeout time.Duration
45+
46+
pingTime time.Duration
47+
pongTime time.Duration
48+
}
49+
50+
// newConfig constructs a new config struct.
51+
func newConfig(sendFunc sendBytesFunc, recvFunc recvBytesFunc,
52+
n uint8) *config {
53+
54+
return &config{
55+
n: n,
56+
s: n + 1,
57+
recvFromStream: recvFunc,
58+
sendToStream: sendFunc,
59+
resendTimeout: defaultResendTimeout,
60+
handshakeTimeout: defaultHandshakeTimeout,
61+
}
62+
}

gbn/gbn_client.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ func NewClientConn(ctx context.Context, n uint8, sendFunc sendBytesFunc,
2121
math.MaxUint8)
2222
}
2323

24-
conn := newGoBackNConn(ctx, sendFunc, receiveFunc, false, n)
24+
cfg := newConfig(sendFunc, receiveFunc, n)
2525

2626
// Apply functional options
2727
for _, o := range opts {
28-
o(conn)
28+
o(cfg)
2929
}
3030

31+
conn := newGoBackNConn(ctx, cfg, "client")
32+
3133
if err := conn.clientHandshake(); err != nil {
3234
if err := conn.Close(); err != nil {
3335
log.Errorf("error closing gbn ClientConn: %v", err)
@@ -76,7 +78,7 @@ func (g *GoBackNConn) clientHandshake() error {
7678
case <-recvNext:
7779
}
7880

79-
b, err := g.recvFromStream(g.ctx)
81+
b, err := g.cfg.recvFromStream(g.ctx)
8082
if err != nil {
8183
errChan <- err
8284
return
@@ -98,15 +100,15 @@ func (g *GoBackNConn) clientHandshake() error {
98100
handshake:
99101
for {
100102
// start Handshake
101-
msg := &PacketSYN{N: g.n}
103+
msg := &PacketSYN{N: g.cfg.n}
102104
msgBytes, err := msg.Serialize()
103105
if err != nil {
104106
return err
105107
}
106108

107109
// Send SYN
108110
g.log.Debugf("Sending SYN")
109-
if err := g.sendToStream(g.ctx, msgBytes); err != nil {
111+
if err := g.cfg.sendToStream(g.ctx, msgBytes); err != nil {
110112
return err
111113
}
112114

@@ -125,7 +127,7 @@ handshake:
125127

126128
var b []byte
127129
select {
128-
case <-time.After(g.handshakeTimeout):
130+
case <-time.After(g.cfg.handshakeTimeout):
129131
g.log.Debugf("SYN resendTimeout. Resending " +
130132
"SYN.")
131133

@@ -160,7 +162,7 @@ handshake:
160162

161163
g.log.Debugf("Got SYN")
162164

163-
if resp.(*PacketSYN).N != g.n {
165+
if resp.(*PacketSYN).N != g.cfg.n {
164166
return io.EOF
165167
}
166168

@@ -171,7 +173,7 @@ handshake:
171173
return err
172174
}
173175

174-
if err := g.sendToStream(g.ctx, synack); err != nil {
176+
if err := g.cfg.sendToStream(g.ctx, synack); err != nil {
175177
return err
176178
}
177179

0 commit comments

Comments
 (0)