core

package
v0.0.0-...-242f3d8 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2020 License: Apache-2.0 Imports: 8 Imported by: 10

Documentation

Index

Constants

View Source
const (
	// TCPServBuilder represents tcp server builder
	TCPServBuilder = "s_tcp"

	// WebsocketServBuilder represents websocket server builder
	WebsocketServBuilder = "s_websocket"

	// UDPServBuilder represents udp server builder
	UDPServBuilder = "s_udp"
)
View Source
const (
	// RetryMaxWaitSec represents the max wait time for retry reconnect.
	RetryMaxWaitSec = 120
)

Variables

View Source
var (
	// ErrWriteMsgQueueFull represents an error that the reserved queue is full.
	ErrWriteMsgQueueFull = errors.New("write queue is full")

	// ErrConnLost connection lost
	ErrConnLost = errors.New("connection lost")
)

Functions

func Register

func Register(b Builder)

Register registers the transport(e.g. TcpAcceptor) builder to the transport map. b.Name (lowercased) will be used as the name registered with this builder.

NOTE: this function must only be called during initialization time (i.e. in an init() function), and is not thread-safe. If multiple Acceptor are registered with the same name, the one registered last will take effect.

Types

type Acceptor

type Acceptor struct {
	sync.RWMutex
	*BaseChannel
	// contains filtered or unexported fields
}

Acceptor represents a simple base server for share code.

func NewAcceptor

func NewAcceptor() *Acceptor

NewAcceptor create a Acceptor instance which can accept new connection from client.

func (*Acceptor) Broadcast

func (acceptor *Acceptor) Broadcast(msg interface{}) error

Broadcast broadcasts message to all client channels. Just a simple wrapper for connection manager. For better performance, should only do FilterPipeline once for all Channel and call RawConn().Write(msg) for less filter operations and less memory cost.

func (*Acceptor) FireConnect

func (acceptor *Acceptor) FireConnect(channel Channel) InboundInvoker

FireConnect fires a Connect event.

func (*Acceptor) FireDisconnect

func (acceptor *Acceptor) FireDisconnect() InboundInvoker

FireDisconnect fires a Disconnect event.

func (*Acceptor) FireError

func (acceptor *Acceptor) FireError(err error) InboundInvoker

func (*Acceptor) FireEvent

func (acceptor *Acceptor) FireEvent(event interface{}) InboundInvoker

FireEvent fires a event.

func (*Acceptor) FireRead

func (acceptor *Acceptor) FireRead(msg interface{}) InboundInvoker

FireRead fires a read event.

func (*Acceptor) InitSubChannel

func (acceptor *Acceptor) InitSubChannel(sub InitChannelCb)

InitSubChannel sets the callback when create a new SubChannel to init it.

func (*Acceptor) Multicast

func (acceptor *Acceptor) Multicast(msg interface{}, channelIDs []interface{}) error

Multicast sends message to specified channels.

func (*Acceptor) SubChannelInitializer

func (acceptor *Acceptor) SubChannelInitializer() InitChannelCb

func (*Acceptor) SubChannels

func (acceptor *Acceptor) SubChannels() []SubChannel

SubChannels returns all SubChannels belong to this AcceptorChannel.

type AcceptorChannel

type AcceptorChannel interface {
	Channel
	InboundInvoker
	ChannelMgr

	// InitSubChannel set the InitChannel function which defines the behaviors when new sub channel created.
	InitSubChannel(sub InitChannelCb)

	SubChannelInitializer() InitChannelCb

	// Listen announces on the local network address.
	Listen(addr net.Addr)

	// Accept accepts the next incoming call
	Accept()
}

AcceptorChannel represents a server-side socket.

type AttrMap

type AttrMap struct {
	sync.Map
}

AttrMap represents a map-like data structure which is safe for concurrent use by multiple goroutines without additional locking or coordination. currently it just a wrapper of sync.Map.

func NewAttrMap

func NewAttrMap() *AttrMap

NewAttrMap returns a pointer of AttrMap instance.

func (*AttrMap) Int32Value

func (attr *AttrMap) Int32Value(key string) int32

Int32Value helper method to return a int32 value by key.

