Skip to content

Commit

Permalink
minecraft/conn.go: Introduce ReadBytes for reading packet payloads …
Browse files Browse the repository at this point in the history
…directly (#229)
  • Loading branch information
cooldogedev authored Aug 2, 2024
1 parent b324153 commit c32a9a2
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion minecraft/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,25 @@ func (conn *Conn) Write(b []byte) (n int, err error) {
return len(b), nil
}

// ReadBytes reads a packet from the connection without decoding it directly.
// For direct reading, consider using ReadPacket() which decodes the packet.
func (conn *Conn) ReadBytes() ([]byte, error) {
if data, ok := conn.takeDeferredPacket(); ok {
return data.full, nil
}
select {
case <-conn.close:
return nil, conn.closeErr("read")
case <-conn.readDeadline:
return nil, conn.wrap(context.DeadlineExceeded, "read")
case data := <-conn.packets:
return data.full, nil
}
}

// Read reads a packet from the connection into the byte slice passed, provided the byte slice is big enough
// to carry the full packet.
// It is recommended to use ReadPacket() rather than Read() in cases where reading is done directly.
// It is recommended to use ReadPacket() and ReadBytes() rather than Read() in cases where reading is done directly.
func (conn *Conn) Read(b []byte) (n int, err error) {
if data, ok := conn.takeDeferredPacket(); ok {
if len(b) < len(data.full) {
Expand Down

0 comments on commit c32a9a2

Please sign in to comment.