muxer

package
v0.103.4 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2024 License: Apache-2.0 Imports: 8 Imported by: 1

Documentation

Overview

Package muxer implements the muxer/demuxer that allows multiple mini-protocols to run over a single connection.

It's not generally intended for this package to be used outside of this library, but it's possible to use it to do more advanced things than the library interface allows for.

Index

Constants

View Source
const ProtocolUnknown uint16 = 0xabcd

Magic number chosen to represent unknown protocols

View Source
const SegmentMaxPayloadLength = 65535

Maximum segment payload length

Variables

This section is empty.

Functions

This section is empty.

Types

type DiffusionMode

type DiffusionMode int

DiffusionMode is an enum for the valid muxer difficusion modes

const (
	DiffusionModeNone                  DiffusionMode = 0 // Default (invalid) diffusion mode
	DiffusionModeInitiator             DiffusionMode = 1 // Initiator-only (client) diffusion mode
	DiffusionModeResponder             DiffusionMode = 2 // Responder-only (server) diffusion mode
	DiffusionModeInitiatorAndResponder DiffusionMode = 3 // Initiator and responder (full duplex) mode
)

Diffusion modes

type Muxer

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

Muxer wraps a connection to allow running multiple mini-protocols over a single connection

func New

func New(conn net.Conn) *Muxer

New creates a new Muxer object and starts the read loop

func (*Muxer) ErrorChan

func (m *Muxer) ErrorChan() chan error

func (*Muxer) RegisterProtocol

func (m *Muxer) RegisterProtocol(
	protocolId uint16,
	protocolRole ProtocolRole,
) (chan *Segment, chan *Segment, chan bool)

RegisterProtocol registers the provided protocol ID with the muxer. It returns a channel for sending, a channel for receiving, and a channel to know when the muxer is shutting down. If the muxer is shutting down, this function will return nil values.

func (*Muxer) Send

func (m *Muxer) Send(msg *Segment) error

Send takes a populated Segment and writes it to the connection. A mutex is used to prevent more than one protocol from sending at once

func (*Muxer) SetDiffusionMode

func (m *Muxer) SetDiffusionMode(diffusionMode DiffusionMode)

SetDiffusionMode sets the muxer diffusion mode after the handshake completes

func (*Muxer) Start

func (m *Muxer) Start()

Start unblocks the read loop after the initial handshake to allow it to start processing messages

func (*Muxer) Stop

func (m *Muxer) Stop()

Stop shuts down the muxer

func (*Muxer) UnregisterProtocol added in v0.83.0

func (m *Muxer) UnregisterProtocol(
	protocolId uint16,
	protocolRole ProtocolRole,
)

type ProtocolRole added in v0.42.0

type ProtocolRole uint

ProtocolRole is an enum of the protocol roles

const (
	ProtocolRoleNone      ProtocolRole = 0 // Default (invalid) protocol role
	ProtocolRoleInitiator ProtocolRole = 1 // Initiator (client) protocol role
	ProtocolRoleResponder ProtocolRole = 2 // Responder (server) protocol role
)

Protocol roles

type Segment

type Segment struct {
	SegmentHeader
	Payload []byte
}

Segment represents basic unit of data in the Ouroboros protocol.

Each chunk of data exchanged by a particular mini-protocol is wrapped in a muxer segment. A segment consists of 4 bytes containing a timestamp, 2 bytes indicating which protocol the data is part of, 2 bytes indicating the size of the payload (up to 65535 bytes), and then the actual payload

func NewSegment

func NewSegment(protocolId uint16, payload []byte, isResponse bool) *Segment

NewSegment returns a new Segment given a protocol ID, payload bytes, and whether the segment is a response

type SegmentHeader

type SegmentHeader struct {
	Timestamp     uint32
	ProtocolId    uint16
	PayloadLength uint16
}

SegmentHeader represents the header bytes on a segment

func (*SegmentHeader) GetProtocolId

func (s *SegmentHeader) GetProtocolId() uint16

GetProtocolId returns the protocol ID of the segment

func (*SegmentHeader) IsRequest

func (s *SegmentHeader) IsRequest() bool

IsRequest returns true if the segment is not a response

func (*SegmentHeader) IsResponse

func (s *SegmentHeader) IsResponse() bool

IsResponse returns true if the segment is a response

Jump to

Keyboard shortcuts

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