func (*AttrMap) Int64Value

func (attr *AttrMap) Int64Value(key string) int64

Int64Value helper method to return a int64 value by key.

func (*AttrMap) IntValue

func (attr *AttrMap) IntValue(key string) int

IntValue helper method to return a int value by key.

func (*AttrMap) SetIfAbsent

func (attr *AttrMap) SetIfAbsent(key string, value interface{}) interface{}

SetIfAbsent Atomically sets to the given value if this value is not set and return nil. If it contains a value it will just return the old value and do nothing.

func (*AttrMap) SetValue

func (attr *AttrMap) SetValue(key string, value interface{})

SetValue sets a key with value.

func (*AttrMap) StringValue

func (attr *AttrMap) StringValue(key string) string

StringValue helper method to return a string value by key.

func (*AttrMap) Uint32Value

func (attr *AttrMap) Uint32Value(key string) uint32

Uint32Value helper method to return a uint32 value by key.

func (*AttrMap) Uint64Value

func (attr *AttrMap) Uint64Value(key string) uint64

Uint64Value helper method to return a uint64 value by key.

func (*AttrMap) Value

func (attr *AttrMap) Value(key string) interface{}

Value returns the value by key.

type BaseChannel

type BaseChannel struct {
	AttrMap
	// contains filtered or unexported fields
}

BaseChannel represents a basic implementation of Channel. it is used as the super class of other Channel.

func NewBaseChannel

func NewBaseChannel(sub Channel) *BaseChannel

NewBaseChannel creates a BaseChannel instance.

func (*BaseChannel) Attr

func (bc *BaseChannel) Attr() *AttrMap

Attr returns the AttrMap which contains a sets of attrs.

func (*BaseChannel) Close

func (bc *BaseChannel) Close()

func (*BaseChannel) ID

func (bc *BaseChannel) ID() interface{}

func (*BaseChannel) LocalAddr

func (bc *BaseChannel) LocalAddr() net.Addr

LocalAddr returns the local addr.

func (*BaseChannel) Pipeline

func (bc *BaseChannel) Pipeline() ChannelPipeline

Pipeline returns the ChannelPipeline.

func (*BaseChannel) RawConn

func (bc *BaseChannel) RawConn() RawConn

func (*BaseChannel) RemoteAddr

func (bc *BaseChannel) RemoteAddr() net.Addr

RemoteAddr return the opposite side addr.

func (*BaseChannel) Write

func (bc *BaseChannel) Write(msg interface{}, extra ...interface{}) error

type BuildOption

type BuildOption func(interface{})

BuildOption represents transport builder option.

type Builder

type Builder interface {
	Build(opts ...BuildOption) AcceptorChannel
	Name() string
}

Builder builds transport.

func GetAcceptorBuilder

func GetAcceptorBuilder(n string) Builder

GetAcceptorBuilder returns acceptor builder by name, ignore case.

type Channel

type Channel interface {
	ID() interface{}

	// Attr returns the AttrMap.
	// each channel contains a AttrMap
	Attr() *AttrMap

	// Write writes message to opposite side.
	Write(msg interface{}, extra ...interface{}) error

	// Close close the connection
	Close()

	// Pipeline returns the ChannelPipeline.
	Pipeline() ChannelPipeline

	// LocalAddr returns the local addr.
	LocalAddr() net.Addr

	// RemoteAddr return the opposite side addr.
	RemoteAddr() net.Addr

	RawConn() RawConn
}

Channel is a nexus to a network socket or a component which is capable of I/O operations such as read, write, connect, and close.

type ChannelContext

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

ChannelContext enables a ChannelHandler to interact with its ChannelPipeline and other handlers. Among other things a handler can notify the next ChannelHandler in the ChannelPipeline. a ChannelContext is an InboundInvoker or an OutboundInvoker or both.

func (*ChannelContext) Attr

func (ctx *ChannelContext) Attr() *AttrMap

func (*ChannelContext) Channel

func (ctx *ChannelContext) Channel() Channel

func (*ChannelContext) Close

func (ctx *ChannelContext) Close() OutboundInvoker

func (*ChannelContext) Executor

