Skip to content

Commit

Permalink
gbn: split out config values into a config struct
Browse files Browse the repository at this point in the history
  • Loading branch information
ellemouton committed Nov 20, 2023
1 parent 6776e47 commit 1f2aec4
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 106 deletions.
62 changes: 62 additions & 0 deletions gbn/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package gbn

import "time"

// config holds the configuration values for an instance of GoBackNConn.
type config struct {
// n is the window size. The sender can send a maximum of n packets
// before requiring an ack from the receiver for the first packet in
// the window. The value of n is chosen by the client during the
// GoBN handshake.
n uint8

// s is the maximum sequence number used to label packets. Packets
// are labelled with incrementing sequence numbers modulo s.
// s must be strictly larger than the window size, n. This
// is so that the receiver can tell if the sender is resending the
// previous window (maybe the sender did not receive the acks) or if
// they are sending the next window. If s <= n then there would be
// no way to tell.
s uint8

// maxChunkSize is the maximum payload size in bytes allowed per
// message. If the payload to be sent is larger than maxChunkSize then
// the payload will be split between multiple packets.
// If maxChunkSize is zero then it is disabled and data won't be split
// between packets.
maxChunkSize int

// resendTimeout is the duration that will be waited before resending
// the packets in the current queue.
resendTimeout time.Duration

// recvFromStream is the function that will be used to acquire the next
// available packet.
recvFromStream recvBytesFunc

// sendToStream is the function that will be used to send over our next
// packet.
sendToStream sendBytesFunc

// handshakeTimeout is the time after which the server or client
// will abort and restart the handshake if the expected response is
// not received from the peer.
handshakeTimeout time.Duration

pingTime time.Duration
pongTime time.Duration
}

// newConfig constructs a new config struct.
func newConfig(sendFunc sendBytesFunc, recvFunc recvBytesFunc,
n uint8) *config {

return &config{
n: n,
s: n + 1,
recvFromStream: recvFunc,
sendToStream: sendFunc,
resendTimeout: defaultResendTimeout,
handshakeTimeout: defaultHandshakeTimeout,
}
}
18 changes: 10 additions & 8 deletions gbn/gbn_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ func NewClientConn(ctx context.Context, n uint8, sendFunc sendBytesFunc,
math.MaxUint8)
}

conn := newGoBackNConn(ctx, sendFunc, receiveFunc, false, n)
cfg := newConfig(sendFunc, receiveFunc, n)

// Apply functional options
for _, o := range opts {
o(conn)
o(cfg)
}

conn := newGoBackNConn(ctx, cfg, "client")

if err := conn.clientHandshake(); err != nil {
if err := conn.Close(); err != nil {
log.Errorf("error closing gbn ClientConn: %v", err)
Expand Down Expand Up @@ -76,7 +78,7 @@ func (g *GoBackNConn) clientHandshake() error {
case <-recvNext:
}

b, err := g.recvFromStream(g.ctx)
b, err := g.cfg.recvFromStream(g.ctx)

Check failure on line 81 in gbn/gbn_client.go

View workflow job for this annotation

GitHub Actions / lint code

variable name 'b' is too short for the scope of its usage (varnamelen)
if err != nil {
errChan <- err
return
Expand All @@ -98,15 +100,15 @@ func (g *GoBackNConn) clientHandshake() error {
handshake:
for {
// start Handshake
msg := &PacketSYN{N: g.n}
msg := &PacketSYN{N: g.cfg.n}
msgBytes, err := msg.Serialize()
if err != nil {
return err
}

// Send SYN
g.log.Debugf("Sending SYN")
if err := g.sendToStream(g.ctx, msgBytes); err != nil {
if err := g.cfg.sendToStream(g.ctx, msgBytes); err != nil {
return err
}

Expand All @@ -125,7 +127,7 @@ handshake:

var b []byte
select {
case <-time.After(g.handshakeTimeout):
case <-time.After(g.cfg.handshakeTimeout):
g.log.Debugf("SYN resendTimeout. Resending " +
"SYN.")

Expand Down Expand Up @@ -160,7 +162,7 @@ handshake:

g.log.Debugf("Got SYN")

if resp.(*PacketSYN).N != g.n {
if resp.(*PacketSYN).N != g.cfg.n {

Check failure on line 165 in gbn/gbn_client.go

View workflow job for this annotation

GitHub Actions / lint code

type assertion must be checked (forcetypeassert)
return io.EOF
}

Expand All @@ -171,7 +173,7 @@ handshake:
return err
}

if err := g.sendToStream(g.ctx, synack); err != nil {
if err := g.cfg.sendToStream(g.ctx, synack); err != nil {
return err
}

Expand Down
Loading

0 comments on commit 1f2aec4

Please sign in to comment.