fnet

package
v0.0.0-...-2f8f7b3 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2022 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const DEFAULT_HEADER_LEN uint32 = 9
View Source
const MAX_MSG_WORKER_SIZE = 100
View Source
const MAX_PACKAGE_SIZE = 32 * 1024
View Source
const MSG_CHAN_BUFF_LEN = 10 * 1024
View Source
const MSG_WORKER_POOL_SIZE = 1000

Variables

This section is empty.

Functions

This section is empty.

Types

type BaseRouter

type BaseRouter struct{}

func (*BaseRouter) Handle

func (br *BaseRouter) Handle(request IRequest) MsgErrCode

消息处理

func (*BaseRouter) PostHandle

func (br *BaseRouter) PostHandle(request IRequest) MsgErrCode

消息处理后调用

func (*BaseRouter) PreHandle

func (br *BaseRouter) PreHandle(request IRequest) MsgErrCode

消息处理前调用

type CSocket

type CSocket struct {
	Socket
}

func NewCSocket

func NewCSocket(netService INetService) *CSocket

func (*CSocket) Connect

func (cs *CSocket) Connect(ipVer, ip string, port int) (error, ISession)

type ConnData

type ConnData struct {
	NodeID     int32
	Port       int32
	IP         string
	ServerName string
	ServerType int32
	ConnState  ConnState
	// contains filtered or unexported fields
}

type ConnDataMap

type ConnDataMap map[int32]*ConnData

func (ConnDataMap) Add

func (m ConnDataMap) Add(connData *ConnData)

func (ConnDataMap) GetByServerID

func (m ConnDataMap) GetByServerID(serverID int32) *ConnData

func (ConnDataMap) GetByServerType

func (m ConnDataMap) GetByServerType(serverType int32) *ConnData

func (ConnDataMap) Remove

func (m ConnDataMap) Remove(serverID int32)

type ConnState

type ConnState uint32
const (
	CS_NONE         ConnState = 0
	CS_CONNECTING   ConnState = 1
	CS_CONNECTED    ConnState = 2
	CS_DISCONNECTED ConnState = 3
	CS_RECONNECTING ConnState = 4
	CS_RECONNECTED  ConnState = 5
)

type DataPack

type DataPack struct{}

func NewDataPack

func NewDataPack() *DataPack

func (*DataPack) GetHeadLen

func (dp *DataPack) GetHeadLen() uint32

func (*DataPack) Pack

func (dp *DataPack) Pack(msg IMessage) ([]byte, error)

func (*DataPack) UnPack

func (dp *DataPack) UnPack(binaryData []byte) (IMessage, error)

type ICSocket

type ICSocket interface {
	Connect(ipVer, ip string, port int) (error, ISession)
}

type IDataPack

type IDataPack interface {
	UnPack(binaryData []byte) (IMessage, error)
	Pack(msg IMessage) ([]byte, error)
	GetHeadLen() uint32
}

type IMessage

type IMessage interface {
	GetDataLen() uint32
	GetMsgID() uint32
	GetMsgType() uint8
	GetData() []byte

	SetMsgID(msgID uint32)
	SetMsgType(msgType uint8)
	SetData(data []byte)
	SetDataLen(dataLen uint32)
}

type IMsgHandler

type IMsgHandler interface {
	DoMsg(request IRequest)
	AddRouter(msgID uint32, router IRouter)

	StartWorkPool()
	DispatchByMsgID(request IRequest)

	IsUseWorkPool() bool
}

type INetClient

type INetClient interface {
	GetSession() ISession
	Connect(ip string, port int) bool
}

抽象网络管理器-客户端

type INetClients

type INetClients interface {
	// 添加将要连接的服务
	AddConnServer(connData *ConnData)
	// 进行连接服务
	ProcessExecute()
	// 重置连接状态
	ResetConnState(nodeID int32, state ConnState)
}

type INetServer

type INetServer interface {
	GetSessionManager() ISessionManager
	GetSession(sessionID int64) (ISession, error)
	Listen() error
}

抽象网络管理器-服务器端

type INetService

type INetService interface {
	GetSocket() ISocket
	SetNodeService(service INodeService)
	GetNodeService() INodeService
}

netservice 接口

type INodeClient

type INodeClient interface {
	// 作为客户端上报服务器信息
	SetReportInfo(serverType ServerType)
	UpdateOnline(count int)
	UpdateServerState(state ServerState)
	OnMasterRouter()
	AddConnServer()
}

type INodeServer

type INodeServer interface {
}

type INodeService

