server

package
v0.0.0-...-101e1e6 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2021 License: GPL-3.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrServerClosed = errors.New("server closed")

ErrServerClosed will throw when server closed

Functions

This section is empty.

Types

type Action

type Action int

Action is control server or connection next action

const (
	NothingAction       Action = iota // nothing to do
	DisconnectionAction               // this action will close connection
	StopServerAction                  // this action will stop server
)

type Address

type Address struct {
}

type Codec

type Codec interface {
	// Encode will encode data before write to client
	Encode([]byte) []byte
	// Decode will decode receive data from client
	Decode(buf buffer.Buffer) []byte
}

Codec will Decode receive data from client and Encode send data before write to client

type Connection

type Connection interface {
	Send(data []byte, withoutEncode bool) error
	Remote() string
	Local() string
	Logger() logger.Logger
}

type Handler

type Handler interface {

	// OnConnected will call when client connected
	// the result action can control server
	OnConnected(conn Connection) (Action, error)

	// OnDisconnected will call when client disconnected
	OnDisconnected(conn Connection) error

	// OnReceived will call when read data from client
	// If Codec is config, decode data(frame) will be input when frame not nil
	// the result action can control server
	OnReceived(frame []byte, conn Connection) (Action, error)

	// OnError will call when any error occurred
	// the result action can control server
	OnError(conn Connection, err error) Action
}

Handler is handler of client read or write data The Action of method returned can control server or connection When Action is StopServerAction: the server will stop When Action is DisconnectionAction: the connection will be close

type NothingCodec

type NothingCodec struct {
}

func (*NothingCodec) Decode

func (n *NothingCodec) Decode(buf buffer.Buffer) []byte

func (*NothingCodec) Encode

func (n *NothingCodec) Encode(b []byte) []byte

type Option

type Option func(options *Options)

func WithCodec

func WithCodec(codec Codec) Option

WithCodec is edit Options Codec field

func WithLogger

func WithLogger(logger logger.Logger) Option

WithLogger is edit Options Logger field

func WithTask

func WithTask(task Task) Option

WithTask is edit Options Task field

type Options

type Options struct {
	// Logger is Logger implements
	Logger logger.Logger
	// Task is Task implements function
	// This function will call when server started
	Task Task
	// Codec is Codec implements
	// All data receive and send will use Codec Encode and Decode
	Codec Codec
}

Options defined server options

type Server

type Server interface {
	// Bind is bind address and handler to server
	Bind(address *Address, handler Handler) error

	// Stop will stop input addresses
	// When addresses empty, the server will stop all bind address
	Stop(addresses ...*Address) error

	// Start is start the server and blocking.
	// When server all addresses stop, will throw ErrServerClosed
	Start() error
}

Server is multi address handler server

type Task

type Task func() (time.Duration, Action)

Jump to

Keyboard shortcuts

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