netlib

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2023 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Overview

config

protocol

binary

Gbp

Gob

nil

encoding

error

handler

session

acceptor

connector

session

connector

Index

Constants

View Source
const (
	EncodingTypeNil = iota
	EncodingTypeGPB
	EncodingTypeBinary
	EncodingTypeGob
	EncodingTypeMax
)
View Source
const (
	InterestOps_Opened uint = iota
	InterestOps_Closed
	InterestOps_Idle
	InterestOps_Received
	InterestOps_Sent
	InterestOps_Max
)
View Source
const (
	DefaultMaxConnect = 1024
)
View Source
const (
	IoServiceMaxCount int = 10
)
View Source
const (
	RWBufRecyclerBacklog int = 128
)
View Source
const (
	ReconnectInterval time.Duration = 5 * time.Second
)

Variables

View Source
var (
	DefaultProtocoDecoderName = "default-protocol-decoder"
	SessionAttributeRcvBuf    = &RWBuffer{}
)
View Source
var (
	LenOfPacketHeader int
	LenOfProtoHeader  int
	MaxPacketSize     int = 1024
)
View Source
var (
	DefaultProtocolEncoderName    = "default-protocol-encoder"
	DefaultBuiltinProtocolEncoder = &DefaultProtocolEncoder{}
	SessionAttributeSndBuf        = &RWBuffer{}

	ErrSndBufCannotGet     = errors.New("Session sndbuf get failed")
	ErrExceedMaxPacketSize = errors.New("exceed max packet size")
)
View Source
var Config = Configuration{}
View Source
var (
	ConnectorMgr = &connectorMgr{
		pool: make(map[string]Connector),
	}
)
View Source
var ErrorTypeNotFit = errors.New("packet not proto.Message type")
View Source
var Gob = &GobEncDecoder{}
View Source
var Gpb = &GbpEncDecoder{}
View Source
var (
	NetModule = newNetEngine()
)
View Source
var Nil = &NilEncDecoder{}
View Source
var RWRecycler = recycler.NewRecycler(
	RWBufRecyclerBacklog,
	func() interface{} {
		rb := &RWBuffer{
			buf: make([]byte, 0, MaxPacketSize),
		}

		return rb
	},
	"rwbuf_recycler",
)
View Source
var (
	SendRoutinePoison *packet = nil
)

Functions

func AllocAction

func AllocAction() *action

func AllocPacket

func AllocPacket() *packet

func BenchmarkAllocAction

func BenchmarkAllocAction(b *testing.B)

func Connect

func Connect(sc *SessionConfig) error

func CreatePacket

func CreatePacket(packetId int) interface{}

func FreeAction

func FreeAction(a *action)

func FreePacket

func FreePacket(p *packet)

func FreeRWBuf

func FreeRWBuf(buf *RWBuffer)

func GetPacketTypeAndId

func GetPacketTypeAndId(pack interface{}) (int, int)

func Listen

func Listen(sc *SessionConfig) error

func MarshalPacket

func MarshalPacket(packetid int, pack interface{}) ([]byte, error)

func MarshalPacketNoPackId

func MarshalPacketNoPackId(pack interface{}) (data []byte, err error)

func RegisteEncoding

func RegisteEncoding(edtype int, ed EncDecoder, tt TypeTester)

func RegisteErrorPacketHandlerCreator

func RegisteErrorPacketHandlerCreator(name string, ephc ErrorPacketHandlerCreator)

func RegisteProtocolDecoder

func RegisteProtocolDecoder(name string, dec ProtocolDecoder)

func RegisteProtocolEncoder

func RegisteProtocolEncoder(name string, enc ProtocolEncoder)

func RegisteSessionFilterCreator

func RegisteSessionFilterCreator(filterName string, sfc SessionFilterCreator)

func RegisteSessionHandlerCreator

func RegisteSessionHandlerCreator(name string, shc SessionHandlerCreator)

func Register1ToMHandler

func Register1ToMHandler(h Handler, packetIds ...int)

func RegisterFactory

func RegisterFactory(packetId int, factory PacketFactory)

func RegisterHandler

func RegisterHandler(packetId int, h Handler)

func RegisterRangeHandler

func RegisterRangeHandler(start, end int, h Handler)

func SendStartNetIoService

func SendStartNetIoService(sc *SessionConfig) bool

func ShutConnector

func ShutConnector(ip string, port int)

func TestAllocAction

func TestAllocAction(t *testing.T)

func TestFreeAction

