net4g

package module
v0.0.0-...-1fbf732 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2017 License: MIT Imports: 18 Imported by: 0

README

net4g

A network lib for golang/game

Documentation

Index

Constants

View Source
const (
	HEART_BEAT_INTERVAL  = 1 * time.Second
	HEART_BEAT_LAST_TIME = "__NET4G01"
	//SESSION_ID = "ID"
	SESSION_GROUP_NAME              = "__NET4G02"
	SESSION_CONNECT_KEY             = "__NET4G03"
	SESSION_CONNECT_KEY_USER        = "__NET4G03_USER"
	SESSION_CONNECT_ESTABLISH_TIME  = "__NET4G04"
	SESSION_CONNECT_LAST_READ_TIME  = "__NET4G05"
	SESSION_CONNECT_LAST_WRITE_TIME = "__NET4G06"
)

Variables

View Source
var NetConfig netConfig

Functions

func Dispatch

func Dispatch(dispatchers []*Dispatcher, agent NetAgent)

func IsHeartbeatData

func IsHeartbeatData(data []byte) bool

func NeedDeserialize

func NeedDeserialize(dispatchers []*Dispatcher, t reflect.Type) bool

func NewNetAddrFn

func NewNetAddrFn(addr ...string) func() (addrs []*NetAddr, err error)

func NewNetHub

func NewNetHub(heartbeat bool, lb bool) *netHub

func NewNetKeyAddrFn

func NewNetKeyAddrFn(key_or_addr ...string) func() (addrs []*NetAddr, err error)

func Serialize

func Serialize(serializer Serializer, v interface{}, h ...interface{}) (data []byte, err error)

func TestCall

func TestCall(functions ...func())

func TestDone

func TestDone(force ...bool)

func TestWait

func TestWait(delay ...int)

Types

type ByteSerializer

type ByteSerializer struct {
	*EmptySerializer
}

func (*ByteSerializer) DeserializeData

func (s *ByteSerializer) DeserializeData(data []byte, t reflect.Type) (h interface{}, v interface{}, err error)

func (*ByteSerializer) Serialize

func (s *ByteSerializer) Serialize(v, h interface{}) (data []byte, err error)

type Closer

type Closer interface {
	Close()
}

type Dispatcher

type Dispatcher struct {
	Name string
	// contains filtered or unexported fields
}

func NewDispatcher

func NewDispatcher(goroutineNum ...int) *Dispatcher

func NewNamedDispatcher

func NewNamedDispatcher(name string, goroutineNum ...int) *Dispatcher

func (*Dispatcher) AddHandler

func (p *Dispatcher) AddHandler(h func(agent NetAgent), idOrType ...interface{})

func (*Dispatcher) AddRawHandler

func (p *Dispatcher) AddRawHandler(h func(agent NetAgent), idOrType ...interface{})

func (*Dispatcher) Destroy

func (p *Dispatcher) Destroy()

func (*Dispatcher) NeedDeserialize

func (p *Dispatcher) NeedDeserialize(t reflect.Type) bool

func (*Dispatcher) NewTicker

func (p *Dispatcher) NewTicker(d time.Duration, h func(t *time.Time, params ...interface{}), params ...interface{}) NetTimer

func (*Dispatcher) NewTimer

func (p *Dispatcher) NewTimer(d time.Duration, h func(t *time.Time, params ...interface{}), params ...interface{}) NetTimer

func (*Dispatcher) OnConnectionClosed

func (p *Dispatcher) OnConnectionClosed(h func(agent NetAgent))

func (*Dispatcher) OnConnectionCreated

func (p *Dispatcher) OnConnectionCreated(h func(agent NetAgent))

func (*Dispatcher) OnDestroy

func (p *Dispatcher) OnDestroy(h func())

type EmptySerializer

type EmptySerializer struct {
	SerializerTypeIdMap   map[reflect.Type]interface{}
	SerializerIdTypeMap   map[interface{}]reflect.Type
	DeserializerTypeIdMap map[reflect.Type]interface{}
	DeserializerIdTypeMap map[interface{}]reflect.Type
	// contains filtered or unexported fields
}

func NewEmptySerializer

func NewEmptySerializer() *EmptySerializer

func (*EmptySerializer) DeserializeId

func (s *EmptySerializer) DeserializeId(raw []byte) (id interface{}, t reflect.Type, data []byte, err error)

func (*EmptySerializer) PreRawPack

func (s *EmptySerializer) PreRawPack(rp *RawPack)

func (*EmptySerializer) RangeId

func (s *EmptySerializer) RangeId(f func(id interface{}, t reflect.Type))

func (*EmptySerializer) RangeIdInt

func (s *EmptySerializer) RangeIdInt(f func(id int, t reflect.Type))

func (*EmptySerializer) RangeIdString