type INodeService interface {
	GetMsgHandler() IMsgHandler
	GetDataPack() IDataPack
	GetNodeConfig() *configure.NetNode
	RegisterRouter(msgID uint32, router IRouter)

	SetOnStart(onStart FuncArgs)
	SetOnStop(onStop FuncArgs)

	SetOnConnected(onConnected FuncArgs)
	SetOnDisconnected(onDisconnected FuncArgs)
	GetOnConnected() FuncArgs
	GetOnDisconnected() FuncArgs

	IsStop() bool
	GetNodeID() int32
}

node 服务接口

type IRequest

type IRequest interface {
	GetSession() ISession
	GetData() []byte
	GetMsgID() uint32
	GetMsgType() uint8
}

包装消息和连接

type IRoleMsgHandler

type IRoleMsgHandler interface {
	DispatchByRoleID(request IRequest)
}

type IRouter

type IRouter interface {
	PreHandle(request IRequest) MsgErrCode
	Handle(request IRequest) MsgErrCode
	PostHandle(request IRequest) MsgErrCode
}

type ISSocket

type ISSocket interface {
	Listen() error
}

type ISceneMsgHandler

type ISceneMsgHandler interface {
	DispatchBySceneID(request IRequest)
}

type IServerMsgHandler

type IServerMsgHandler interface {
	DispatchByServerID(request IRequest)
}

type ISession

type ISession interface {
	Start()
	Stop()
	GetID() int64
	Context() context.Context

	GetConn() net.Conn  // 获取原始连接
	RemoteAddr() string // 获取远程客户端地址
	LocalAddr() string  //

	SendProto(msgID uint32, msg *proto.Message) error
	SendJson(msgID uint32, msg interface{}) error

	SendProtoBuffer(msgID uint32, data []byte) error
	SendJsonBuffer(msgID uint32, data []byte) error

	SetProperty(key string, value interface{})
	GetProperty(key string) interface{}
	RemoveProperty(key string)

	SetNetService(service INetService)
	GetNetService() INetService

	GetNodeService() INodeService
}

type ISessionManager

type ISessionManager interface {
	Add(session ISession)
	Remove(session ISession)
	RemoveById(sessionID int64)
	Len() int
	Clear()
	GetSession(sessionID int64) (ISession, error)
	ClearSession(sID int64)
}

type ISocket

type ISocket interface {
	Close()
	Recv(data []byte) (n int, err error)
	Send(data []byte) (int, error)
	GetConn() net.Conn
	LocalAddr() string
	RemoteAddr() string
}

type Message

type Message struct {
	ID      uint32
	DataLen uint32
	MsgType uint8
	Data    []byte
}

func NewMessage

func NewMessage(msgID uint32, msgType uint8, data []byte) *Message

func (*Message) GetData

func (m *Message) GetData() []byte

func (*Message) GetDataLen

func (m *Message) GetDataLen() uint32

func (*Message) GetMsgID

func (m *Message) GetMsgID() uint32

func (*Message) GetMsgType

func (m *Message) GetMsgType() uint8

func (*Message) SetData

func (m *Message) SetData(data []byte)

func (*Message) SetDataLen

func (m *Message) SetDataLen(dataLen uint32)

func (*Message) SetMsgID

func (m *Message) SetMsgID(msgID uint32)

func (*Message) SetMsgType

func (m *Message) SetMsgType(msgType uint8)

type MsgErrCode

type MsgErrCode int64
const (
	MEC_OK            MsgErrCode = 0
	MEC_ERR           MsgErrCode = 1
	MEC_MSGID         MsgErrCode = 2
	MEC_MSG_UNMARSHAL MsgErrCode = 3
	MEC_MSG_MARSHAL   MsgErrCode = 3
)

type MsgHandler

type MsgHandler struct {
	// 根据消息id指定路由
	Apis map[uint32]IRouter
	// contains filtered or unexported fields
}

func NewMsgHandler

func NewMsgHandler() *MsgHandler

func (*MsgHandler) AddRouter

func (mh *MsgHandler) AddRouter(msgID uint32, router IRouter)

func (*MsgHandler) DispatchByMsgID

func (mh *MsgHandler) DispatchByMsgID(request IRequest)

func (*MsgHandler) DispatchByRoleID

func (mh *MsgHandler) DispatchByRoleID(request IRequest)

func (*MsgHandler) DispatchBySceneID

func (mh *MsgHandler) DispatchBySceneID(request IRequest)

func (*MsgHandler) DispatchByServerID