func TestFreeAction(t *testing.T)

func UnmarshalPacket

func UnmarshalPacket(data []byte) (int, interface{}, error)

func UnmarshalPacketNoPackId

func UnmarshalPacketNoPackId(data []byte, pck interface{}) error

Types

type Acceptor

type Acceptor interface {
	GetSessionConfig() *SessionConfig
	Addr() net.Addr
	// contains filtered or unexported methods
}

func GetAcceptors

func GetAcceptors() []Acceptor

type ActionPool

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

func NewActionPool

func NewActionPool(num int) *ActionPool

func (*ActionPool) Get

func (ap *ActionPool) Get() (a *action)

func (*ActionPool) Give

func (ap *ActionPool) Give(a *action)

type BasicSessionHandler

type BasicSessionHandler struct {
}

func (*BasicSessionHandler) OnPacketReceived

func (bsh *BasicSessionHandler) OnPacketReceived(s *Session, packetid int, packet interface{})

func (*BasicSessionHandler) OnPacketSent

func (bsh *BasicSessionHandler) OnPacketSent(s *Session, data []byte)

func (*BasicSessionHandler) OnSessionClosed

func (bsh *BasicSessionHandler) OnSessionClosed(s *Session)

func (*BasicSessionHandler) OnSessionIdle

func (bsh *BasicSessionHandler) OnSessionIdle(s *Session)

func (*BasicSessionHandler) OnSessionOpened

func (bsh *BasicSessionHandler) OnSessionOpened(s *Session)

type BinaryEncDecoder

type BinaryEncDecoder struct {
}

func (*BinaryEncDecoder) Marshal

func (this *BinaryEncDecoder) Marshal(pack interface{}) ([]byte, error)

func (*BinaryEncDecoder) Unmarshal

func (this *BinaryEncDecoder) Unmarshal(buf []byte, pack interface{}) error

type Configuration

type Configuration struct {
	SrvInfo    ServerInfo
	IoServices []SessionConfig
}

func (*Configuration) Close

func (c *Configuration) Close() error

func (*Configuration) Init

func (c *Configuration) Init() error

func (*Configuration) Name

func (c *Configuration) Name() string

type Connector

type Connector interface {
	GetSessionConfig() *SessionConfig
	// contains filtered or unexported methods
}

type DefaultProtocolDecoder

type DefaultProtocolDecoder struct {
}

func (*DefaultProtocolDecoder) Decode

func (pdi *DefaultProtocolDecoder) Decode(s *Session, r io.Reader) (packetid int, packet interface{}, err error, raw []byte)

func (*DefaultProtocolDecoder) FinishDecode

func (pdi *DefaultProtocolDecoder) FinishDecode(s *Session)

type DefaultProtocolEncoder

type DefaultProtocolEncoder struct {
	PacketCutor PacketCutSlicesFunc
}

func (*DefaultProtocolEncoder) CutAndSendPacket

func (dec *DefaultProtocolEncoder) CutAndSendPacket(s *Session, data []byte, w io.Writer) (err error)

func (*DefaultProtocolEncoder) Encode

func (dec *DefaultProtocolEncoder) Encode(s *Session, packetid int, packet interface{}, w io.Writer) (data []byte, err error)

func (*DefaultProtocolEncoder) FinishEncode

func (dec *DefaultProtocolEncoder) FinishEncode(s *Session)

type EncDecoder

type EncDecoder interface {
	Unmarshal(buf []byte, pack interface{}) error
	Marshal(pack interface{}) ([]byte, error)
}

type ErrorPacketHandler

type ErrorPacketHandler interface {
	OnErrorPacket(s *Session, packetid int, data []byte) bool //run in session receive goroutine
}

type ErrorPacketHandlerCreator

type ErrorPacketHandlerCreator func() ErrorPacketHandler

func GetErrorPacketHandlerCreator

func GetErrorPacketHandlerCreator(name string) ErrorPacketHandlerCreator

type ErrorPacketHandlerWrapper

type ErrorPacketHandlerWrapper func(session *Session, packetid int, data []byte) bool

func (ErrorPacketHandlerWrapper) OnErrorPacket

func (hw ErrorPacketHandlerWrapper) OnErrorPacket(session *Session, packetid int, data []byte) bool

type GbpEncDecoder

type GbpEncDecoder struct {
}

func (*GbpEncDecoder) Marshal

func (this *GbpEncDecoder) Marshal(pack interface{}) ([]byte, error)

func (*GbpEncDecoder) Unmarshal

