netcode

package
v3.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ErrCodecNotFound = ServerError("codec not found")
	ErrDecodeFailed  = ServerError("decode failed")
	ErrInternal      = ServerError("internal error")
)

Variables

This section is empty.

Functions

func Start

func Start(port int, path string, onSocket func(w http.ResponseWriter, r *http.Request, c *Conn), o *AcceptOptions)

Types

type AcceptOptions

type AcceptOptions = websocket.AcceptOptions

type Beacon

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

func CreateBeacon

func CreateBeacon(socket *Socket, interval time.Duration, callback func(time.Duration)) *Beacon

func (*Beacon) Destroy

func (b *Beacon) Destroy()

func (*Beacon) Start

func (b *Beacon) Start()

func (*Beacon) Stop

func (b *Beacon) Stop()

type BinaryEncoder

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

func CreateBinaryEncoder

func CreateBinaryEncoder(codecs []*RegisteredCodec, idCodec Codec) *BinaryEncoder

func (BinaryEncoder) Decode

func (e BinaryEncoder) Decode(data []byte) (message *Message, err error)

func (BinaryEncoder) Encode

func (e BinaryEncoder) Encode(message *Message) ([]byte, error)

type BooleanCodec

type BooleanCodec struct {
}

func (BooleanCodec) Decode

func (c BooleanCodec) Decode(buffer *bytes.Buffer) any

func (BooleanCodec) Encode

func (c BooleanCodec) Encode(buffer *bytes.Buffer, data any)

type Codec

type Codec interface {
	Encode(buffer *bytes.Buffer, data any)
	Decode(buffer *bytes.Buffer) any
}

type Conn

type Conn = websocket.Conn

type Float32Codec

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

func (Float32Codec) Decode

func (c Float32Codec) Decode(buffer *bytes.Buffer) any

func (Float32Codec) Encode

func (c Float32Codec) Encode(buffer *bytes.Buffer, data any)

type Float64Codec

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

func (Float64Codec) Decode

func (c Float64Codec) Decode(buffer *bytes.Buffer) any

func (Float64Codec) Encode

func (c Float64Codec) Encode(buffer *bytes.Buffer, data any)

type FloatPrecisionCodec

type FloatPrecisionCodec[F float32 | float64, T int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64] struct {
	// contains filtered or unexported fields
}

func CreateFloatPrecisionCodec

func CreateFloatPrecisionCodec[F float32 | float64, T int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64](codec Codec, precision int) *FloatPrecisionCodec[F, T]

func (*FloatPrecisionCodec[F, T]) Decode

func (c *FloatPrecisionCodec[F, T]) Decode(buffer *bytes.Buffer) any

func (*FloatPrecisionCodec[F, T]) Encode

func (c *FloatPrecisionCodec[F, T]) Encode(buffer *bytes.Buffer, data any)

type Int8Codec

type Int8Codec struct {
}

func (Int8Codec) Decode

func (c Int8Codec) Decode(buffer *bytes.Buffer) any

func (Int8Codec) Encode

func (c Int8Codec) Encode(buffer *bytes.Buffer, data any)

type Int16Codec

type Int16Codec struct {
}

func (Int16Codec) Decode

func (c Int16Codec) Decode(buffer *bytes.Buffer) any

func (Int16Codec) Encode

func (c Int16Codec) Encode(buffer *bytes.Buffer, data any)

type Int32Codec

type Int32Codec struct {
}

func (Int32Codec) Decode

func (c Int32Codec) Decode(buffer *bytes.Buffer) any

func (Int32Codec) Encode

func (c Int32Codec) Encode(buffer *bytes.Buffer, data any)

type Int64Codec

type Int64Codec struct {
}

func (Int64Codec) Decode

func (c Int64Codec) Decode(buffer *bytes.Buffer) any

func (Int64Codec) Encode

func (c Int64Codec) Encode(buffer *bytes.Buffer, data any)

type Message

type Message struct {
	Name string
	Data any
}

type NullCodec

type NullCodec struct {
}

func (NullCodec) Decode

func (c NullCodec) Decode(buffer *bytes.Buffer) any

func (NullCodec) Encode

func (c NullCodec) Encode(buffer *bytes.Buffer, data any)

type RegisteredCodec

type RegisteredCodec struct {
	Id      uint8
	Name    string
	Handler Codec
}

type ServerError

type ServerError string

func (ServerError) Error

func (err ServerError) Error() string

type Socket

type Socket struct {
	ID      uint
	Conn    *websocket.Conn
	Encoder *BinaryEncoder
	In      chan SocketMessage
	// contains filtered or unexported fields
}

func CreateSocket

func CreateSocket(id uint, c *websocket.Conn, encoder *BinaryEncoder, in chan SocketMessage) *Socket

func (*Socket) Close

func (s *Socket) Close(code int, reason string) error

func (*Socket) CloseNow added in v3.2.0