func (mh *MsgHandler) DispatchByServerID(request IRequest)

func (*MsgHandler) DoMsg

func (mh *MsgHandler) DoMsg(request IRequest)

func (*MsgHandler) IsUseWorkPool

func (mh *MsgHandler) IsUseWorkPool() bool

func (*MsgHandler) StartWorkPool

func (mh *MsgHandler) StartWorkPool()

type MsgType

type MsgType uint8
const (
	MT_OTHER MsgType = 0
	MT_PROTO MsgType = 1
	MT_JSON  MsgType = 2
	MT_HTTP  MsgType = 3
)

type NetClient

type NetClient struct {
	NetService
	// contains filtered or unexported fields
}

func NewNetClient

func NewNetClient(service INodeService) *NetClient

func (*NetClient) Connect

func (c *NetClient) Connect(ip string, port int) bool

func (*NetClient) GetNodeService

func (c *NetClient) GetNodeService() INodeService

func (*NetClient) GetSession

func (c *NetClient) GetSession() ISession

func (*NetClient) GetSocket

func (c *NetClient) GetSocket() ISocket

func (*NetClient) SetNodeService

func (c *NetClient) SetNodeService(service INodeService)

type NetClients

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

func NewNetClients

func NewNetClients(service INodeService) *NetClients

func (*NetClients) AddConnServer

func (cs *NetClients) AddConnServer(connData *ConnData)

func (*NetClients) ProcessExecute

func (cs *NetClients) ProcessExecute()

func (*NetClients) ResetConnState

func (cs *NetClients) ResetConnState(nodeID int32, state ConnState)

type NetEventType

type NetEventType uint32
const (
	NET_EOF       NetEventType = 0
	NET_ERROR     NetEventType = 1
	NET_TIMEOUT   NetEventType = 2
	NET_CONNECTED NetEventType = 3
)

type NetServer

type NetServer struct {
	NetService
	// contains filtered or unexported fields
}

func NewNetServer

func NewNetServer(nodeService INodeService, servIP string, servPort int) *NetServer

func (*NetServer) GetNodeService

func (s *NetServer) GetNodeService() INodeService

func (*NetServer) GetSession

func (s *NetServer) GetSession(sessionID int64) (ISession, error)

func (*NetServer) GetSessionManager

func (s *NetServer) GetSessionManager() ISessionManager

func (*NetServer) GetSocket

func (s *NetServer) GetSocket() ISocket

func (*NetServer) Listen

func (s *NetServer) Listen() error

func (*NetServer) SetNodeService

func (s *NetServer) SetNodeService(service INodeService)

type NetService

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

type NetState

type NetState uint32
const (
	NS_NONE  NetState = 0
	NS_START NetState = 1
	NS_STOP  NetState = 2
)

type NodeClient

type NodeClient struct {
	NodeService
	// contains filtered or unexported fields
}

func NewNodeClient

func NewNodeClient(config *configure.NetNode) *NodeClient

func (*NodeClient) AddConnServer

func (nc *NodeClient) AddConnServer()

func (*NodeClient) OnMasterRouter

func (nc *NodeClient) OnMasterRouter()

func (*NodeClient) Serve

func (nc *NodeClient) Serve()

func (*NodeClient) SetReportInfo

func (nc *NodeClient) SetReportInfo(serverType ServerType)

func (*NodeClient) Start

func (nc *NodeClient) Start()

func (*NodeClient) Stop

func (nc *NodeClient) Stop()

func (*NodeClient) UpdateOnline

func (nc *NodeClient) UpdateOnline(count int)

func (*NodeClient) UpdateServerState

func (nc *NodeClient) UpdateServerState(state ServerState)

type NodeServer

type NodeServer struct {
	NodeService
	// contains filtered or unexported fields
}

func NewNodeServer

func NewNodeServer(config *configure.NetNode) *NodeServer

func (*NodeServer) Serve

func (s *NodeServer) Serve()

func (*NodeServer) Start

func (s *NodeServer) Start()

func (*NodeServer) Stop

func (s *NodeServer) Stop()

type NodeService

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

func (*NodeService) GetDataPack

func (ns *NodeService) GetDataPack() IDataPack

func (*NodeService) GetMsgHandler

func (ns *NodeService) GetMsgHandler() IMsgHandler

func (*NodeService) GetNodeConfig

func (ns *NodeService) GetNodeConfig() *configure.NetNode

func (*NodeService) GetNodeID

func (ns *NodeService) GetNodeID() int32

func (*NodeService) GetOnConnected