func (this *GbpEncDecoder) Unmarshal(buf []byte, pack interface{}) error

type GobEncDecoder

type GobEncDecoder struct {
}

func (*GobEncDecoder) Marshal

func (this *GobEncDecoder) Marshal(pack interface{}) ([]byte, error)

func (*GobEncDecoder) Unmarshal

func (this *GobEncDecoder) Unmarshal(buf []byte, pack interface{}) error

type Handler

type Handler interface {
	Process(session *Session, packetid int, data interface{}) error
}

func GetHandler

func GetHandler(packetId int) Handler

type HandlerWrapper

type HandlerWrapper func(session *Session, packetid int, data interface{}) error

func (HandlerWrapper) Process

func (hw HandlerWrapper) Process(session *Session, packetid int, data interface{}) error

type ISession

type ISession interface {
	SetAttribute(key, value interface{}) bool
	RemoveAttribute(key interface{})
	GetAttribute(key interface{}) interface{}
	GetSessionConfig() *SessionConfig
	LocalAddr() string
	RemoteAddr() string
	IsIdle() bool
	Close()
	Send(packetid int, data interface{}, sync ...bool) bool
	FireConnectEvent() bool
	FireDisconnectEvent() bool
	FirePacketReceived(packetid int, packet interface{}) bool
	FirePacketSent(data []byte) bool
	FireSessionIdle() bool
}

type NetEngine

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

func (*NetEngine) Connect

func (e *NetEngine) Connect(sc *SessionConfig) error

func (*NetEngine) GetAcceptors

func (e *NetEngine) GetAcceptors() []Acceptor

func (*NetEngine) Init

func (e *NetEngine) Init()

func (*NetEngine) Listen

func (e *NetEngine) Listen(sc *SessionConfig) error

func (*NetEngine) ModuleName

func (e *NetEngine) ModuleName() string

////////////////////////////////////////////////////////////////// / Module Implement [beg] //////////////////////////////////////////////////////////////////

func (*NetEngine) ShutConnector

func (e *NetEngine) ShutConnector(ip string, port int)

func (*NetEngine) Shutdown

func (e *NetEngine) Shutdown()

func (*NetEngine) Update

func (e *NetEngine) Update()

type NetLibParamError

type NetLibParamError struct {
	Src   string
	Param string
}

func (*NetLibParamError) Error

func (self *NetLibParamError) Error() string

type NilEncDecoder

type NilEncDecoder struct {
}

func (*NilEncDecoder) Marshal

func (this *NilEncDecoder) Marshal(pack interface{}) ([]byte, error)

func (*NilEncDecoder) Unmarshal

func (this *NilEncDecoder) Unmarshal(buf []byte, pack interface{}) error

type PacketCutSlicesFunc

type PacketCutSlicesFunc func(data []byte) (int, []interface{})

type PacketFactory

type PacketFactory interface {
	CreatePacket() interface{}
}

type PacketFactoryWrapper

type PacketFactoryWrapper func() interface{}

func (PacketFactoryWrapper) CreatePacket

func (pfw PacketFactoryWrapper) CreatePacket() interface{}

type PacketHeader

type PacketHeader struct {
	EncodeType int16
	PacketId   int16
}

type PacketPool

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

func NewPacketPool

func NewPacketPool(num int) *PacketPool

func (*PacketPool) Get

func (pp *PacketPool) Get() (p *packet)

func (*PacketPool) Give

func (pp *PacketPool) Give(p *packet)

type ProtoHeader

type ProtoHeader struct {
	Len uint16
	Seq uint16
}

type ProtocolDecoder

type ProtocolDecoder interface {
	Decode(s *Session, r io.Reader) (packetid int, packet interface{}, err error, raw []byte)
	FinishDecode(s *Session)
}

func GetProtocolDecoder

func GetProtocolDecoder(name string) ProtocolDecoder

type ProtocolEncoder

type ProtocolEncoder interface {
	Encode(s *Session, packetid int, packet interface{}, w io.Writer) (data []byte, err error)
	FinishEncode(s *Session)
}

func GetProtocolEncoder

func GetProtocolEncoder(name string) ProtocolEncoder

type RWBuffer

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

func AllocRWBuf

func AllocRWBuf() *RWBuffer

func (*RWBuffer) Init

func (rwb *RWBuffer) Init()

type ServerInfo

type ServerInfo struct {
	Name   string
	Type   int
	Id     int
	AreaID int
	Data   string
	Banner []string
}

