Re: [go-nuts] Read N bytes from a TLS stream and block the caller while still respecting the deadline

2021-11-30 Thread 'wagner riffel' via golang-nuts
On Tue Nov 30, 2021 at 11:20 PM CET, LastName Almaember wrote:
> It's meant to first read a nine-byte header (8 bits of a type field
> followed by a 64 bit length). In theory, it should work fine, but it
> always immediately returns InputTooShort (right at the first check).

io.Readers returns up to len(p), not exactly len(p), you have to loop
over calling read until you have enough input, there's io.ReadAtLeast
and io.ReadFull helpers that does that for you.

> A bufio *might* work for this, however, I'm not confident that it will
> respect the deadline set on the connection.
>

bufio will respect your deadline, it doesn't skip errors from the
underlying reader, just returns them up. I suspect that it doesn't solve
the issue mentioned above tho.

BR.
-w

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CG3S8W6WU9LX.1XINQC5EZN91Q%40pampas.


[go-nuts] Read N bytes from a TLS stream and block the caller while still respecting the deadline

2021-11-30 Thread LastName Almaember
Hello,

I have this function:
// Read a message from a stream
func ReadFromStream(rd io.Reader) (Message, error) {
result := Message{}
// read the first nine bytes only
// (mtype + length)
buf := make([]byte, 9)
read, err := rd.Read(buf)
if err != nil {
return result, err
}
if read < 9 {
// we didn't read a full header. Well, shit.
return result, InputTooShort
}

result.MType = buf[0]
contentLen := binary.LittleEndian.Uint64(buf[1:])
result.Content = make([]byte, contentLen)

read, err = rd.Read(result.Content)
if err != nil {
return result, err
}
if read < int(contentLen) {
return result, InputTooShort
}
return result, nil
}

It's meant to first read a nine-byte header (8 bits of a type field 
followed by a 64 bit length). 
In theory, it should work fine, but it always immediately returns 
InputTooShort
(right at the first check).

A bufio *might* work for this, however, I'm not confident that it will 
respect the
deadline set on the connection.

Thanks for the attention,
almaember

P.S. This is my system:
[almaember@manjarocado ~]$ uname -a
Linux manjarocado 5.13.19-2-MANJARO #1 SMP PREEMPT Sun Sep 19 21:31:53 UTC 
2021 x86_64 GNU/Linux
[almaember@manjarocado ~]$ go version
go version go1.17.3 linux/amd64

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/3ea0fe76-f31b-4a49-afcb-5f0e611e3e4dn%40googlegroups.com.