func (ns *NodeService) GetOnConnected() FuncArgs

func (*NodeService) GetOnDisconnected

func (ns *NodeService) GetOnDisconnected() FuncArgs

func (*NodeService) IsStop

func (ns *NodeService) IsStop() bool

func (*NodeService) RegisterRouter

func (ns *NodeService) RegisterRouter(msgID uint32, router IRouter)

func (*NodeService) SetOnConnected

func (ns *NodeService) SetOnConnected(onConnected FuncArgs)

func (*NodeService) SetOnDisconnected

func (ns *NodeService) SetOnDisconnected(onDisconnected FuncArgs)

func (*NodeService) SetOnStart

func (ns *NodeService) SetOnStart(onStart FuncArgs)

func (*NodeService) SetOnStop

func (ns *NodeService) SetOnStop(onStop FuncArgs)

type Request

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

func NewRequest

func NewRequest(session ISession, msg IMessage) *Request

func (*Request) GetData

func (r *Request) GetData() []byte

func (*Request) GetMsgID

func (r *Request) GetMsgID() uint32

func (*Request) GetMsgType

func (r *Request) GetMsgType() uint8

func (*Request) GetSession

func (r *Request) GetSession() ISession

type SSocket

type SSocket struct {
	Socket
	// contains filtered or unexported fields
}

func NewSSocket

func NewSSocket(netService INetService, ip string, port int) *SSocket

func (*SSocket) Listen

func (ss *SSocket) Listen() error

type ServState

type ServState uint32
const (
	SS_NONE  ServState = 0
	SS_START ServState = 1
	SS_STOP  ServState = 2
	SS_FATAL ServState = 3
)

type Session

type Session struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewSession

func NewSession(connID int64, socket ISocket) *Session

func (*Session) Context

func (s *Session) Context() context.Context

func (*Session) GetConn

func (s *Session) GetConn() net.Conn

func (*Session) GetID

func (s *Session) GetID() int64

func (*Session) GetNetService

func (s *Session) GetNetService() INetService

func (*Session) GetNodeService

func (s *Session) GetNodeService() INodeService

func (*Session) GetProperty

func (s *Session) GetProperty(key string) interface{}

func (*Session) LocalAddr

func (s *Session) LocalAddr() string

func (*Session) RemoteAddr

func (s *Session) RemoteAddr() string

func (*Session) RemoveProperty

func (s *Session) RemoveProperty(key string)

func (*Session) SendJson

func (s *Session) SendJson(msgID uint32, msg interface{}) error

func (*Session) SendJsonBuffer

func (s *Session) SendJsonBuffer(msgID uint32, data []byte) error

func (*Session) SendProto

func (s *Session) SendProto(msgID uint32, msg *proto.Message) error

func (*Session) SendProtoBuffer

func (s *Session) SendProtoBuffer(msgID uint32, data []byte) error

func (*Session) SetNetService

func (s *Session) SetNetService(service INetService)

func (*Session) SetProperty

func (s *Session) SetProperty(key string, value interface{})

func (*Session) Start

func (s *Session) Start()

func (*Session) Stop

func (s *Session) Stop()

type SessionManager

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

func NewSessionManager

func NewSessionManager() *SessionManager

func (*SessionManager) Add

func (cm *SessionManager) Add(session ISession)

func (*SessionManager) Clear

func (cm *SessionManager) Clear()

func (*SessionManager) ClearSession

func (cm *SessionManager) ClearSession(sID int64)

func (*SessionManager) GetSession

func (cm *SessionManager) GetSession(sessionID int64) (ISession, error)

func (*SessionManager) Len

func (cm *SessionManager) Len() int

func (*SessionManager) Remove

func (cm *SessionManager) Remove(session ISession)

func (*SessionManager) RemoveById

func (cm *SessionManager) RemoveById(sessionID int64)

type Socket

type Socket struct {
	Closed    bool
	IP        string
	Port      int
	IPVersion string
	// contains filtered or unexported fields
}

func NewSocket

func NewSocket(ip string, port int, conn net.Conn) *Socket

func (*Socket) Close

func (s *Socket) Close()

func (*Socket) GetConn

func (s *Socket) GetConn() net.Conn

func (*Socket) LocalAddr

func (s *Socket) LocalAddr() string

func (*Socket) Recv

func (s *Socket) Recv(data []byte) (n int, err error)

func (*Socket) RemoteAddr

func (s *Socket) RemoteAddr() string

func (*Socket) Send

func (s *Socket) Send(data []byte) (n int, err error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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