type Session

type Session struct {
	Id int
	// contains filtered or unexported fields
}

func (*Session) Close

func (s *Session) Close()

func (*Session) FireConnectEvent

func (s *Session) FireConnectEvent() bool

func (*Session) FireDisconnectEvent

func (s *Session) FireDisconnectEvent() bool

func (*Session) FirePacketReceived

func (s *Session) FirePacketReceived(packetid int, packet interface{}) bool

func (*Session) FirePacketSent

func (s *Session) FirePacketSent(data []byte) bool

func (*Session) FireSessionIdle

func (s *Session) FireSessionIdle() bool

func (*Session) GetAttribute

func (s *Session) GetAttribute(key interface{}) interface{}

func (*Session) GetSessionConfig

func (s *Session) GetSessionConfig() *SessionConfig

func (*Session) IsConned

func (s *Session) IsConned() bool

func (*Session) IsIdle

func (s *Session) IsIdle() bool

func (*Session) LocalAddr

func (s *Session) LocalAddr() string

func (*Session) RemoteAddr

func (s *Session) RemoteAddr() string

func (*Session) RemoveAttribute

func (s *Session) RemoveAttribute(key interface{})

func (*Session) Send

func (s *Session) Send(packetid int, data interface{}, sync ...bool) bool

func (*Session) SetAttribute

func (s *Session) SetAttribute(key, value interface{}) bool

type SessionCloseListener

type SessionCloseListener interface {
	// contains filtered or unexported methods
}

type SessionConfig

type SessionConfig struct {
	Id                     int
	Type                   int
	AreaId                 int
	Protocol               string //"tcp" "ws"
	Name                   string
	Ip                     string
	Path                   string //path of "ws"
	OuterIp                string //对外的IP
	Port                   int
	MaxDone                int
	MaxPend                int
	MaxPacket              int
	MaxConn                int
	ExtraConn              int
	RcvBuff                int
	SndBuff                int
	SoLinger               int
	WriteTimeout           time.Duration
	ReadTimeout            time.Duration
	IdleTimeout            time.Duration
	KeepAlive              bool
	KeepAlivePeriod        time.Duration
	KeepAliveIdle          time.Duration
	KeepAliveCount         int
	NoDelay                bool
	IsClient               bool
	IsAutoReconn           bool
	IsInnerLink            bool
	AuthKey                string //Authentication Key
	EncoderName            string //ProtocolEncoder name
	DecoderName            string //ProtocolDecoder name
	ErrorPacketHandlerName string
	FilterChain            []string
	HandlerChain           []string
	SupportFragment        bool
	AllowMultiConn         bool
	// contains filtered or unexported fields
}

func (*SessionConfig) GetFilter

func (sc *SessionConfig) GetFilter(name string) SessionFilter

func (*SessionConfig) GetHandler

func (sc *SessionConfig) GetHandler(name string) SessionHandler

func (*SessionConfig) Init

func (sc *SessionConfig) Init()

type SessionCutPacketListener

type SessionCutPacketListener interface {
	// contains filtered or unexported methods
}

type SessionFilter

type SessionFilter interface {
	GetName() string
	GetInterestOps() uint
	OnSessionOpened(s *Session) bool                                    //run in main goroutine
	OnSessionClosed(s *Session) bool                                    //run in main goroutine
	OnSessionIdle(s *Session) bool                                      //run in main goroutine
	OnPacketReceived(s *Session, packetid int, packet interface{}) bool //run in session receive goroutine
	OnPacketSent(s *Session, data []byte) bool                          //run in session send goroutine
}

type SessionFilterChain

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

func NewSessionFilterChain

func NewSessionFilterChain() *SessionFilterChain

func (*SessionFilterChain) AddFirst

func (sfc *SessionFilterChain) AddFirst(sf SessionFilter)

func (*SessionFilterChain) AddLast

func (sfc *SessionFilterChain) AddLast(sf SessionFilter)

func (*SessionFilterChain) GetFilter

func (sfc *SessionFilterChain) GetFilter(name string) SessionFilter

func (*SessionFilterChain) OnPacketReceived

func (sfc *SessionFilterChain) OnPacketReceived(s *Session, packetid int, packet interface{}) bool

func (*SessionFilterChain) OnPacketSent

func (sfc *SessionFilterChain) OnPacketSent(s *Session, data []byte) bool

func (*SessionFilterChain) OnSessionClosed

func (sfc *SessionFilterChain) OnSessionClosed(s *Session) bool

