Documentation ¶
Overview ¶
Package drpcwire provides low level helpers for the drpc wire protocol.
Index ¶
- func AppendFrame(buf []byte, fr Frame) []byte
- func AppendVarint(buf []byte, x uint64) []byte
- func MarshalError(err error) []byte
- func ReadVarint(buf []byte) (rem []byte, out uint64, ok bool, err error)
- func SplitFrame(data []byte, atEOF bool) (int, []byte, error)
- func SplitN(pkt Packet, n int, cb func(fr Frame) error) error
- func UnmarshalError(data []byte) error
- type Frame
- type ID
- type Kind
- type Packet
- type Reader
- type Writer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AppendFrame ¶
AppendFrame appends a marshaled form of the frame to the provided buffer.
func AppendVarint ¶
AppendVarint appends the varint encoding of x to the buffer and returns it.
func MarshalError ¶
MarshalError returns a byte form of the error with any error code incorporated.
func ReadVarint ¶
ReadVarint reads a varint encoded integer from the front of buf, returning the remaining bytes, the value, and if there was a success. if ok is false, the returned buffer is the same as the passed in buffer.
func SplitFrame ¶
SplitFrame is used by bufio.Scanner to split frames out of a stream of bytes.
func SplitN ¶
SplitN splits the marshaled form of the Packet into a number of frames such that each frame is at most n bytes. It calls the callback with every such frame. If n is zero, a default of 1024 is used.
func UnmarshalError ¶
UnmarshalError unmarshals the marshaled error to one with a code.
Types ¶
type Frame ¶
type Frame struct { // Data is the payload of bytes. Data []byte // ID is used so that the frame can be reconstructed. ID ID // Kind is the kind of the payload. Kind Kind // Done is true if this is the last frame for the ID. Done bool }
Frame is a split data frame on the wire.
func ParseFrame ¶
ParseFrame attempts to parse a frame at the beginning of buf. If successful then rem contains the unparsed data, fr contains the parsed frame, ok will be true, and err will be nil. If there is not enough data for a frame, ok will be false and err will be nil. If the data in the buf is malformed, then an error is returned.
type ID ¶
type ID struct { // Stream is the stream identifier. Stream uint64 // Message is the message identifier. Message uint64 }
ID represents a packet id.
type Kind ¶
type Kind uint8
Kind is the enumeration of all the different kinds of messages drpc sends.
const ( // KindInvoke is used to invoke an rpc. The body is the name of the rpc. KindInvoke Kind = 1 // KindMessage is used to send messages. The body is a protobuf. KindMessage Kind = 2 // KindError is used to inform that an error happened. The body is an error // with a code attached. KindError Kind = 3 // KindClose is used to inform that the rpc is dead. It has no body. KindClose Kind = 5 // KindCloseSend is used to inform that no more messages will be sent. // It has no body. KindCloseSend Kind = 6 // body must be empty )
type Packet ¶
type Packet struct { // Data is the payload of the packet. Data []byte // ID is the identifier for the packet. ID ID // Kind is the kind of the packet. Kind Kind }
Packet is a single message sent by drpc.
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Reader reconstructs packets from frames read from an io.Reader.
func (*Reader) ReadPacket ¶
ReadPacket reads a packet from the io.Reader. IDs read from frames must be monotonically increasing. When a new ID is read, the old data is discarded. This allows for easier asynchronous interrupts. If the amount of data in the Packet becomes too large, an error is returned.
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
Writer is a helper to buffer and write packets and frames to an io.Writer.
func NewWriter ¶
NewWriter returns a Writer that will attempt to buffer size data before sending it to the io.Writer.
func (*Writer) Flush ¶
Flush forces a flush of any buffered data to the io.Writer. It is a no-op if there is no data in the buffer.
func (*Writer) WriteFrame ¶
WriteFrame appends the frame into the buffer, and if the buffer is larger than the configured size, flushes it.
func (*Writer) WritePacket ¶
WritePacket writes the packet as a single frame, ignoring any size constraints.