func (ctx *ChannelContext) Executor() Executor

func (*ChannelContext) FireConnect

func (ctx *ChannelContext) FireConnect(channel Channel) InboundInvoker

func (*ChannelContext) FireDisconnect

func (ctx *ChannelContext) FireDisconnect() InboundInvoker

func (*ChannelContext) FireError

func (ctx *ChannelContext) FireError(err error) InboundInvoker

func (*ChannelContext) FireEvent

func (ctx *ChannelContext) FireEvent(event interface{}) InboundInvoker

func (*ChannelContext) FireRead

func (ctx *ChannelContext) FireRead(msg interface{}) InboundInvoker

func (*ChannelContext) FireWrite

func (ctx *ChannelContext) FireWrite(msg interface{}) OutboundInvoker

func (*ChannelContext) Name

func (ctx *ChannelContext) Name() string

func (*ChannelContext) Write

func (ctx *ChannelContext) Write(msg interface{}) OutboundInvoker

type ChannelIDGenerator

type ChannelIDGenerator interface {
	GenID() interface{}
}

type ChannelMgr

type ChannelMgr interface {
	// SubChannels returns all SubChannels belong to this AcceptorChannel.
	SubChannels() []SubChannel

	// Broadcast broadcasts message to all client channels.
	Broadcast(msg interface{}) error

	// Multicast sends message to specified channels.
	Multicast(msg interface{}, channelIDs []interface{}) error
}

ChannelMgr represents a manager of Channel.

type ChannelPipeline

type ChannelPipeline interface {
	InboundInvoker
	OutboundInvoker

	AddFirst(executor Executor, name string, h interface{})
	AddLast(executor Executor, name string, h interface{})
	AddAfter(afterName string, executor Executor, name string, handler interface{})
	AddBefore(beforeName string, executor Executor, name string, handler interface{})
	Channel() Channel
}

ChannelPipeline is a list of ChannelHandler which handles or intercepts inbound events and outbound operations of a ChannelPipeline implements an advanced form of the Intercepting Filter pattern to give a user full control over how an event is handled and how the ChannelHandler in a pipeline interact with each other. One ChannelPipeline per Channel.

func NewChannelPipeline

func NewChannelPipeline(channel Channel) ChannelPipeline

NewChannelPipeline creates a new ChannelPipeline instance.

type Connector

type Connector struct {
	*BaseChannel
	// contains filtered or unexported fields
}

func NewConnector

func NewConnector() *Connector

func (*Connector) Close

func (c *Connector) Close()

func (*Connector) FireConnect

func (c *Connector) FireConnect(channel Channel) InboundInvoker

func (*Connector) FireDisconnect

func (c *Connector) FireDisconnect() InboundInvoker

func (*Connector) FireError

func (c *Connector) FireError(err error) InboundInvoker

func (*Connector) FireEvent

func (c *Connector) FireEvent(event interface{}) InboundInvoker

func (*Connector) FireRead

func (c *Connector) FireRead(msg interface{}) InboundInvoker

func (*Connector) InitSubChannel

func (c *Connector) InitSubChannel(sub InitChannelCb)

func (*Connector) LocalAddr

func (c *Connector) LocalAddr() net.Addr

LocalAddr returns the local addr.

func (*Connector) RemoteAddr

func (c *Connector) RemoteAddr() net.Addr

RemoteAddr return the opposite side addr.

func (*Connector) SubChannelInitializer

func (c *Connector) SubChannelInitializer() InitChannelCb

func (*Connector) Write

func (c *Connector) Write(msg interface{}, extra ...interface{}) error

type ConnectorChannel

type ConnectorChannel interface {
	Channel
	InboundInvoker

	// InitSubChannel set the InitChannel function which defines the behaviors when new sub channel created.
	InitSubChannel(sub InitChannelCb)

	SubChannelInitializer() InitChannelCb

	// Connect connects to the special address.
	Connect(addr interface{}) (SubChannel, error)
}

ConnectorChannel respresents a client-side socket.

type DefaultChannelContext

type DefaultChannelContext struct {
	*ChannelContext
	// contains filtered or unexported fields
}

func NewDefaultChannelContext

