Skip to content

feat(plumbing/format/pktline): add DelimPkt and ResponseEndPkt support (protocol v2) #1345

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
18 changes: 18 additions & 0 deletions plumbing/format/pktline/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ var (
Flush = []byte{}
// FlushString is the payload to use with the EncodeString method to encode a flush-pkt.
FlushString = ""
// DelimPkt is the contents of a delim-pkt pkt-line.
DelimPkt = []byte{'0', '0', '0', '1'}
// ResponseEndPkt is the contents of a response-end-pkt pkt-line.
ResponseEndPkt = []byte{'0', '0', '0', '2'}
// ErrPayloadTooLong is returned by the Encode methods when any of the
// provided payloads is bigger than MaxPayloadSize.
ErrPayloadTooLong = errors.New("payload is too long")
Expand All @@ -50,6 +54,20 @@ func (e *Encoder) Flush() error {
return err
}

// Delim encodes a delim-pkt to the output stream.
func (e *Encoder) Delim() error {
defer trace.Packet.Print("packet: > 0001")
_, err := e.w.Write(DelimPkt)
return err
}

// ResponseEnd encodes a response-end-pkt to the output stream.
func (e *Encoder) ResponseEnd() error {
defer trace.Packet.Print("packet: > 0002")
_, err := e.w.Write(ResponseEndPkt)
return err
}

// Encode encodes a pkt-line with the payload specified and write it to
// the output stream. If several payloads are specified, each of them
// will get streamed in their own pkt-lines.
Expand Down
21 changes: 19 additions & 2 deletions plumbing/format/pktline/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ var ErrInvalidPktLen = errors.New("invalid pkt-len found")
//
// After each Scan call, the Bytes method will return the payload of the
// corresponding pkt-line on a shared buffer, which will be 65516 bytes
// or smaller. Flush pkt-lines are represented by empty byte slices.
// or smaller. Flush, Delim and ResponseEnd pkt-lines are represented
// by an empty byte slice. The specific pkt-type can be deteremined via
// the FlushPkt, DelimPkt or ResponseEndPkt method.
//
// Scanning stops at EOF or the first I/O error.
type Scanner struct {
Expand Down Expand Up @@ -81,6 +83,21 @@ func (s *Scanner) Scan() bool {
return true
}

// FlushPkt returns true if the packet as a FlushPkt
func (s *Scanner) FlushPkt() bool {
return s.len == [lenSize]byte{'0', '0', '0', '0'}
}

// DelimPkt returns true if the packet as a DelimPkt
func (s *Scanner) DelimPkt() bool {
return s.len == [lenSize]byte{'0', '0', '0', '1'}
}

// ResponseEndPkt returns true if the packet as a ResponseEndPkt
func (s *Scanner) ResponseEndPkt() bool {
return s.len == [lenSize]byte{'0', '0', '0', '2'}
}

// Bytes returns the most recent payload generated by a call to Scan.
// The underlying array may point to data that will be overwritten by a
// subsequent call to Scan. It does no allocation.
Expand Down Expand Up @@ -108,7 +125,7 @@ func (s *Scanner) readPayloadLen() (int, error) {
case n == 0:
return 0, nil
case n <= lenSize:
return 0, ErrInvalidPktLen
return 0, nil
case n > OversizePayloadMax+lenSize:
return 0, ErrInvalidPktLen
default:
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy