socket

package module
v0.0.0-...-fab27cb Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2023 License: Apache-2.0 Imports: 6 Imported by: 0

README

socket

A simple TCP framework

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidCodec     = errors.New("invalid codec callback")
	ErrInvalidOnMessage = errors.New("invalid on message callback")
)

Functions

func New

func New(addr *net.TCPAddr) (*server, error)

Types

type Codec

type Codec interface {
	Decode([]byte) (Message, error)
	Encode(Message) ([]byte, error)
}

Codec is the interface for message coder and decoder. Application programmer can define a custom codec themselves.

type Conn

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

Conn represents a client connection to a TCP server.

func NewConn

func NewConn(conn *net.TCPConn, opt ...Option) (*Conn, error)

NewConn returns a new client connection which has not started to serve requests yet.

func (*Conn) Addr

func (c *Conn) Addr() net.Addr

func (*Conn) Run

func (c *Conn) Run(ctx context.Context) error

Run run the connection, creating two go-routines for reading and writing.

func (*Conn) Write

func (c *Conn) Write(message Message) error

Write write []byte to the Conn.

type Handler

type Handler interface {
	Handle(conn *net.TCPConn)
}

type Logger

type Logger interface {
	Debug(args ...interface{})
	Info(args ...interface{})
	Warn(args ...interface{})
	Error(args ...interface{})
}

Logger is our contract for the logger

type Message

type Message interface {
	Length() int
	Body() []byte
}

type Option

type Option func(*options)

Option sets server options.

func BufferSizeOption

func BufferSizeOption(indicator int) Option

HeartbeatOption returns a Option that is the size of buffered channel, for example an indicator of defaultBufferSize32 means a size of 256.

func CustomCodecOption

func CustomCodecOption(codec Codec) Option

CustomCodecOption returns a Option that will apply a custom Codec.

func HeartbeatOption

func HeartbeatOption(heartbeat time.Duration) Option

HeartbeatOption returns a Option that is the size of buffered channel, for example an indicator of defaultBufferSize32 means a size of 256.

func MessageMaxSize

func MessageMaxSize(size int) Option

MessageMaxSize returns a Option that will set buffer to receive message when new []byte arrived.

func OnErrorOption

func OnErrorOption(cb func(error) bool) Option

OnErrorOption returns a Option that will set callback to call when error occurs.

func OnMessageOption

func OnMessageOption(cb func(Message) error) Option

OnMessageOption returns a Option that will set callback to call when new message arrived.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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