func NewDefaultChannelContext(executor Executor, name string, pipeline ChannelPipeline, handler interface{}) *DefaultChannelContext

type DefaultInboundHandler

type DefaultInboundHandler struct{}

func NewDefaultInboundHandler

func NewDefaultInboundHandler() *DefaultInboundHandler

func (*DefaultInboundHandler) OnConnect

func (ih *DefaultInboundHandler) OnConnect(ctx *ChannelContext, channel Channel)

func (*DefaultInboundHandler) OnDisconnect

func (ih *DefaultInboundHandler) OnDisconnect(ctx *ChannelContext)

func (*DefaultInboundHandler) OnError

func (ih *DefaultInboundHandler) OnError(ctx *ChannelContext, err error)

func (*DefaultInboundHandler) OnEvent

func (ih *DefaultInboundHandler) OnEvent(ctx *ChannelContext, event interface{})

func (*DefaultInboundHandler) OnRead

func (ih *DefaultInboundHandler) OnRead(ctx *ChannelContext, msg interface{})

type DefaultOutboundHandler

type DefaultOutboundHandler struct{}

func NewDefaultOutboundHandler

func NewDefaultOutboundHandler() *DefaultOutboundHandler

func (*DefaultOutboundHandler) OnWrite

func (oh *DefaultOutboundHandler) OnWrite(ctx *ChannelContext, msg interface{})

type DefaultSubChannel

type DefaultSubChannel struct {
	*BaseChannel
	sync.Mutex
	// contains filtered or unexported fields
}

DefaultSubChannel represents a default implementation of SubChannel. When a new connection conntect to ServreChannel, a corresponding

SubChannel will be created.

func (*DefaultSubChannel) Close

func (dsc *DefaultSubChannel) Close()

Close closes the connection.

func (*DefaultSubChannel) FireConnect

func (dsc *DefaultSubChannel) FireConnect(channel Channel) InboundInvoker

FireConnect fires a connect event. Nothing to do for a SubChannel.

func (*DefaultSubChannel) FireDisconnect

func (dsc *DefaultSubChannel) FireDisconnect() InboundInvoker

FireDisconnect fires a disconnect event.

func (*DefaultSubChannel) FireError

func (dsc *DefaultSubChannel) FireError(err error) InboundInvoker

func (*DefaultSubChannel) FireEvent

func (dsc *DefaultSubChannel) FireEvent(event interface{}) InboundInvoker

func (*DefaultSubChannel) FireRead

func (dsc *DefaultSubChannel) FireRead(msg interface{}) InboundInvoker

FireRead fires a read event.

func (*DefaultSubChannel) GracefullyClose

func (dsc *DefaultSubChannel) GracefullyClose()

func (*DefaultSubChannel) LocalAddr

func (dsc *DefaultSubChannel) LocalAddr() net.Addr

LocalAddr returns the local addr.

func (*DefaultSubChannel) RawConn

func (dsc *DefaultSubChannel) RawConn() RawConn

RawConn returns the raw connection.

func (*DefaultSubChannel) RemoteAddr

func (dsc *DefaultSubChannel) RemoteAddr() net.Addr

RemoteAddr return the opposite side addr.

func (*DefaultSubChannel) Write

func (dsc *DefaultSubChannel) Write(msg interface{}, extra ...interface{}) (err error)

Write writes message to opposite side.

type Executor

type Executor interface {
	Execute(task func())
}

type HeadContext

type HeadContext struct {
	*ChannelContext
}

HeadContext the header of pipeline.

func NewHeadContext

func NewHeadContext(pipeline ChannelPipeline) *HeadContext

NewHeadContext return a new instance of *HeadContext.

func (*HeadContext) OnConnect

func (hctx *HeadContext) OnConnect(ctx *ChannelContext, channel Channel)

OnConnect processes a connect event.

func (*HeadContext) OnDisconnect

func (hctx *HeadContext) OnDisconnect(ctx *ChannelContext)

OnDisconnect processes a disconnect event.

func (*HeadContext) OnError

func (hctx *HeadContext) OnError(ctx *ChannelContext, err error)

func (*HeadContext) OnEvent