func (s *EmptySerializer) RangeIdString(f func(id int, t reflect.Type))

func (*EmptySerializer) RegisterDeserializeId

func (s *EmptySerializer) RegisterDeserializeId(v interface{}, one_id ...interface{}) (id interface{}, err error)

func (*EmptySerializer) RegisterId

func (s *EmptySerializer) RegisterId(v interface{}, one_id ...interface{}) (id interface{}, err error)

func (*EmptySerializer) RegisterSerializeId

func (s *EmptySerializer) RegisterSerializeId(v interface{}, one_id ...interface{}) (id interface{}, err error)

func (*EmptySerializer) SetIdInitValue

func (s *EmptySerializer) SetIdInitValue(id int)

type Intercepter

type Intercepter interface {
	Intercept(data []byte) []byte
}

type JsonSerializer

type JsonSerializer struct {
	*EmptySerializer
}

func (*JsonSerializer) DeserializeData

func (s *JsonSerializer) DeserializeData(data []byte, t reflect.Type) (h, v interface{}, err error)

func (*JsonSerializer) Serialize

func (s *JsonSerializer) Serialize(v, h interface{}) (data []byte, err error)

type NetAddr

type NetAddr struct {
	Key  string
	Addr string
}

func NewAddr

func NewAddr(addr string) *NetAddr

func NewKeyAddr

func NewKeyAddr(key, addr string) *NetAddr

type NetAgent

type NetAgent interface {
	RawPack() *RawPack
	Header() interface{}
	Msg() interface{}
	RemoteAddr() net.Addr
	Session() NetSession
	Key(key ...string) string
	Write(v interface{}, h ...interface{}) error
	Close()
	IsClosed() bool
}

type NetConn

type NetConn interface {
	RemoteAddr() net.Addr
	Read() (p []byte, err error)
	Write(p []byte) error
	Session() NetSession
	Close()
	IsClosed() bool
	NotWrittenData() [][]byte
	PopCount() (read int64, write int64)
	PopDataUsage() (read int64, write int64)
}

type NetHub

type NetHub interface {
	Add(key string, conn NetConn)
	Key(conn NetConn, key string)
	Get(key string) NetConn
	Remove(conn NetConn)
	Slice() []NetConn
	Count() int
	Heartbeat(conn NetConn)
	Kick(key string)
	SetSerializer(serializer Serializer)
	Broadcast(v interface{}, filter func(session NetSession) bool, h ...interface{}) error
	BroadcastAll(v interface{}, h ...interface{}) error
	BroadcastOthers(mySession NetSession, v interface{}, h ...interface{}) error
	BroadcastOne(v interface{}, errFunc func(error), h ...interface{}) error
	Send(key string, v interface{}) error
	MultiSend(keys []string, v interface{}) error
	SetGroup(session NetSession, group string)
	Group(group string, v interface{}, h ...interface{}) error
	GroupOne(group string, v interface{}, errFunc func(error), h ...interface{}) error
	RangeConn(f func(NetConn))
	RangeSession(f func(session NetSession))
	Statistics()
	PackCount() (totalRead int64, totalWrite int64, popRead int64, popWrite int64)
	DataUsage() (totalRead int64, totalWrite int64, popRead int64, popWrite int64)
	Closed() bool
	Destroy()
}

type NetReader

type NetReader interface {
	Read(after func(data []byte) bool)
}

type NetSession

type NetSession interface {
	Set(key string, value interface{})
	Get(key string, defaultValue ...interface{}) interface{}
	GetString(key string, defaultValue ...string) string
	GetInt(key string, defaultValue ...int) int
	GetInt64(key string, defaultValue ...int64) int64
	GetBool(key string, defaultValue ...bool) bool
	Has(key string) bool
	Remove(key string)
	SetLogin()
	IsLogin() bool
}

func NewNetSession

func NewNetSession() NetSession

type NetTimer

type NetTimer interface {
	Start(d time.Duration)
	Stop() bool
}

func NewNetTicker

func NewNetTicker(dispatcher *Dispatcher, d time.Duration, h func(time *time.Time, params ...interface{}), params ...interface{}) NetTimer

func NewNetTimer

func NewNetTimer(dispatcher *Dispatcher, d time.Duration, h func(time *time.Time, params ...interface{}), params ...interface{}) NetTimer

type NetTimerEvent

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

type NetWriter

type NetWriter interface {
	Write(p []byte)
}

type ProtobufSerializer

type ProtobufSerializer struct {
	*EmptySerializer
}

func (*ProtobufSerializer) DeserializeData

func (s *ProtobufSerializer) DeserializeData(data []byte, t reflect.Type) (h, v interface{}, err error)

func (*ProtobufSerializer) Serialize

func (s *ProtobufSerializer) Serialize(v, h interface{}) (data []byte, err error)

type RawPack