func (*SessionFilterChain) OnSessionIdle

func (sfc *SessionFilterChain) OnSessionIdle(s *Session) bool

func (*SessionFilterChain) OnSessionOpened

func (sfc *SessionFilterChain) OnSessionOpened(s *Session) bool

type SessionFilterCreator

type SessionFilterCreator func() SessionFilter

func GetSessionFilterCreator

func GetSessionFilterCreator(name string) SessionFilterCreator

type SessionHandler

type SessionHandler interface {
	GetName() string
	GetInterestOps() uint
	OnSessionOpened(s *Session)                                    //run in main goroutine
	OnSessionClosed(s *Session)                                    //run in main goroutine
	OnSessionIdle(s *Session)                                      //run in main goroutine
	OnPacketReceived(s *Session, packetid int, packet interface{}) //run in session receive goroutine
	OnPacketSent(s *Session, data []byte)                          //run in session send goroutine
}

type SessionHandlerChain

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

func NewSessionHandlerChain

func NewSessionHandlerChain() *SessionHandlerChain

func (*SessionHandlerChain) AddFirst

func (shc *SessionHandlerChain) AddFirst(sh SessionHandler)

func (*SessionHandlerChain) AddLast

func (shc *SessionHandlerChain) AddLast(sh SessionHandler)

func (*SessionHandlerChain) GetHandler

func (shc *SessionHandlerChain) GetHandler(name string) SessionHandler

func (*SessionHandlerChain) OnPacketReceived

func (shc *SessionHandlerChain) OnPacketReceived(s *Session, packetid int, packet interface{})

func (*SessionHandlerChain) OnPacketSent

func (shc *SessionHandlerChain) OnPacketSent(s *Session, data []byte)

func (*SessionHandlerChain) OnSessionClosed

func (shc *SessionHandlerChain) OnSessionClosed(s *Session)

func (*SessionHandlerChain) OnSessionIdle

func (shc *SessionHandlerChain) OnSessionIdle(s *Session)

func (*SessionHandlerChain) OnSessionOpened

func (shc *SessionHandlerChain) OnSessionOpened(s *Session)

type SessionHandlerCreator

type SessionHandlerCreator func() SessionHandler

func GetSessionHandlerCreator

func GetSessionHandlerCreator(name string) SessionHandlerCreator

type TcpAcceptor

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

func (*TcpAcceptor) Addr

func (a *TcpAcceptor) Addr() net.Addr

func (*TcpAcceptor) GetSessionConfig

func (a *TcpAcceptor) GetSessionConfig() *SessionConfig

type TcpConnector

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

func (*TcpConnector) GetSessionConfig

func (c *TcpConnector) GetSessionConfig() *SessionConfig

type TcpSession

type TcpSession struct {
	Session
	// contains filtered or unexported fields
}

func (*TcpSession) LocalAddr

func (s *TcpSession) LocalAddr() string

func (*TcpSession) RemoteAddr

func (s *TcpSession) RemoteAddr() string

type TypeTester

type TypeTester func(pack interface{}) int

type UnparsePacketTypeErr

type UnparsePacketTypeErr struct {
	EncodeType int16
	PacketId   int16
}

func NewUnparsePacketTypeErr

func NewUnparsePacketTypeErr(et, packid int16) *UnparsePacketTypeErr

func (*UnparsePacketTypeErr) Error

func (this *UnparsePacketTypeErr) Error() string

type WsAcceptor

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

func (*WsAcceptor) Addr

func (a *WsAcceptor) Addr() net.Addr

func (*WsAcceptor) GetSessionConfig

func (a *WsAcceptor) GetSessionConfig() *SessionConfig

type WsAddr

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

func (*WsAddr) Network

func (a *WsAddr) Network() string

name of the network

func (*WsAddr) String

func (a *WsAddr) String() string

string form of address

type WsConnector

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

func (*WsConnector) GetSessionConfig

func (c *WsConnector) GetSessionConfig() *SessionConfig

type WsSession

type WsSession struct {
	Session
	// contains filtered or unexported fields
}

func (*WsSession) LocalAddr

func (s *WsSession) LocalAddr() string

func (*WsSession) RemoteAddr

func (s *WsSession) RemoteAddr() string

Directories

Path Synopsis
Package tcpkeepalive implements additional TCP keepalive control beyond what is currently offered by the net pkg.
Package tcpkeepalive implements additional TCP keepalive control beyond what is currently offered by the net pkg.

Jump to

Keyboard shortcuts

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