Skip to content

Commit 5fa6e72

Browse files
committed
gbn: split out config values into a config struct
1 parent 51877c3 commit 5fa6e72

File tree

5 files changed

+128
-102
lines changed

5 files changed

+128
-102
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
@@ -101,15 +103,15 @@ func (g *GoBackNConn) clientHandshake() error {
101103
handshake:
102104
for {
103105
// start Handshake
104-
msg := &PacketSYN{N: g.n}
106+
msg := &PacketSYN{N: g.cfg.n}
105107
msgBytes, err := msg.Serialize()
106108
if err != nil {
107109
return err
108110
}
109111

110112
// Send SYN
111113
g.log.Debugf("Sending SYN")
112-
if err := g.sendToStream(g.ctx, msgBytes); err != nil {
114+
if err := g.cfg.sendToStream(g.ctx, msgBytes); err != nil {
113115
return err
114116
}
115117

@@ -128,7 +130,7 @@ handshake:
128130

129131
var b []byte
130132
select {
131-
case <-time.After(g.handshakeTimeout):
133+
case <-time.After(g.cfg.handshakeTimeout):
132134
g.log.Debugf("SYN resendTimeout. Resending " +
133135
"SYN.")
134136

@@ -165,7 +167,7 @@ handshake:
165167

166168
g.log.Debugf("Got SYN")
167169

168-
if respSYN.N != g.n {
170+
if respSYN.N != g.cfg.n {
169171
return io.EOF
170172
}
171173

@@ -176,7 +178,7 @@ handshake:
176178
return err
177179
}
178180

179-
if err := g.sendToStream(g.ctx, synack); err != nil {
181+
if err := g.cfg.sendToStream(g.ctx, synack); err != nil {
180182
return err
181183
}
182184

0 commit comments

Comments
 (0)