protocol

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2023 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package protocol provides a object representation of a socket.io packet.

This package currently supports socket.io version 1 to socket.io version 5. It provides classes and API to read and write socket.io wire format, as represented by:

<packet type>[<# of binary attachments>-][<namespace>,][<acknowledgment id>][JSON-stringified payload without binary]
[<binary attachment>]

or as a real example:

1-/admin,456["project:delete",{"_placeholder":true,"num":0}]

this is with the API:

Packet.Type
Packet.AckID
Packet.Namespace
Packet.Data

This object is expected to be written and read directly to the connection. It's a streaming but transport agnostic.

Index

Constants

View Source
const (
	ErrReadFailed                  erro.StringF = "failed to read bytes:: %w"
	ErrUnmarshalInitialFieldFailed erro.StringF = "failed to unmarshal initial fields:: %w"
	ErrMarshalDataFailed           erro.StringF = "failed to marshal data:: %w"
	ErrUnmarshalDataFailed         erro.StringF = "failed to unmarshal data:: %w"
	ErrMarshalBinaryDataFailed     erro.StringF = "failed to marshal binary data:: %w"
	ErrUnmarshalBinaryDataFailed   erro.StringF = "failed to unmarshal binary data:: %w"
	ErrParseIntFailed              erro.StringF = "failed to parse int (%s):: %w"
	ErrUnexpectedPacketType        erro.StringF = "unexpected data type %T"
	ErrUnexpectedAttachmentEnd     erro.String  = "unexpected attachment end"
	ErrUnexpectedJSONEnd           erro.String  = "unexpected JSON end"
	ErrBinaryDataUnsupported       erro.String  = "binary data unsupported in this version"
	ErrReadUseBuffer               errsPacket   = "%s: read buffer"
	ErrShortRead                   erro.State   = "read: short"
	ErrShortWrite                  erro.State   = "write: short"
	ErrEmptyDataArray              erro.State   = "data array: empty"
)

All of the errors that can happen while reading or writing socket.io packets

View Source
const (
	ErrDecodeBase64Failed erro.StringF = "failed to decode msgpack base64 field:: %w"
	ErrDecodeFieldFailed  erro.StringF = "failed to decode msgpack field:: %w"
	ErrEncodeFieldFailed  erro.StringF = "failed to encode msgpack field:: %w"
)
View Source
const (
	ConnectPacket packetType = iota
	DisconnectPacket
	EventPacket
	AckPacket
	ErrorPacket
)

The packet type codes available in the socket.io protocol version 1

View Source
const (
	BinaryAckPacket packetType = BinaryEventPacket + 1
)
View Source
const (
	BinaryEventPacket packetType = ErrorPacket + 1
)

add a BINARY_EVENT packet type after the ERROR packet type

View Source
const (
	ConnectErrorPacket packetType = ErrorPacket
)

Variables

This section is empty.

Functions

This section is empty.

Types

type NewPacket

type NewPacket func() Packet

NewPacket provides an external type for a Packet factory function. This is mainly used when creating new socket.io protocol transports.

type Option

type Option func(Packet)

Option is a type that is used for configuring an existing Packet types with data

func WithAckID

func WithAckID(ackID uint64) Option

WithAckID sets the AckID field in a Packet object

func WithNamespace

func WithNamespace(namespace string) Option

WithNamespace sets the Namespace field in a Packet object

func WithType

func WithType(_type byte) Option

WithType sets the Type field in a Packet object

type Packet

type Packet interface {
	WithOption(...Option) Packet

	WithType(byte) Packet
	WithNamespace(string) Packet
	WithAckID(uint64) Packet
	WithData(interface{}) Packet
}

Packet is the interface for objects that can be passed around as socket.io packets. It provides a fluent interface for adding data common data to the underling type. The WithOption method can be used to add data to underling types that have additional data than that standard, type, namespace, ackID and data.

func NewPacketV1

func NewPacketV1() Packet

NewPacketV1 returns a Packet interface that will read/write socket.io version 1 packets

func NewPacketV2

func NewPacketV2() Packet

NewPacketV2 returns a Packet interface that will read/write socket.io version 2 packets

func NewPacketV3

func NewPacketV3() Packet

func NewPacketV4

func NewPacketV4() Packet

func NewPacketV5

func NewPacketV5() Packet

type PacketError

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

A PacketError holds buffered data that can be used in the event of an error. The underlining error is still sent, and can be compared with using errors.Is.

func (PacketError) Error

func (e PacketError) Error() string

Error he method to allow this struct to be passed as an error

func (PacketError) Is

func (e PacketError) Is(target error) bool

Is matches the target error to one of the underlining errors attached to this struct.

type PacketV1

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

PacketV1 embeds a base packet and will convert the SocketIO version 1 values

func (*PacketV1) Copy

func (pac *PacketV1) Copy(w io.Writer, r io.Reader) (n int64, err error)

Copy forces an io.Copy to use the .Read and .Write methods to provide the copy

func (*PacketV1) GetAckID

func (pac *PacketV1) GetAckID() uint64

GetAckID returns the underlining string type for a socket.io packet AckID

func (*PacketV1) GetData

func (pac *PacketV1) GetData() interface{}

GetData returns the underlining data string/array type for a socket.io packet Data

func (*PacketV1) GetNamespace

func (pac *PacketV1) GetNamespace() string

GetNamespace returns the underlining string type for a socket.io packet Namespace

func (*PacketV1) GetType

func (pac *PacketV1) GetType() byte

GetType returns the underlining byte type for a socket.io packet Type

func (PacketV1) Len added in v0.0.4

func (pac PacketV1) Len() (n int)

func (*PacketV1) Read

func (pac *PacketV1) Read(p []byte) (n int, err error)

Read writes out the PacketV1 object to a socket.io protocol version 1 wire format to p []bytes. This method can handle Read being called multiple times during the course of populating the []bytes.

func (*PacketV1) ReadFrom

func (pac *PacketV1) ReadFrom(r io.Reader) (n int64, err error)

ReadFrom copies the []bytes from the socket.io wire format to the PacketV1 struct.

func (*PacketV1) WithAckID

func (pac *PacketV1) WithAckID(x uint64) Packet

WithAckID sets the packet AckID to the x string (the underlining type for packetAckID) type. This is so that external packages don't need to know about protocol.PacketAckID types, but just the basic underlining type, we will convert it to the correct type.

func (*PacketV1) WithData

func (pac *PacketV1) WithData(x interface{}) Packet

func (*PacketV1) WithNamespace

func (pac *PacketV1) WithNamespace(x string) Packet

WithNamespace sets the packet Namespace to the x string (the underlining type for packetNS) type. This is so that external packages don't need to know about protocol.PacketType types, but just the basic underlining type, we will convert it to the correct type.

func (*PacketV1) WithOption

func (pac *PacketV1) WithOption(opts ...Option) Packet

WithOption applies all of the Option functions to a packet object, then returns the Packet interface. This method satisfies the interface for a Packet, so it allows packet(s) to be Packet(s).

func (*PacketV1) WithType

func (pac *PacketV1) WithType(x byte) Packet

WithType sets the packet Type to the x byte (the underlining type for packetType) type. This is so that external packages don't need to know about protocol.PacketType types, but just the basic underlining type, we will convert it to the correct type.

func (*PacketV1) Write

func (pac *PacketV1) Write(p []byte) (n int, err error)

Write takes in protocol version 1 wire format in p []bytes. This method can handle Read being called multiple times during the course of populating the PacketV1 object.

func (*PacketV1) WriteTo

func (pac *PacketV1) WriteTo(w io.Writer) (n int64, err error)

WriteTo copies the PacketV1 struct to the []byte socket.io wire format.

type PacketV2

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

PacketV2 embeds a base packet and will convert the SocketIO version 2 values

func (*PacketV2) Copy

func (pac *PacketV2) Copy(w io.Writer, r io.Reader) (n int64, err error)

Copy forces an io.Copy to use the .Read and .Write methods to provide the copy

func (*PacketV2) GetAckID

func (pac *PacketV2) GetAckID() uint64

GetAckID returns the underlining string type for a socket.io packet AckID

func (*PacketV2) GetData

func (pac *PacketV2) GetData() interface{}

GetData returns the underlining data string/array type for a socket.io packet Data

func (*PacketV2) GetNamespace

func (pac *PacketV2) GetNamespace() string

GetNamespace returns the underlining string type for a socket.io packet Namespace

func (*PacketV2) GetType

func (pac *PacketV2) GetType() byte

GetType returns the underlining byte type for a socket.io packet Type

func (PacketV2) Len added in v0.0.4

func (pac PacketV2) Len() (n int)

func (*PacketV2) Read

func (pac *PacketV2) Read(p []byte) (n int, err error)

Read writes out the PacketV2 object to a socket.io protocol version 2 wire format to p []bytes. This method can handle Read being called multiple times during the course of populating the []bytes.

func (*PacketV2) ReadFrom

func (pac *PacketV2) ReadFrom(r io.Reader) (n int64, err error)

ReadFrom copies the []bytes from the socket.io wire format to the PacketV1 struct.

func (*PacketV2) WithAckID

func (pac *PacketV2) WithAckID(x uint64) Packet

WithAckID sets the packet AckID to the x string (the underlining type for packetAckID) type. This is so that external packages don't need to know about protocol.PacketAckID types, but just the basic underlining type, we will convert it to the correct type.

func (*PacketV2) WithData

func (pac *PacketV2) WithData(x interface{}) Packet

func (*PacketV2) WithNamespace

func (pac *PacketV2) WithNamespace(x string) Packet

WithNamespace sets the packet Namespace to the x string (the underlining type for packetNS) type. This is so that external packages don't need to know about protocol.PacketType types, but just the basic underlining type, we will convert it to the correct type.

func (*PacketV2) WithOption

func (pac *PacketV2) WithOption(opts ...Option) Packet

WithOption applies all of the Option functions to a packet object, then returns the Packet interface. This method satisfies the interface for a Packet, so it allows packet(s) to be Packet(s).

func (*PacketV2) WithType

func (pac *PacketV2) WithType(x byte) Packet

WithType sets the packet Type to the x byte (the underlining type for packetType) type. This is so that external packages don't need to know about protocol.PacketType types, but just the basic underlining type, we will convert it to the correct type.

func (*PacketV2) Write

func (pac *PacketV2) Write(p []byte) (n int, err error)

Write takes in protocol version 2 wire format in p []bytes. This method can handle Read being called multiple times during the course of populating the PacketV2 object.

func (*PacketV2) WriteTo

func (pac *PacketV2) WriteTo(w io.Writer) (n int64, err error)

WriteTo copies the PacketV1 struct to the []byte socket.io wire format.

type PacketV3

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

func (*PacketV3) Copy

func (pac *PacketV3) Copy(w io.Writer, r io.Reader) (n int64, err error)

func (*PacketV3) GetAckID

func (pac *PacketV3) GetAckID() uint64

GetAckID returns the underlining string type for a socket.io packet AckID

func (*PacketV3) GetData

func (pac *PacketV3) GetData() interface{}

GetData returns the underlining data string/array type for a socket.io packet Data

func (*PacketV3) GetNamespace

func (pac *PacketV3) GetNamespace() string

GetNamespace returns the underlining string type for a socket.io packet Namespace

func (*PacketV3) GetType

func (pac *PacketV3) GetType() byte

GetType returns the underlining byte type for a socket.io packet Type

func (PacketV3) Len added in v0.0.4

func (pac PacketV3) Len() (n int)

func (*PacketV3) Read

func (pac *PacketV3) Read(p []byte) (n int, err error)

func (*PacketV3) ReadFrom

func (pac *PacketV3) ReadFrom(r io.Reader) (n int64, err error)

func (*PacketV3) WithAckID

func (pac *PacketV3) WithAckID(x uint64) Packet

WithAckID sets the packet AckID to the x string (the underlining type for packetAckID) type. This is so that external packages don't need to know about protocol.PacketAckID types, but just the basic underlining type, we will convert it to the correct type.

func (*PacketV3) WithData

func (pac *PacketV3) WithData(x interface{}) Packet

WithData sets the packet Namespace to the x interface{} (the underlining type for packetData) type. This is so that external packages don't need to know about protocol.PacketDataString, protocol.PacketDataArray or protocol.PacketDataObject types, but just the basic underlining type, we will convert it to the correct type.

func (*PacketV3) WithNamespace

func (pac *PacketV3) WithNamespace(x string) Packet

WithNamespace sets the packet Namespace to the x string (the underlining type for packetNS) type. This is so that external packages don't need to know about protocol.PacketType types, but just the basic underlining type, we will convert it to the correct type.

func (*PacketV3) WithOption

func (pac *PacketV3) WithOption(opts ...Option) Packet

WithOption applies all of the Option functions to a packet object, then returns the Packet interface. This method satisfies the interface for a Packet, so it allows packet(s) to be Packet(s).

func (*PacketV3) WithType

func (pac *PacketV3) WithType(x byte) Packet

WithType sets the packet Type to the x byte (the underlining type for packetType) type. This is so that external packages don't need to know about protocol.PacketType types, but just the basic underlining type, we will convert it to the correct type.

func (*PacketV3) Write

func (pac *PacketV3) Write(p []byte) (n int, err error)

func (*PacketV3) WriteTo

func (pac *PacketV3) WriteTo(w io.Writer) (n int64, err error)

type PacketV4

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

func (*PacketV4) Copy

func (pac *PacketV4) Copy(w io.Writer, r io.Reader) (n int64, err error)

func (*PacketV4) GetAckID

func (pac *PacketV4) GetAckID() uint64

GetAckID returns the underlining string type for a socket.io packet AckID

func (*PacketV4) GetData

func (pac *PacketV4) GetData() interface{}

GetData returns the underlining data string/array type for a socket.io packet Data

func (*PacketV4) GetNamespace

func (pac *PacketV4) GetNamespace() string

GetNamespace returns the underlining string type for a socket.io packet Namespace

func (*PacketV4) GetType

func (pac *PacketV4) GetType() byte

GetType returns the underlining byte type for a socket.io packet Type

func (PacketV4) Len added in v0.0.4

func (pac PacketV4) Len() (n int)

func (*PacketV4) Read

func (pac *PacketV4) Read(p []byte) (n int, err error)

func (*PacketV4) ReadBinary added in v0.0.4

func (pac *PacketV4) ReadBinary() (bin func(r io.Reader) error)

func (*PacketV4) ReadFrom

func (pac *PacketV4) ReadFrom(r io.Reader) (n int64, err error)

func (*PacketV4) WithAckID

func (pac *PacketV4) WithAckID(x uint64) Packet

WithAckID sets the packet AckID to the x string (the underlining type for packetAckID) type. This is so that external packages don't need to know about protocol.PacketAckID types, but just the basic underlining type, we will convert it to the correct type.

func (*PacketV4) WithData

func (pac *PacketV4) WithData(x interface{}) Packet

WithData sets the packet Namespace to the x interface{} (the underlining type for packetData) type. This is so that external packages don't need to know about protocol.PacketDataString, protocol.PacketDataArray or protocol.PacketDataObject types, but just the basic underlining type, we will convert it to the correct type.

func (*PacketV4) WithNamespace

func (pac *PacketV4) WithNamespace(x string) Packet

WithNamespace sets the packet Namespace to the x string (the underlining type for packetNS) type. This is so that external packages don't need to know about protocol.PacketType types, but just the basic underlining type, we will convert it to the correct type.

func (*PacketV4) WithOption

func (pac *PacketV4) WithOption(opts ...Option) Packet

WithOption applies all of the Option functions to a packet object, then returns the Packet interface. This method satisfies the interface for a Packet, so it allows packet(s) to be Packet(s).

func (*PacketV4) WithType

func (pac *PacketV4) WithType(x byte) Packet

WithType sets the packet Type to the x byte (the underlining type for packetType) type. This is so that external packages don't need to know about protocol.PacketType types, but just the basic underlining type, we will convert it to the correct type.

func (*PacketV4) Write

func (pac *PacketV4) Write(p []byte) (n int, err error)

func (*PacketV4) WriteTo

func (pac *PacketV4) WriteTo(w io.Writer) (n int64, err error)

type PacketV5

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

func (*PacketV5) Copy

func (pac *PacketV5) Copy(w io.Writer, r io.Reader) (n int64, err error)

func (*PacketV5) GetAckID

func (pac *PacketV5) GetAckID() uint64

GetAckID returns the underlining string type for a socket.io packet AckID

func (*PacketV5) GetData

func (pac *PacketV5) GetData() interface{}

GetData returns the underlining data string/array type for a socket.io packet Data

func (*PacketV5) GetNamespace

func (pac *PacketV5) GetNamespace() string

GetNamespace returns the underlining string type for a socket.io packet Namespace

func (*PacketV5) GetType

func (pac *PacketV5) GetType() byte

GetType returns the underlining byte type for a socket.io packet Type

func (PacketV5) Len added in v0.0.4

func (pac PacketV5) Len() (n int)

func (*PacketV5) Read

func (pac *PacketV5) Read(p []byte) (n int, err error)

func (*PacketV5) ReadBinary added in v0.0.4

func (pac *PacketV5) ReadBinary() (bin func(r io.Reader) error)

func (*PacketV5) ReadFrom

func (pac *PacketV5) ReadFrom(r io.Reader) (n int64, err error)

func (*PacketV5) WithAckID

func (pac *PacketV5) WithAckID(x uint64) Packet

WithAckID sets the packet AckID to the x string (the underlining type for packetAckID) type. This is so that external packages don't need to know about protocol.PacketAckID types, but just the basic underlining type, we will convert it to the correct type.

func (*PacketV5) WithData

func (pac *PacketV5) WithData(x interface{}) Packet

WithData sets the packet Namespace to the x interface{} (the underlining type for packetData) type. This is so that external packages don't need to know about protocol.PacketDataString, protocol.PacketDataArray or protocol.PacketDataObject types, but just the basic underlining type, we will convert it to the correct type.

func (*PacketV5) WithNamespace

func (pac *PacketV5) WithNamespace(x string) Packet

WithNamespace sets the packet Namespace to the x string (the underlining type for packetNS) type. This is so that external packages don't need to know about protocol.PacketType types, but just the basic underlining type, we will convert it to the correct type.

func (*PacketV5) WithOption

func (pac *PacketV5) WithOption(opts ...Option) Packet

WithOption applies all of the Option functions to a packet object, then returns the Packet interface. This method satisfies the interface for a Packet, so it allows packet(s) to be Packet(s).

func (*PacketV5) WithType

func (pac *PacketV5) WithType(x byte) Packet

WithType sets the packet Type to the x byte (the underlining type for packetType) type. This is so that external packages don't need to know about protocol.PacketType types, but just the basic underlining type, we will convert it to the correct type.

func (*PacketV5) Write

func (pac *PacketV5) Write(p []byte) (n int, err error)

func (*PacketV5) WriteTo

func (pac *PacketV5) WriteTo(w io.Writer) (n int64, err error)

Jump to

Keyboard shortcuts

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