Skip to content

Commit

Permalink
Added a buffered reader to dtx connections (#438)
Browse files Browse the repository at this point in the history
* Added a buffered reader to dtx connections

* Simplified dtx decoder tests and added buffers to all readers in tests
  • Loading branch information
diegoperini authored Jul 24, 2024
1 parent 6cc0b7c commit 029c608
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 36 deletions.
3 changes: 2 additions & 1 deletion ios/dtx_codec/connection.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dtx

import (
"bufio"
"errors"
"io"
"math"
Expand Down Expand Up @@ -167,7 +168,7 @@ func (dtxConn *Connection) Send(message []byte) error {
// reader reads messages from the byte stream and dispatches them to the right channel when they are decoded.
func reader(dtxConn *Connection) {
for {
reader := dtxConn.deviceConnection.Reader()
reader := bufio.NewReader(dtxConn.deviceConnection.Reader())
msg, err := ReadMessage(reader)
if err != nil {
defer dtxConn.close(err)
Expand Down
55 changes: 20 additions & 35 deletions ios/dtx_codec/decoder_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dtx_test

import (
"bufio"
"bytes"
"os"
"testing"
Expand Down Expand Up @@ -161,7 +162,7 @@ func TestFragmentedMessage(t *testing.T) {
nonblockingFullMessage := defragmenter.Extract()

// now test that the blocking decoder creates the same message and that it is decodeable
dtxReader := bytes.NewReader(dat)
dtxReader := bufio.NewReader(bytes.NewReader(dat))
msg, err = dtx.ReadMessage(dtxReader)
if assert.NoError(t, err) {
assert.Equal(t, uint16(3), msg.Fragments)
Expand All @@ -187,7 +188,7 @@ func TestFragmentedMessage(t *testing.T) {
assert.Equal(t, true, defragmenter.HasFinished())
defraggedMessage := defragmenter.Extract()
assert.Equal(t, defraggedMessage, nonblockingFullMessage)
dtxReader = bytes.NewReader(defraggedMessage)
dtxReader = bufio.NewReader(bytes.NewReader(defraggedMessage))
_, err = dtx.ReadMessage(dtxReader)
assert.NoError(t, err)
}
Expand All @@ -197,39 +198,23 @@ func TestDecoder(t *testing.T) {
if err != nil {
t.Fatal(err)
}
msg, remainingBytes, err := dtx.DecodeNonBlocking(dat)
if assert.NoError(t, err) {
assert.Equal(t, 0, len(remainingBytes))
assert.Equal(t, msg.Fragments, uint16(1))
assert.Equal(t, msg.FragmentIndex, uint16(0))
assert.Equal(t, msg.MessageLength, 612)
assert.Equal(t, 0, msg.ChannelCode)
assert.Equal(t, false, msg.ExpectsReply)
assert.Equal(t, 2, msg.Identifier)
assert.Equal(t, 0, msg.ChannelCode)

assert.Equal(t, dtx.MessageType(2), msg.PayloadHeader.MessageType)
assert.Equal(t, uint32(425), msg.PayloadHeader.AuxiliaryLength)
assert.Equal(t, uint32(596), msg.PayloadHeader.TotalPayloadLength)
assert.Equal(t, uint32(0), msg.PayloadHeader.Flags)

}

msg, err = dtx.ReadMessage(bytes.NewReader(dat))
if assert.NoError(t, err) {

assert.Equal(t, msg.Fragments, uint16(1))
assert.Equal(t, msg.FragmentIndex, uint16(0))
assert.Equal(t, msg.MessageLength, 612)
assert.Equal(t, 0, msg.ChannelCode)
assert.Equal(t, false, msg.ExpectsReply)
assert.Equal(t, 2, msg.Identifier)
assert.Equal(t, 0, msg.ChannelCode)

assert.Equal(t, dtx.MessageType(2), msg.PayloadHeader.MessageType)
assert.Equal(t, uint32(425), msg.PayloadHeader.AuxiliaryLength)
assert.Equal(t, uint32(596), msg.PayloadHeader.TotalPayloadLength)
assert.Equal(t, uint32(0), msg.PayloadHeader.Flags)

reader := bufio.NewReader(bytes.NewReader(append(dat[:], dat[:]...)))
for i := 0; i < 2; i++ {
msg, err := dtx.ReadMessage(reader)
if assert.NoError(t, err) {
assert.Equal(t, msg.Fragments, uint16(1))
assert.Equal(t, msg.FragmentIndex, uint16(0))
assert.Equal(t, msg.MessageLength, 612)
assert.Equal(t, 0, msg.ChannelCode)
assert.Equal(t, false, msg.ExpectsReply)
assert.Equal(t, 2, msg.Identifier)
assert.Equal(t, 0, msg.ChannelCode)

assert.Equal(t, dtx.MessageType(2), msg.PayloadHeader.MessageType)
assert.Equal(t, uint32(425), msg.PayloadHeader.AuxiliaryLength)
assert.Equal(t, uint32(596), msg.PayloadHeader.TotalPayloadLength)
assert.Equal(t, uint32(0), msg.PayloadHeader.Flags)
}
}
}

0 comments on commit 029c608

Please sign in to comment.