Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a buffered reader to dtx connections #438

Merged
merged 2 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
}
Loading