Skip to content

Commit

Permalink
Add simplified reader/writer (reduce extensive error checking)
Browse files Browse the repository at this point in the history
  • Loading branch information
robinbraemer committed Dec 21, 2023
1 parent 476a8e2 commit d032469
Show file tree
Hide file tree
Showing 6 changed files with 336 additions and 296 deletions.
10 changes: 7 additions & 3 deletions pkg/edition/java/proto/codec/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,13 @@ func (d *Decoder) decodePayload(p []byte) (ctx *proto.PacketContext, err error)
}

// Packet is known, decode data into it.
if err = ctx.Packet.Decode(ctx, payload); err != nil {
if err == io.EOF { // payload was too short or decoder has a bug
err = io.ErrUnexpectedEOF
err = util.RecoverFunc(func() error {
return ctx.Packet.Decode(ctx, payload)
})
if err != nil {
if errors.Is(err, io.EOF) {
// payload was too short or packet decoder has a bug
err = errors.Join(err, io.ErrUnexpectedEOF)
}
return ctx, errs.NewSilentErr("error decoding packet (type: %T, id: %s, protocol: %s, direction: %s): %w",
ctx.Packet, ctx.PacketID, ctx.Protocol, ctx.Direction, err)
Expand Down
4 changes: 3 additions & 1 deletion pkg/edition/java/proto/codec/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ func (e *Encoder) WritePacket(packet proto.Packet) (n int, err error) {
Payload: nil,
}

if err = packet.Encode(ctx, buf); err != nil {
if err = util.RecoverFunc(func() error {
return packet.Encode(ctx, buf)
}); err != nil {
return
}

Expand Down
Loading

0 comments on commit d032469

Please sign in to comment.