Skip to content

Commit

Permalink
gbn: modularise
Browse files Browse the repository at this point in the history
  • Loading branch information
ellemouton committed Dec 1, 2023
1 parent c2216e4 commit c4a0876
Show file tree
Hide file tree
Showing 10 changed files with 818 additions and 522 deletions.
10 changes: 0 additions & 10 deletions gbn/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,6 @@ type config struct {
// 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.
Expand Down Expand Up @@ -53,7 +44,6 @@ func newConfig(sendFunc sendBytesFunc, recvFunc recvBytesFunc,

return &config{
n: n,
s: n + 1,
recvFromStream: recvFunc,
sendToStream: sendFunc,
resendTimeout: defaultResendTimeout,
Expand Down
7 changes: 4 additions & 3 deletions gbn/gbn_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
// The resendTimeout parameter defines the duration to wait before resending data
// if the corresponding ACK for the data is not received.
func NewClientConn(ctx context.Context, n uint8, sendFunc sendBytesFunc,
receiveFunc recvBytesFunc, opts ...Option) (*GoBackNConn, error) {
receiveFunc recvBytesFunc, opts ...Option) (GBN, error) {

if n == math.MaxUint8 {
return nil, fmt.Errorf("n must be smaller than %d",
Expand All @@ -28,14 +28,15 @@ func NewClientConn(ctx context.Context, n uint8, sendFunc sendBytesFunc,
o(cfg)
}

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

if err := conn.clientHandshake(); err != nil {
if err := conn.Close(); err != nil {
conn.log.Errorf("error closing gbn ClientConn: %v", err)
}
return nil, err
}

conn.start()

return conn, nil
Expand All @@ -50,7 +51,7 @@ func NewClientConn(ctx context.Context, n uint8, sendFunc sendBytesFunc,
// SYNACK.
// 3b. If the client does not receive SYN from the server within a given
// timeout, then the client restarts the handshake from step 1.
func (g *GoBackNConn) clientHandshake() error {
func (g *gbn) clientHandshake() error {

Check failure on line 54 in gbn/gbn_client.go

View workflow job for this annotation

GitHub Actions / lint code

cognitive complexity 35 of func `(*gbn).clientHandshake` is high (> 30) (gocognit)
// Spin off the recv function in a goroutine so that we can use
// a select to choose to timeout waiting for data from the receive
// stream. This is needed instead of a context timeout because the
Expand Down
Loading

0 comments on commit c4a0876

Please sign in to comment.