type RawPack struct {
	Id     interface{}
	Type   reflect.Type
	Header []byte
	Data   []byte
}

func NewRawPack

func NewRawPack(id interface{}, data ...[]byte) *RawPack

func NewRawPackByType

func NewRawPackByType(v interface{}, data ...[]byte) *RawPack

func (*RawPack) IntId

func (rp *RawPack) IntId() int

func (*RawPack) StringId

func (rp *RawPack) StringId() string

type ReadMode

type ReadMode string
const (
	READ_MODE_BY_LENGTH ReadMode = "by_length"
	READ_MODE_BEGIN_END ReadMode = "begin_end"
)

type Serializer

type Serializer interface {
	SetIdInitValue(id int)
	RegisterId(v interface{}, one_id ...interface{}) (id interface{}, err error)
	RegisterSerializeId(v interface{}, one_id ...interface{}) (id interface{}, err error)
	RegisterDeserializeId(v interface{}, one_id ...interface{}) (id interface{}, err error)
	Serialize(v, h interface{}) (data []byte, err error)
	DeserializeId(raw []byte) (id interface{}, t reflect.Type, data []byte, err error)
	DeserializeData(data []byte, t reflect.Type) (h, v interface{}, err error)
	RangeId(f func(id interface{}, t reflect.Type))
}

func NewByteSerializer

func NewByteSerializer() Serializer

func NewJsonSerializer

func NewJsonSerializer() Serializer

func NewProtobufSerializer

func NewProtobufSerializer() Serializer

func NewStringSerializer

func NewStringSerializer() Serializer

type StringSerializer

type StringSerializer struct {
	*EmptySerializer
}

func (*StringSerializer) DeserializeData

func (s *StringSerializer) DeserializeData(data []byte, t reflect.Type) (h, v interface{}, err error)

func (*StringSerializer) Serialize

func (s *StringSerializer) Serialize(v, h interface{}) (raw []byte, err error)

type TCPClient

type TCPClient struct {
	AutoReconnect bool
	// contains filtered or unexported fields
}

func NewTcpClient

func NewTcpClient(name string, addrFn func() (addrs []*NetAddr, err error)) *TCPClient

func (*TCPClient) AddDispatchers

func (c *TCPClient) AddDispatchers(dispatchers ...*Dispatcher) *TCPClient

func (*TCPClient) Close

func (c *TCPClient) Close()

func (*TCPClient) Connect

func (c *TCPClient) Connect() *TCPClient

func (*TCPClient) DisableAutoReconnect

func (c *TCPClient) DisableAutoReconnect() *TCPClient

func (*TCPClient) EnableMonitor

func (c *TCPClient) EnableMonitor(monitorLog *log4g.Loggers) *TCPClient

func (*TCPClient) SetHub

func (c *TCPClient) SetHub(hub NetHub) *TCPClient

func (*TCPClient) SetReadIntercepter

func (c *TCPClient) SetReadIntercepter(intercepter Intercepter) *TCPClient

func (*TCPClient) SetSerializer

func (c *TCPClient) SetSerializer(serializer Serializer) *TCPClient

func (*TCPClient) SetWriteIntercepter

func (c *TCPClient) SetWriteIntercepter(intercepter Intercepter) *TCPClient

func (*TCPClient) Wait

func (c *TCPClient) Wait()

type TCPServer

type TCPServer struct {
	Name string
	Addr string

	Serializer Serializer
	Dispatcher *Dispatcher
	// contains filtered or unexported fields
}

func NewNamedTcpServer

func NewNamedTcpServer(name string, addr ...string) *TCPServer

func NewTcpServer

func NewTcpServer(addr ...string) *TCPServer

func (*TCPServer) AddDispatchers

func (s *TCPServer) AddDispatchers(dispatchers ...*Dispatcher) *TCPServer

func (*TCPServer) Close

func (s *TCPServer) Close()

func (*TCPServer) EnableMonitor

func (s *TCPServer) EnableMonitor(monitorLog *log4g.Loggers) *TCPServer

func (*TCPServer) NewJsonSerializer

func (s *TCPServer) NewJsonSerializer() *TCPServer

func (*TCPServer) SetHub

func (s *TCPServer) SetHub(hub NetHub) *TCPServer

func (*TCPServer) SetReadIntercepter

func (s *TCPServer) SetReadIntercepter(intercepter Intercepter) *TCPServer

func (*TCPServer) SetSerializer

func (s *TCPServer) SetSerializer(serializer Serializer) *TCPServer

func (*TCPServer) SetWriteIntercepter

func (s *TCPServer) SetWriteIntercepter(intercepter Intercepter) *TCPServer

func (*TCPServer) Start

func (s *TCPServer) Start() *TCPServer

func (*TCPServer) Wait

func (s *TCPServer) Wait(others ...Closer)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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