packet

package
v0.0.0-...-b172891 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 9, 2020 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

func NewReader

func NewReader(buffer []byte) *Reader

func (*Reader) Align

func (r *Reader) Align(width int)

Discard bytes up to the next multiple of width, e.g. Align(4) skips ahead until the next aligned 4-byte boundary.

func (*Reader) CheckRemaining

func (r *Reader) CheckRemaining(needed int) error

func (*Reader) ReadByte

func (r *Reader) ReadByte() byte

func (*Reader) ReadRemaining

func (r *Reader) ReadRemaining() []byte

func (*Reader) ReadSlice

func (r *Reader) ReadSlice(n int) []byte

func (*Reader) ReadString

func (r *Reader) ReadString(n int) string

func (*Reader) ReadUint16

func (r *Reader) ReadUint16() uint16

func (*Reader) ReadUint24

func (r *Reader) ReadUint24() uint32

func (*Reader) ReadUint32

func (r *Reader) ReadUint32() uint32

func (*Reader) ReadUint64

func (r *Reader) ReadUint64() uint64

func (*Reader) Remaining

func (r *Reader) Remaining() int

Return the number of bytes left in the buffer.

func (*Reader) Skip

func (r *Reader) Skip(n int)

type SharedBuffer

type SharedBuffer struct {
	// contains filtered or unexported fields
}

A SharedBuffer represents a read-only byte buffer that may be accessed concurrently from multiple goroutines. When a SharedBuffer is passed to a consumer function, the consumer should process the bytes and Release() the buffer as quickly as possible. If the bytes cannot be processed quickly, the consumer should make a copy, Release(), then continue processing its local copy.

Sharing is managed by reference counting. Hold() increments the reference count by 1, Release() decrements it by 1. The done function is called when the count reaches 0.

Example usage:

func consumer(buf *SharedBuffer) {
	defer buf.Release() // Ensure the shared buffer will be released.
	data := buf.Bytes()
	// Process data...
}

func producer() {
	data := generateData()
	doneCh := make(chan struct{})
	buf := NewSharedBuffer(data, len(consumers), func() { close(doneCh) })
	for _, consume := consumers {
		go consume(buf)
	}
	// Wait for all consumers to finish.
	<-doneCh
}

The goal is to avoid extraneous allocations/copies when a potentially large byte buffer needs to be consumed by multiple goroutines.

func NewSharedBuffer

func NewSharedBuffer(data []byte, count int, done func()) *SharedBuffer

func (*SharedBuffer) Bytes

func (buf *SharedBuffer) Bytes() []byte

Bytes returns the underlying byte buffer.

func (*SharedBuffer) Hold

func (buf *SharedBuffer) Hold()

Increments the hold count.

func (*SharedBuffer) Release

func (buf *SharedBuffer) Release()

Decrements the hold count. When the hold count reaches zero, the underlying byte buffer will be released.

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

func NewWriter

func NewWriter(buffer []byte) *Writer

func NewWriterSize

func NewWriterSize(n int) *Writer

func (*Writer) Align

func (w *Writer) Align(width int)

Pad with zeros up to the next multiple of width, e.g. Align(4) adds zero bytes until the next 4-byte boundary.

func (*Writer) Bytes

func (w *Writer) Bytes() []byte

Return a slice of the bytes written so far.

func (*Writer) Capacity

func (w *Writer) Capacity() int

Return the number of bytes that the underlying buffer can hold.

func (*Writer) CheckCapacity

func (w *Writer) CheckCapacity(needed int) error

func (*Writer) Length

func (w *Writer) Length() int

Return the number of bytes written so far.

func (*Writer) Reset

func (w *Writer) Reset()

func (*Writer) Rewind

func (w *Writer) Rewind(n int)

func (*Writer) WriteByte

func (w *Writer) WriteByte(v byte)

func (*Writer) WriteSlice

func (w *Writer) WriteSlice(p []byte) error

Write the given bytes, if there is enough room.

func (*Writer) WriteString

func (w *Writer) WriteString(s string) error

func (*Writer) WriteUint16

func (w *Writer) WriteUint16(v uint16)

func (*Writer) WriteUint24

func (w *Writer) WriteUint24(v uint32)

func (*Writer) WriteUint32

func (w *Writer) WriteUint32(v uint32)

func (*Writer) WriteUint64

func (w *Writer) WriteUint64(v uint64)

func (*Writer) ZeroPad

func (w *Writer) ZeroPad(n int)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL