Skip to content

Commit

Permalink
Make Client always commit params
Browse files Browse the repository at this point in the history
  • Loading branch information
edaniels committed Mar 6, 2024
1 parent 02dc2ec commit f784cc1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
20 changes: 9 additions & 11 deletions datachannel.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ type Config struct {
LoggerFactory logging.LoggerFactory
}

func newDataChannel(stream *sctp.Stream, config *Config) (*DataChannel, error) {
func newDataChannel(stream *sctp.Stream, config *Config) *DataChannel {
return &DataChannel{
Config: *config,
stream: stream,
log: config.LoggerFactory.NewLogger("datachannel"),
}, nil
}
}

// Dial opens a data channels over SCTP
Expand Down Expand Up @@ -118,7 +118,12 @@ func Client(stream *sctp.Stream, config *Config) (*DataChannel, error) {
return nil, fmt.Errorf("failed to send ChannelOpen %w", err)
}
}
return newDataChannel(stream, config)
dc := newDataChannel(stream, config)

if err := dc.commitReliabilityParams(); err != nil {
return nil, err
}
return dc, nil
}

// Accept is used to accept incoming data channels over SCTP
Expand Down Expand Up @@ -167,10 +172,7 @@ func Server(stream *sctp.Stream, config *Config) (*DataChannel, error) {
config.Label = string(openMsg.Label)
config.Protocol = string(openMsg.Protocol)

dataChannel, err := newDataChannel(stream, config)
if err != nil {
return nil, err
}
dataChannel := newDataChannel(stream, config)

err = dataChannel.writeDataChannelAck()
if err != nil {
Expand Down Expand Up @@ -283,10 +285,6 @@ func (c *DataChannel) handleDCEP(data []byte) error {

switch msg := msg.(type) {
case *channelAck:
c.log.Debug("Received DATA_CHANNEL_ACK")
if err = c.commitReliabilityParams(); err != nil {
return err
}
c.onOpenComplete()
default:
return fmt.Errorf("%w %v", ErrInvalidMessageType, msg)
Expand Down
9 changes: 2 additions & 7 deletions datachannel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,21 +384,16 @@ func TestDataChannel(t *testing.T) {
assert.True(t, reflect.DeepEqual(dc0.Config, *cfg), "local config should match")
assert.True(t, reflect.DeepEqual(dc1.Config, *cfg), "remote config should match")

err = dc0.commitReliabilityParams()
assert.NoError(t, err, "should succeed")
err = dc1.commitReliabilityParams()
assert.NoError(t, err, "should succeed")

var n int

binary.BigEndian.PutUint32(sbuf, 1)
n, err = dc0.WriteDataChannel(sbuf, true)
assert.Nil(t, err, "Read() should succeed")
assert.Nil(t, err, "Write() should succeed")
assert.Equal(t, len(sbuf), n, "data length should match")

binary.BigEndian.PutUint32(sbuf, 2)
n, err = dc0.WriteDataChannel(sbuf, true)
assert.Nil(t, err, "Read() should succeed")
assert.Nil(t, err, "Write() should succeed")
assert.Equal(t, len(sbuf), n, "data length should match")

time.Sleep(100 * time.Millisecond)
Expand Down

0 comments on commit f784cc1

Please sign in to comment.