Skip to content

Commit 1eef16f

Browse files
committed
u2fhid: Write as many bytes as possible
To be more efficient multiple u2fhid packets are written simultaneously if possible
1 parent 2b90fad commit 1eef16f

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

communication/u2fhid/u2fhid.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ func (communication *Communication) SendFrame(msg string) error {
9292

9393
func (communication *Communication) sendFrame(msg string) error {
9494
dataLen := len(msg)
95+
out := newBuffer()
9596
if dataLen == 0 {
9697
return nil
9798
}
@@ -103,8 +104,10 @@ func (communication *Communication) sendFrame(msg string) error {
103104
buf.WriteByte(0xee)
104105
}
105106
x := buf.Bytes() // needs to be in a var: https://github.com/golang/go/issues/14210#issuecomment-346402945
106-
_, err := communication.device.Write(x)
107-
return errp.WithMessage(errp.WithStack(err), "Failed to send message")
107+
out.Write(x)
108+
//_, err := communication.device.Write(x)
109+
//return errp.WithMessage(errp.WithStack(err), "Failed to send message")
110+
return nil
108111
}
109112
readBuffer := bytes.NewBufferString(msg)
110113
// init frame
@@ -134,6 +137,14 @@ func (communication *Communication) sendFrame(msg string) error {
134137
return err
135138
}
136139
}
140+
for out.Len() > 0 {
141+
x := out.Bytes()
142+
n, err := communication.device.Write(x)
143+
if err != nil {
144+
return errp.WithMessage(errp.WithStack(err), "Failed to send message")
145+
}
146+
out.Next(n)
147+
}
137148
return nil
138149
}
139150

0 commit comments

Comments
 (0)