Documentation ¶
Overview ¶
msg is a byte serialization format for 'packets' or 'messages' or 'envelopes'.
Goal of msg is to be able to store these 'packets' or 'messages' on storage mediums, be able to send them through ip links, fifo, unix pipes. (disclaimer: checksums are not.. yet.. part of the packets, so these are not to be used on unstable/unreliable mediums.)
Each packet contains ID, Feature and data. Data can be of any length, and it shall automatically be packed into a sequence of packets that can be later recompiled into one.
Index ¶
- Constants
- type Box
- type Envelope
- func (e *Envelope) AddLabels(id, feature []byte) error
- func (e *Envelope) AddMessage(data []byte) error
- func (e *Envelope) Checksum(from, to int) ([]byte, error)
- func (e *Envelope) Generate() (*[]byte, int, error)
- func (e *Envelope) GenerateFromByte(n int) (*[]byte, int, error)
- func (e *Envelope) GetHeaderSize() int
- func (e *Envelope) SetCompression(cmp uint8) error
- func (e *Envelope) SetVersion(ver uint16) error
- type EnvelopeHeaders
- type EnvelopeLabels
- type Headers
- func (h *Headers) CalculatePacketCount(size int)
- func (h *Headers) Checksum() ([]byte, error)
- func (h *Headers) Generate() (*[]byte, error)
- func (h *Headers) IncrementIndex()
- func (h *Headers) Parse(data *[]byte, pad int) error
- func (h *Headers) SetChecksum(checksum []byte) error
- func (h *Headers) ValidateChecksum(checksum [20]byte) bool
- type Message
- func (m *Message) AppendData(data []byte) error
- func (m *Message) Checksum() ([]byte, error)
- func (m *Message) ChecksumFromBytes(from, to int) ([]byte, error)
- func (m *Message) Compress(level int) error
- func (m *Message) DeCompress() error
- func (m *Message) GetData(from, to int) (*[]byte, int, error)
- func (m *Message) Len() int
- func (m *Message) Parse(data *[]byte, pad int) error
- func (m *Message) SetData(d []byte) error
- type Prefix
- type StreamReader
- func (s *StreamReader) Close() error
- func (s *StreamReader) GetEnvelope() *Envelope
- func (s *StreamReader) GetState() *envelopeReadState
- func (s *StreamReader) IsFull() bool
- func (s *StreamReader) Read(data []byte) (int, error)
- func (s *StreamReader) ReadWhole() (int, error)
- func (s *StreamReader) Reset(e *Envelope, r io.Reader) error
- func (s *StreamReader) SetState(es *envelopeReadState)
- type StreamWriter
Constants ¶
const ( //init byte, not very necessary but helps to keep track of the stream. INIT_BYTE = 1 //static values, may change between versions PREFIX_LEN = 8 HEADER_LEN = 16 + 20 ENVELOPE_HEADER_LEN = 8 MESSAGE_PREFIX_LEN = 2 //CHECKSUM_LEN = 20 TOTAL_HEADER_LEN = PREFIX_LEN + HEADER_LEN + ENVELOPE_HEADER_LEN + MESSAGE_PREFIX_LEN // + CHECKSUM_LEN //default message size - Can be modified to fit use cases, such as but not limited to Tcp jumbo frames, large files, etc. //by default, the size is calculated to fit into TCP frame. SIZE = 1500 - TOTAL_HEADER_LEN - INIT_BYTE //1445 //packet types. this is to be changed possibly, or one could make custom types? these are just hints for receiver. INIT = iota FRAGMENT STREAM ROUTE BROADCAST MULTICAST )
Envelope constants.
const ( NIL = iota LZ4 GZIP )
Compression types
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Box ¶
type Box struct {
// contains filtered or unexported fields
}
func (*Box) InsertEnvelope ¶
func (*Box) RecornstructMessage ¶
type Envelope ¶
type Envelope struct { Prefix Prefix // Prefix for every envelope Headers Headers // headers for the content EnvelopeHeaders EnvelopeHeaders // envelope headers EnvelopeLabels EnvelopeLabels //envelope labels Message Message // message MessageSizeLimit int //limit of message size (for tcp frames) AutoIncrement bool //automatically increment message index IncludeChecksum bool }
Envelope structure contains all parts of the envelope
func (*Envelope) AddLabels ¶
AddLabels is a convenience function to add ID and feature byte slices into the Envelope's EnvelopeLabels struct. you can manually insert these by doing something like this:
e.EnvelopeLabels.MessageId = []byte{"my id"}
func (*Envelope) AddMessage ¶
AddMessage is a convenience function to insert data into the envelopes Message. You can also insert data manually by doing something like this:
myData := []byte{"foo bar"} e.Message.Data = &myData
func (*Envelope) Generate ¶
Generate generates the envelope from zero. This function retuns data, length and error.
func (*Envelope) GenerateFromByte ¶
GenerateFromByte generates envelope starting from N byte. This function retuns data, length and error. This is for sending large, or multipart envelopes.
processedBytes := 0 for processedBytes < e.Envelope.Message.Len() { data, n, err := s.envelope.GenerateFromByte(processed) if err != nil { //..handle error } //..send/store/whatever with the data. processedBytes += n }
func (*Envelope) GetHeaderSize ¶
GetHeaderSize Calculates header size of the envelope. Does not include the hidden headers included in Message used for compression.
func (*Envelope) SetCompression ¶
SetCompression sets the compression method for data, set to 'NIL' for no compression.
func (*Envelope) SetVersion ¶
SetVersions sets the envelope version TODO - this is not currently used or validated as this is version 1.
type EnvelopeHeaders ¶
type EnvelopeHeaders struct { MessageIdLength uint16 //[0:4] length of the message id MessageFeatureLength uint16 //[4:8] length of the message feature }
EnvelopeHeaders contain the headers for the message header, including ID and Feature labels lengths.
func (*EnvelopeHeaders) Checksum ¶
func (eh *EnvelopeHeaders) Checksum() ([]byte, error)
Checksum generates SHA-1 checksum from the EnvelopeHeaders for envelope.
func (*EnvelopeHeaders) Generate ¶
func (eh *EnvelopeHeaders) Generate() (*[]byte, error)
Generate generates the EnvelopeHeaders for the envelope.
func (*EnvelopeHeaders) Parse ¶
func (eh *EnvelopeHeaders) Parse(data *[]byte, pad int) error
Parse parses input data, data can be read from padded starting position.
func (*EnvelopeHeaders) SetLengths ¶
func (eh *EnvelopeHeaders) SetLengths(el EnvelopeLabels)
SetLengths sets the values for the EnvelopeHeaders from EnvelopeLabels.
type EnvelopeLabels ¶
EnvelopeLabels contain optional user defined ID and Feature for the packet. TODO - limits for the sizes to ensure that the labels cannot be larger than the packet max size itself.
func (*EnvelopeLabels) Checksum ¶
func (el *EnvelopeLabels) Checksum() ([]byte, error)
Checksum returns SHA-1 checksum for the EnvelopeLabels
func (*EnvelopeLabels) Generate ¶
func (el *EnvelopeLabels) Generate() (*[]byte, error)
Generate generates the EnvelopeLables for the envelope
func (*EnvelopeLabels) GetLengths ¶
func (el *EnvelopeLabels) GetLengths() (uint16, uint16)
GetLengths returns the lengths of MessageId and Messagefeature.
func (*EnvelopeLabels) Len ¶
func (el *EnvelopeLabels) Len() int
Len returns combined length of the MessageId and MessageFeature.
func (*EnvelopeLabels) Parse ¶
func (el *EnvelopeLabels) Parse(eh EnvelopeHeaders, data *[]byte, padding int) error
Parse parses input data, data can be read from padded starting position.
type Headers ¶
type Headers struct { PacketIndex uint32 //[0:4] number of packets in the message TotalPackets uint32 //[4:8] total number of packets in this message TotalLength uint64 //[8:16] total length of the message PacketChecksum [20]byte //[16:36] Checksum (potentially zeroes.) }
Headers contain the headers of the message, which includes the index of the packet (if multipart packet), total number of packets, and total length of the combined data contained of all the packets.
func (*Headers) CalculatePacketCount ¶
func (*Headers) IncrementIndex ¶
func (h *Headers) IncrementIndex()
func (*Headers) SetChecksum ¶
SetChecksum is a convenience function to set checksum
func (*Headers) ValidateChecksum ¶
type Message ¶
type Message struct { Data *[]byte //Data of any size Compressed bool //true false if current data is compressed CompressionType uint8 //compression type used/to be used. // contains filtered or unexported fields }
Message contains the (optional) user defined data for the envelope.
func (*Message) AppendData ¶
func (*Message) ChecksumFromBytes ¶
func (*Message) DeCompress ¶
DeCompress decompresses the current data and marks it as uncompressed.
func (*Message) GetData ¶
GetData retusn data from X position to Y position. Returns data, length, and error.
type Prefix ¶
type Prefix struct { Init uint8 // [0:1] always 1? Version uint16 // [1:3] 1..to be changed? Length uint16 // [3:5] length of whole packet HeaderLength uint16 // [5:7] length of headers Type uint8 // [7:8] packet type }
Prefix contains the basic data for the current packet Including: Init byte, version of the packet, length of the current packet, length of headers and type.
type StreamReader ¶
type StreamReader struct {
// contains filtered or unexported fields
}
func NewStreamReader ¶
func NewStreamReader(e *Envelope, r io.Reader) (*StreamReader, error)
func (*StreamReader) Close ¶
func (s *StreamReader) Close() error
func (*StreamReader) GetEnvelope ¶
func (s *StreamReader) GetEnvelope() *Envelope
func (*StreamReader) GetState ¶
func (s *StreamReader) GetState() *envelopeReadState
func (*StreamReader) IsFull ¶
func (s *StreamReader) IsFull() bool
func (*StreamReader) Read ¶
func (s *StreamReader) Read(data []byte) (int, error)
todo FIX does not implement reader{} .. wtf was I thinking
func (*StreamReader) ReadWhole ¶
func (s *StreamReader) ReadWhole() (int, error)
func (*StreamReader) SetState ¶
func (s *StreamReader) SetState(es *envelopeReadState)
type StreamWriter ¶
type StreamWriter struct {
// contains filtered or unexported fields
}
func NewStreamWriter ¶
func NewStreamWriter(e *Envelope, writer io.Writer) (*StreamWriter, error)
func (*StreamWriter) Close ¶
func (s *StreamWriter) Close() error
Close - this does nothing, does it even need to do anything?
func (*StreamWriter) Flush ¶
func (s *StreamWriter) Flush() error
Flush implements io.Writer (does nothing)