protocol

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Header size in bytes (1 byte type + 4 bytes length).
	HeaderSize = 5
)

Variables

View Source
var (
	ErrInvalidMessage = errors.New("invalid message")
)

Functions

func Unmarshal

func Unmarshal[T Parsable](msg T, data *Message)

Unmarshal converts a byte slice to the appropriate message type.

Types

type BeginConnection

type BeginConnection struct {
	Subdomain string
}

func (*BeginConnection) Marshal

func (b *BeginConnection) Marshal() *Message

Marshal converts a BeginConnection to a byte slice.

func (*BeginConnection) Unmarshal

func (b *BeginConnection) Unmarshal(payload []byte)

Unmarshal converts a byte slice to a BeginConnection.

type CloseConnection

type CloseConnection struct {
	Reason string
}

func (*CloseConnection) Marshal

func (c *CloseConnection) Marshal() *Message

func (*CloseConnection) Unmarshal

func (c *CloseConnection) Unmarshal(payload []byte)

type ConnectionReady

type ConnectionReady struct {
	Subdomain string
}

func (*ConnectionReady) Marshal

func (c *ConnectionReady) Marshal() *Message

Marshal converts a ConnectionReady to a byte slice.

func (*ConnectionReady) Unmarshal

func (c *ConnectionReady) Unmarshal(payload []byte)

Unmarshal converts a byte slice to a ConnectionReady.

type ConnectionRegister

type ConnectionRegister struct {
	Subdomain string
	Host      string
	Port      uint32
	Protocol  Protocol
	Token     string
}

func (*ConnectionRegister) Marshal

func (c *ConnectionRegister) Marshal() *Message

func (*ConnectionRegister) Unmarshal

func (c *ConnectionRegister) Unmarshal(payload []byte)

type ConnectionRegisterResp

type ConnectionRegisterResp struct {
	Success   bool
	Subdomain string
	Message   string
}

func (*ConnectionRegisterResp) Marshal

func (c *ConnectionRegisterResp) Marshal() *Message

func (*ConnectionRegisterResp) Unmarshal

func (c *ConnectionRegisterResp) Unmarshal(payload []byte)

type EndConnection

type EndConnection struct {
	Subdomain string
}

func (*EndConnection) Marshal

func (e *EndConnection) Marshal() *Message

Marshal converts an EndConnection to a byte slice.

func (*EndConnection) Unmarshal

func (e *EndConnection) Unmarshal(payload []byte)

Unmarshal converts a byte slice to an EndConnection.

type ErrorMessage

type ErrorMessage struct {
	Message string
}

func NewErrorMessage

func NewErrorMessage(message string) *ErrorMessage

func (*ErrorMessage) Marshal

func (e *ErrorMessage) Marshal() *Message

func (*ErrorMessage) Unmarshal

func (e *ErrorMessage) Unmarshal(payload []byte)

type Heartbeat

type Heartbeat struct {
	Message string
}

func (*Heartbeat) Marshal

func (h *Heartbeat) Marshal() *Message

func (*Heartbeat) Unmarshal

func (h *Heartbeat) Unmarshal(payload []byte)

type Message

type Message struct {
	Type    MessageType
	Length  uint32
	Payload []byte
}

Message represents a protocol message.

func ReadMessage

func ReadMessage(r io.Reader) (int, *Message, error)

ReadMessage reads a message from the given reader.

func (*Message) Write

func (m *Message) Write(w io.Writer) (int, error)

Write writes the message to the given writer.

type MessageType

type MessageType int
const (

	// Registration messages
	// These messages are used to register a connection with the server.
	MessageConnectionRegister     MessageType = 1
	MessageConnectionRegisterResp MessageType = 2

	// Maintenance messages
	// These messages are used to maintain the connection with the server.
	MessageDisconnect MessageType = 3
	MessageHeartbeat  MessageType = 4
	MessageError      MessageType = 5

	// Data messages
	// These messages are used to open and close streams of data.
	MessageBeginStream     MessageType = 6
	MessageEndStream       MessageType = 7
	MessageConnectionReady MessageType = 8
)

func (MessageType) String

func (t MessageType) String() string

type Parsable

type Parsable interface {
	Marshal() *Message
	Unmarshal([]byte)
}

type Protocol

type Protocol string
const (
	HTTP Protocol = "http"
	TCP  Protocol = "tcp"
)

func ProtocolFromByte

func ProtocolFromByte(b byte) Protocol

func (Protocol) Byte

func (p Protocol) Byte() byte

func (Protocol) Valid

func (p Protocol) Valid() bool

Jump to

Keyboard shortcuts

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