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 22, 2023
1 parent 22ef935 commit ef7b4fc
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 102 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)
if err != nil {
errChan <- err
return
Expand All @@ -101,15 +103,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 @@ -128,7 +130,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 @@ -165,7 +167,7 @@ handshake:

g.log.Debugf("Got SYN")

if respSYN.N != g.n {
if respSYN.N != g.cfg.n {
return io.EOF
}

Expand All @@ -176,7 +178,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 ef7b4fc

Please sign in to comment.