frame

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2024 License: MIT Imports: 8 Imported by: 0

README

frame

這裏定義了一種簡單快速的 frame 編碼方案,目的在於序列化數據以便存儲或通過網路存儲

frame 由三部分組成

  • id uint8 uint16 uint32 uint64 數據 id,雙方使用 id 識別數據是什麼
  • flag 此字節最高的 1bit 如果爲 0 表示只有一個 payload
  • payload 7bit+不定長度,記錄了實際數據載荷

flag+payload 數量是不定的

id + 0 + payload

id + 1 + payload, + 1 + payload, ...,  0 + payload

Documentation

Index

Constants

View Source
const (
	Metadata     = 1
	PrivateChain = 2
	PublicChain  = 3
	PublicKey    = 4
)

Variables

View Source
var ErrFramePayloadLengthInvalid = errors.New(`frame payload length invalid`)
View Source
var ErrFrameWriterExpired = errors.New(`frame writer expired`)
View Source
var ErrIdBitsInvalid = errors.New(`id bits invalid`)
View Source
var ErrIdInvalid = errors.New(`ID Invalid`)
View Source
var ErrNotMetadata = errors.New(`not a metadata`)
View Source
var ErrNotPrivateChain = errors.New(`not a private chain`)
View Source
var ErrNotPublicChain = errors.New(`not a public chain`)
View Source
var ErrNotPublicKey = errors.New(`not a public key`)
View Source
var ErrPayloadBitsInvalid = errors.New(`payload bits invalid`)

Functions

func BytesToString

func BytesToString(b []byte) string

func MarshalMetadata

func MarshalMetadata(m *raw.Metadata) (b []byte, e error)

func MarshalPrivateChain

func MarshalPrivateChain(m *raw.PrivateChain) (b []byte, e error)

func MarshalPublicChain

func MarshalPublicChain(m *raw.PublicChain) (b []byte, e error)

func MarshalPublicKey

func MarshalPublicKey(m *raw.PublicKey) (b []byte, e error)

func StringToBytes

func StringToBytes(s string) []byte

func UnmarshalMetadata

func UnmarshalMetadata(b []byte, m *raw.Metadata) (e error)

func UnmarshalPrivateChain

func UnmarshalPrivateChain(b []byte, m *raw.PrivateChain) (e error)

func UnmarshalPublicChain

func UnmarshalPublicChain(b []byte, m *raw.PublicChain) (e error)

func UnmarshalPublicKey

func UnmarshalPublicKey(b []byte, m *raw.PublicKey) (e error)

func Write

func Write(w io.Writer, id uint8, data []byte) (n uint64, e error)

id + [sz + data]...

func WriteString

func WriteString(w io.Writer, id uint8, data string) (uint64, error)

Types

type Decoder

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

解析器

func NewDecoder

func NewDecoder(r io.Reader, opt ...Option) (dec *Decoder, e error)

創建一個解碼器

func (*Decoder) Decode

func (dec *Decoder) Decode() (id uint64, data []byte, e error)

func (*Decoder) NextReader

func (dec *Decoder) NextReader() (id uint64, r *FrameReader, e error)

創建一個幀讀取器

type Encoder

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

編碼器

func NewEncoder

func NewEncoder(w io.Writer, opt ...Option) (enc *Encoder, e error)

創建一個編碼器

func (*Encoder) Encode

func (enc *Encoder) Encode(id uint64, data []byte) (e error)

編碼一個完整的幀

func (*Encoder) EncodeString

func (enc *Encoder) EncodeString(id uint64, data string) (e error)

編碼一個完整的幀

func (*Encoder) NextWriter

func (enc *Encoder) NextWriter(id uint64) (w *FrameWriter, e error)

創建一個幀寫入器

type FrameReader

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

func (*FrameReader) Read

func (f *FrameReader) Read(b []byte) (n int, e error)

type FrameWriter

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

func (*FrameWriter) Close

func (f *FrameWriter) Close() (e error)

func (*FrameWriter) Write

func (f *FrameWriter) Write(b []byte) (n int, e error)

func (*FrameWriter) WriteClose

func (f *FrameWriter) WriteClose(b []byte) (n int, e error)

type Option

type Option interface {
	// contains filtered or unexported methods
}

func WithByteOrder

func WithByteOrder(byteOrder binary.ByteOrder) Option

Set byteOrder

func WithId

func WithId(bits int) Option

Set the length of bytes occupied by id

func WithPayload

func WithPayload(bits int) Option

Set the length of bytes occupied by payload

Jump to

Keyboard shortcuts

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