Documentation
¶
Index ¶
- Constants
- Variables
- func AddNodeRoute(mid uint32, nodeRoute *NodeRoute)
- func BindSID(agent *Agent)
- func ClusterLocalDataRoute(agent *Agent, session *cproto.Session, msg *Message, nodeRoute *NodeRoute, ...) error
- func Count() int
- func DefaultDataRoute(agent *Agent, msg *Message, route *NodeRoute)
- func ForeachAgent(fn func(a *Agent))
- func GetSID(uid int64) (cfacade.SID, bool)
- func LocalDataRoute(agent *Agent, session *cproto.Session, msg *Message, nodeRoute *NodeRoute, ...)
- func Response(iActor cfacade.IActor, session *cproto.Session, mid uint32, v interface{})
- func SetEndian(e binary.ByteOrder)
- func SetHeartbeatTime(t time.Duration)
- func SetWriteBacklog(backlog int)
- func Unbind(sid cfacade.SID)
- type Actor
- func (p *Actor) AddConnector(connector cfacade.IConnector)
- func (p *Actor) AddNodeRoute(mid uint32, nodeRoute *NodeRoute)
- func (p *Actor) Connectors() []cfacade.IConnector
- func (p *Actor) Load(app cfacade.IApplication)
- func (p *Actor) OnInit()
- func (p *Actor) SetEndian(e binary.ByteOrder)
- func (p *Actor) SetHeartbeatTime(t time.Duration)
- func (*Actor) SetOnDataRoute(fn DataRouteFunc)
- func (p *Actor) SetOnNewAgent(fn OnNewAgentFunc)
- func (p *Actor) SetWriteBacklog(backlog int)
- type ActorBase
- type Agent
- func Bind(sid cfacade.SID, uid cfacade.UID) (*Agent, error)
- func GetAgent(sid string, uid cfacade.UID) (*Agent, bool)
- func GetAgentWithSID(sid cfacade.SID) (*Agent, bool)
- func GetAgentWithUID(uid cfacade.UID) (*Agent, bool)
- func NewAgent(app cfacade.IApplication, conn net.Conn, sid cfacade.SID, agentPath string) *Agent
- func (a *Agent) AddOnClose(fn OnCloseFunc)
- func (a *Agent) AgentPath() string
- func (a *Agent) Bind(uid cfacade.UID) (*Agent, error)
- func (a *Agent) Close()
- func (a *Agent) Data() *cproto.SessionData
- func (a *Agent) GetData(key string) string
- func (a *Agent) IP() string
- func (a *Agent) IsClosed() bool
- func (a *Agent) Kick(mid uint32, reason interface{}, closed bool)
- func (a *Agent) NewSession(mid uint32) *cproto.Session
- func (a *Agent) RemoteAddr() string
- func (a *Agent) Response(mid uint32, v interface{})
- func (a *Agent) Run()
- func (a *Agent) SID() cfacade.SID
- func (a *Agent) SendKick(pkg []byte)
- func (a *Agent) SendRaw(bytes []byte)
- func (a *Agent) SetData(key, value string)
- func (a *Agent) SetLastAt()
- func (a *Agent) SetState(state int32) bool
- func (a *Agent) SetUID(uid cfacade.UID)
- func (a *Agent) State() int32
- func (a *Agent) UID() cfacade.UID
- func (a *Agent) Unbind()
- type DataRouteFunc
- type Message
- type NodeRoute
- type OnCloseFunc
- type OnNewAgentFunc
Constants ¶
const ( AgentInit int32 = 0 // initial state AgentClosed int32 = 4 // closed, no further processing )
Agent lifecycle states.
const (
ResponseFuncName = "response"
)
Remote function name constants.
Variables ¶
var (
NoneMessage = Message{} // zero-value sentinel
)
Package-level message constants.
Functions ¶
func AddNodeRoute ¶
AddNodeRoute maps a mid (message id) to a NodeRoute for routing incoming messages.
func ClusterLocalDataRoute ¶
func ClusterLocalDataRoute(agent *Agent, session *cproto.Session, msg *Message, nodeRoute *NodeRoute, nodeID, targetPath string) error
ClusterLocalDataRoute publishes a message to a local actor on a remote node via the cluster.
func DefaultDataRoute ¶
DefaultDataRoute is the default message routing handler. It dispatches locally when the target node type matches the agent's node, or forwards to a random member of the target node type via the cluster.
func ForeachAgent ¶
func ForeachAgent(fn func(a *Agent))
ForeachAgent iterates over all connected agents.
func LocalDataRoute ¶
func LocalDataRoute(agent *Agent, session *cproto.Session, msg *Message, nodeRoute *NodeRoute, targetPath string)
LocalDataRoute posts a message to a local actor on this node.
func Response ¶
Response looks up the agent by the session and sends a response payload back to the client.
func SetHeartbeatTime ¶
SetHeartbeatTime sets the heartbeat interval for agent connections. Values less than 1 second are ignored.
func SetWriteBacklog ¶
func SetWriteBacklog(backlog int)
SetWriteBacklog sets the size of the write and pending channel buffers. Values less than or equal to 0 are ignored.
Types ¶
type Actor ¶ added in v1.6.7
func (*Actor) AddConnector ¶ added in v1.6.7
func (p *Actor) AddConnector(connector cfacade.IConnector)
AddConnector registers a connector that will be started when the parser loads.
func (*Actor) AddNodeRoute ¶ added in v1.6.7
AddNodeRoute maps a mid (message id) to a NodeRoute for routing incoming messages.
func (*Actor) Connectors ¶ added in v1.6.7
func (p *Actor) Connectors() []cfacade.IConnector
Connectors returns the list of registered connectors.
func (*Actor) Load ¶ added in v1.6.7
func (p *Actor) Load(app cfacade.IApplication)
Load starts the parser: creates the agent actor and starts all registered connectors.
func (*Actor) SetEndian ¶ added in v1.6.7
SetEndian sets the byte order used for encoding/decoding message headers.
func (*Actor) SetHeartbeatTime ¶ added in v1.6.7
SetHeartbeatTime sets the heartbeat interval for agent connections.
func (*Actor) SetOnDataRoute ¶ added in v1.6.7
func (*Actor) SetOnDataRoute(fn DataRouteFunc)
SetOnDataRoute sets the callback that handles routing of incoming data messages.
func (*Actor) SetOnNewAgent ¶ added in v1.6.7
func (p *Actor) SetOnNewAgent(fn OnNewAgentFunc)
SetOnNewAgent sets the callback that is invoked when a new agent connection is established.
func (*Actor) SetWriteBacklog ¶ added in v1.6.7
SetWriteBacklog sets the size of the write and pending channel buffers.
type Agent ¶
type Agent struct {
cfacade.IApplication // app
// contains filtered or unexported fields
}
Agent represents a single client connection. All conn.Write and teardown operations happen exclusively in writeChan's goroutine.
func Bind ¶ added in v1.5.3
Bind associates a uid with an existing sid. Returns any previously bound agent for the same uid (duplicate login).
func GetAgentWithSID ¶ added in v1.5.3
GetAgentWithSID looks up an agent by session id.
func GetAgentWithUID ¶
GetAgentWithUID looks up an agent by user id.
func NewAgent ¶
NewAgent creates an agent for a new connection. It is the only way to build a usable one: every field but the embedded application is unexported, so an Agent literal written elsewhere has no connection, no channels and no session data. Binding it into the lookup maps afterwards is the caller's job — see BindSID.
sid and agentPath are connection identity: they are captured here and never change, so any goroutine may read them. The session key/values start empty and are reached through Data.
func (*Agent) AddOnClose ¶
func (a *Agent) AddOnClose(fn OnCloseFunc)
func (*Agent) AgentPath ¶ added in v1.6.7
AgentPath returns the parser actor path this connection belongs to.
func (*Agent) Data ¶ added in v1.6.7
func (a *Agent) Data() *cproto.SessionData
Data returns this connection's session key/values. Its methods are safe to call from any goroutine.
func (*Agent) GetData ¶ added in v1.6.7
GetData reads one session value off this connection — shorthand for Data().Get, for the common case of reading a single key.
func (*Agent) NewSession ¶ added in v1.6.7
NewSession builds a session for one message of this connection: a new object carrying the connection's identity, a copy of its session data, and mid.
A new object on every call is the point: writing the message id onto one session shared by the connection's goroutines is a data race. Pass 0 where a session is needed but no message is being routed.
func (*Agent) RemoteAddr ¶
func (*Agent) SetData ¶ added in v1.6.7
SetData writes one session value on this connection — shorthand for Data().Set.
type DataRouteFunc ¶
DataRouteFunc is called to route a decoded message to the target actor.
type Message ¶
type Message struct {
MID uint32 // message id for request/response matching
Len uint32 // length of the data payload
Data []byte // payload bytes
}
Message represents a decoded simple-protocol message. The wire format is: MID(4 bytes) + DataLen(4 bytes) + Data(DataLen bytes).
type NodeRoute ¶
type NodeRoute struct {
NodeType string // target node type
ActorID string // target actor id
FuncName string // target function name
}
NodeRoute describes the target actor and function for a given message id.
func GetNodeRoute ¶
GetNodeRoute returns the NodeRoute for the given message id.
type OnCloseFunc ¶
type OnCloseFunc func(*Agent)
type OnNewAgentFunc ¶
type OnNewAgentFunc func(newAgent *Agent)