func (s *Socket) CloseNow() error

CloseNow closes immediately, without a close handshake.

func (*Socket) Handle

func (s *Socket) Handle(data []byte)

func (*Socket) Ping

func (s *Socket) Ping() error

func (*Socket) PingContext added in v3.2.0

func (s *Socket) PingContext(ctx context.Context) error

PingContext waits for the pong, returning early if ctx is cancelled.

func (*Socket) Run

func (s *Socket) Run(onClose func(*Socket))

func (*Socket) Send

func (s *Socket) Send(message *Message)

func (*Socket) SetReadTimeout added in v3.2.0

func (s *Socket) SetReadTimeout(d time.Duration)

SetReadTimeout bounds how long Run waits for a message before closing the connection. Zero (the default) disables the deadline.

func (*Socket) Write

func (s *Socket) Write(data []byte)

type SocketMessage

type SocketMessage struct {
	Socket  *Socket
	Message *Message
	Err     error
}

type Sockets

type Sockets struct {
	Encoder *BinaryEncoder
	In      chan SocketMessage
	Out     chan *Socket
	List    map[uint]*Socket
	// contains filtered or unexported fields
}

func CreateSockets

func CreateSockets(encoder *BinaryEncoder, maxId uint, inBufferSize int) *Sockets

func (*Sockets) Add

func (ss *Sockets) Add(c *websocket.Conn) (*Socket, error)

func (*Sockets) Clear

func (ss *Sockets) Clear()

func (*Sockets) Count

func (ss *Sockets) Count() int

func (*Sockets) ForEach

func (ss *Sockets) ForEach(callback func(*Socket))

func (*Sockets) ForOther

func (ss *Sockets) ForOther(socket *Socket, callback func(*Socket))

func (*Sockets) Remove

func (ss *Sockets) Remove(socket *Socket)

func (*Sockets) Send

func (ss *Sockets) Send(socket *Socket, message *Message)

func (*Sockets) SendAll

func (ss *Sockets) SendAll(message *Message)

func (*Sockets) SendOther

func (ss *Sockets) SendOther(socket *Socket, message *Message)

func (*Sockets) SetReadTimeout added in v3.2.0

func (ss *Sockets) SetReadTimeout(d time.Duration)

SetReadTimeout sets the read deadline applied to sockets created after it. Zero (the default) disables the deadline.

func (*Sockets) WriteAll

func (ss *Sockets) WriteAll(data []byte)

func (*Sockets) WriteOther

func (ss *Sockets) WriteOther(socket *Socket, data []byte)

type StringCodec

type StringCodec struct {
}

func (StringCodec) Decode

func (c StringCodec) Decode(buffer *bytes.Buffer) any

func (StringCodec) Encode

func (c StringCodec) Encode(buffer *bytes.Buffer, data any)

type StringLongCodec

type StringLongCodec struct {
}

func (StringLongCodec) Decode

func (c StringLongCodec) Decode(buffer *bytes.Buffer) any

func (StringLongCodec) Encode

func (c StringLongCodec) Encode(buffer *bytes.Buffer, data any)

type UInt8Codec

type UInt8Codec struct {
}

* 8 bit unsigned Int codec (0 to 255)

func (UInt8Codec) Decode

func (c UInt8Codec) Decode(buffer *bytes.Buffer) any

func (UInt8Codec) Encode

func (c UInt8Codec) Encode(buffer *bytes.Buffer, data any)

type UInt16Codec

type UInt16Codec struct {
}

*

  • 16 bit unsigned Int codec (from 0 to 65535)

func (UInt16Codec) Decode

func (c UInt16Codec) Decode(buffer *bytes.Buffer) any

func (UInt16Codec) Encode

func (c UInt16Codec) Encode(buffer *bytes.Buffer, data any)

type UInt32Codec

type UInt32Codec struct {
}

*

  • 32 bit unsigned Int codec (from 0 to 4294967295)

func (UInt32Codec) Decode

func (c UInt32Codec) Decode(buffer *bytes.Buffer) any

func (UInt32Codec) Encode

func (c UInt32Codec) Encode(buffer *bytes.Buffer, data any)

type UInt64Codec

type UInt64Codec struct {
}

* 64 bit unsigned Int codec (0 to 18446744073709551615)

func (UInt64Codec) Decode

func (c UInt64Codec) Decode(buffer *bytes.Buffer) any

func (UInt64Codec) Encode

func (c UInt64Codec) Encode(buffer *bytes.Buffer, data any)

type UIntLongCodec

type UIntLongCodec struct {
	ByteLength int
}

func (UIntLongCodec) Decode

func (c UIntLongCodec) Decode(buffer *bytes.Buffer) any

func (UIntLongCodec) Encode

func (c UIntLongCodec) Encode(buffer *bytes.Buffer, data any)

Jump to

Keyboard shortcuts

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