simple

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2024 License: MIT Imports: 17 Imported by: 0

README

简单化版的网络数据包解析实现

  • 起因是网友@刘在Q群中反馈,需要一个更简单的网络数据包结构。
  • 本结构只是一个自定义网络数据包的演示。在实际开发中各位可根据自身需求进行定制。
  • 代码未做详细测试,并且缺少对应的客户端实现。在使用中有任何问题欢迎反馈。
包结构
  • 本结构参照zinx的网络包结构,通过消息ID + 数据长度 + 数据构建一个网络数据包
  • MID uint32(4 bytes) + DataLen uint32(4 bytes) + Data(n bytes)
使用方法
  • 在网关节点构建一个simple的网络数据包解析器
  • 通过simple.AddNodeRoute(mid,&NodeRoute{...})构造数据包路由策略
  • 示例代码
示例代码
// 构建简单的网络数据包解析器
func buildSimpleParser(app *cherry.AppBuilder) cfacade.INetParser {
    agentActor := simple.NewActor("user")
    agentActor.AddConnector(cconnector.NewTCP(":10011"))
    agentActor.AddConnector(cconnector.NewWS(app.Address()))
	
        agentActor.SetOnNewAgent(func(newAgent *simple.Agent) {
        childActor := &ActorAgent{}
        //newAgent.AddOnClose(childActor.onSessionClose)
        agentActor.Child().Create(newAgent.SID(), childActor)
    })

    // 设置大头&小头
    agentActor.SetEndian(binary.LittleEndian)
    // 设置心跳时间
    agentActor.SetHeartbeatTime(60 * time.Second)
    // 设置积压消息数量
    agentActor.SetWriteBacklog(64)

    // 设置数据路由函数
    //agentActor.SetOnDataRoute(onSimpleDataRoute)

    // 设置消息节点路由(建议配合data-config配置表使用)
    // mid = 1 的消息路由到  gate节点.user的Actor.login函数上
    agentActor.AddNodeRoute(1, &simple.NodeRoute{
        NodeType: "gate",
        ActorID:  "user",
        FuncName: "login",
    })
	
    return agentActor
}

Documentation

Index

Constants

View Source
const (
	AgentInit   int32 = 0
	AgentClosed int32 = 3
)
View Source
const (
	ResponseFuncName = "response"
)

Variables

View Source
var (
	NoneMessage = Message{} // none message

)

Functions

func AddNodeRoute

func AddNodeRoute(mid uint32, nodeRoute *NodeRoute)

func BindSID

func BindSID(agent *Agent)

func BindUID

func BindUID(sid cfacade.SID, uid cfacade.UID) error

func ClusterLocalDataRoute

func ClusterLocalDataRoute(agent *Agent, session *cproto.Session, msg *Message, nodeRoute *NodeRoute, nodeID, targetPath string) error

func Count

func Count() int

func DefaultDataRoute

func DefaultDataRoute(agent *Agent, msg *Message, route *NodeRoute)

func ForeachAgent

func ForeachAgent(fn func(a *Agent))

func LocalDataRoute

func LocalDataRoute(agent *Agent, session *cproto.Session, msg *Message, nodeRoute *NodeRoute, targetPath string)

func NewActor

func NewActor(agentActorID string) *actor

func Response

func Response(iActor cfacade.IActor, session *cproto.Session, mid uint32, v interface{})

func SetEndian

func SetEndian(e binary.ByteOrder)

func SetHeartbeatTime

func SetHeartbeatTime(t time.Duration)

func SetWriteBacklog

func SetWriteBacklog(backlog int)

func Unbind

func Unbind(sid cfacade.SID)

Types

type ActorBase

type ActorBase struct {
	cactor.Base
}

func (*ActorBase) Response

func (p *ActorBase) Response(session *cproto.Session, mid uint32, v interface{})

type Agent

type Agent struct {
	cfacade.IApplication // app
	// contains filtered or unexported fields
}

func GetAgent

func GetAgent(sid cfacade.SID) (*Agent, bool)

func GetAgentWithUID

func GetAgentWithUID(uid cfacade.UID) (*Agent, bool)

func NewAgent

func NewAgent(app cfacade.IApplication, conn net.Conn, session *cproto.Session) Agent

func (*Agent) AddOnClose

func (a *Agent) AddOnClose(fn OnCloseFunc)

func (*Agent) Bind

func (a *Agent) Bind(uid cfacade.UID) error

func (*Agent) Close

func (a *Agent) Close()

func (*Agent) RemoteAddr

func (a *Agent) RemoteAddr() string

func (*Agent) Response

func (a *Agent) Response(mid uint32, v interface{})

func (*Agent) Run

func (a *Agent) Run()

func (*Agent) SID

func (a *Agent) SID() cfacade.SID

func (*Agent) SendRaw

func (a *Agent) SendRaw(bytes []byte)

func (*Agent) Session

func (a *Agent) Session() *cproto.Session

func (*Agent) SetLastAt

func (a *Agent) SetLastAt()

func (*Agent) SetState

func (a *Agent) SetState(state int32) bool

func (*Agent) State

func (a *Agent) State() int32

func (*Agent) UID

func (a *Agent) UID() cfacade.UID

func (*Agent) Unbind

func (a *Agent) Unbind()

type DataRouteFunc

type DataRouteFunc func(agent *Agent, msg *Message, route *NodeRoute)

type Message

type Message struct {
	MID  uint32
	Len  uint32
	Data []byte
}

func ReadMessage

func ReadMessage(conn net.Conn) (Message, bool, error)

type NodeRoute

type NodeRoute struct {
	NodeType string
	ActorID  string
	FuncName string
}

func GetNodeRoute

func GetNodeRoute(mid uint32) (*NodeRoute, bool)

type OnCloseFunc

type OnCloseFunc func(*Agent)

type OnNewAgentFunc

type OnNewAgentFunc func(newAgent *Agent)

Jump to

Keyboard shortcuts

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