func (hctx *HeadContext) OnEvent(ctx *ChannelContext, event interface{})

func (*HeadContext) OnRead

func (hctx *HeadContext) OnRead(ctx *ChannelContext, msg interface{})

OnRead processes a read event.

func (*HeadContext) OnWrite

func (hctx *HeadContext) OnWrite(ctx *ChannelContext, msg interface{})

OnWrite processes a write event.

type InboundHandler

type InboundHandler interface {
	// OnConnect called when a new channel connected.
	OnConnect(ctx *ChannelContext, channel Channel)

	// OnDisconnect called when a channel disconnected.
	OnDisconnect(ctx *ChannelContext)

	// OnRead called when reads new data.
	OnRead(ctx *ChannelContext, msg interface{})

	// OnRead called when event triggered.
	OnEvent(ctx *ChannelContext, event interface{})

	// OnError called when error occurred
	OnError(ctx *ChannelContext, err error)
}

InboundHandler processes all inbound event.

type InboundInvoker

type InboundInvoker interface {
	// FireConnect fire a connect event when new channel created.
	FireConnect(newChannel Channel) InboundInvoker

	// FireDisconnect fire a disconnect event when a channel destoryed.
	FireDisconnect() InboundInvoker

	// FireRead fire a read event when new data comes.
	FireRead(msg interface{}) InboundInvoker

	FireEvent(event interface{}) InboundInvoker

	FireError(err error) InboundInvoker
}

InboundInvoker invokes inbound event handler.

type InitChannelCb

type InitChannelCb func(channel SubChannel)

InitChannelCb defines the behaviors when AcceptorChannel creates a new sub channel.

type OutboundHandler

type OutboundHandler interface {
	// OnWrite calls when write new data.
	OnWrite(ctx *ChannelContext, msg interface{})
}

OutboundHandler processes all outbound event.

type OutboundInvoker

type OutboundInvoker interface {
	// FireWrite fire a write event when send msg to channel.
	FireWrite(msg interface{}) OutboundInvoker
}

OutboundInvoker invokes outbound event handler.

type RawConn

type RawConn interface {
	Read(buf bytes.ReadOnlyBuffer) error
	Write(data []byte)

	// Close closes the connection.
	// Any blocked Read or Write operations will be unblocked and return errors.
	Close() error

	// LocalAddr returns the local network address.
	LocalAddr() net.Addr

	// RemoteAddr returns the remote network address.
	RemoteAddr() net.Addr

	SetConn(conn net.Conn)
}

type ReconnectOpts

type ReconnectOpts struct {
	AutoReconnect     bool
	MaxReconnectTimes int
}

type SubChannel

type SubChannel interface {
	Channel
	InboundInvoker

	// GracefullyClose closes gracefully with all message sent before close.
	GracefullyClose()
}

SubChannel represents a server-side connection connected by a client.

func NewDefaultSubChannel

func NewDefaultSubChannel(conn RawConn, readBufSize, writeBufSize int, reconnOpts ...*ReconnectOpts) SubChannel

NewDefaultSubChannel returns a new instance of SubChannel The same time, the subchannel will start the read loop and write loop to serve reading message and writting message.

type TailContext

type TailContext struct {
	*ChannelContext
}

TailContext represents the tail of pipeline.

func NewTailContext

func NewTailContext(pipeline ChannelPipeline) *TailContext

NewTailContext returns a new instance of *TailContext.

func (*TailContext) OnConnect

func (tctx *TailContext) OnConnect(ctx *ChannelContext, channel Channel)

OnConnect do nothing to stop the pipeline.

func (*TailContext) OnDisconnect

func (tctx *TailContext) OnDisconnect(ctx *ChannelContext)

OnDisconnect do nothing to stop the pipeline.

func (*TailContext) OnError

func (tctx *TailContext) OnError(ctx *ChannelContext, err error)

func (*TailContext) OnEvent

func (hctx *TailContext) OnEvent(ctx *ChannelContext, event interface{})

func (*TailContext) OnRead

func (tctx *TailContext) OnRead(ctx *ChannelContext, msg interface{})

OnRead do nothing to stop the pipeline.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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