Documentation
¶
Index ¶
- Constants
- func BindSID(agent *Agent)
- func BuildSession(agent *Agent, msg *pmessage.Message) *cproto.Session
- func ClusterLocalDataRoute(agent *Agent, session *cproto.Session, route *pmessage.Route, ...) error
- func Count() int
- func DefaultDataRoute(agent *Agent, route *pmessage.Route, msg *pmessage.Message)
- func ForeachAgent(fn func(a *Agent))
- func Kick(iActor cfacade.IActor, agentPath, sid string, reason any, closed bool)
- func KickUID(iActor cfacade.IActor, agentPath string, uid cfacade.UID, reason any, ...)
- func LocalDataRoute(agent *Agent, session *cproto.Session, route *pmessage.Route, ...)
- func Push(iActor cfacade.IActor, agentPath, sid string, uid cfacade.UID, route string, ...)
- func PushWithSID(iActor cfacade.IActor, agentPath, sid, route string, v any)
- func PushWithUID(iActor cfacade.IActor, agentPath string, uid cfacade.UID, route string, v any)
- func PushWithUIDS(iActor cfacade.IActor, agentPath string, uidList []int64, allUID bool, ...)
- func Response(iActor cfacade.IActor, agentPath, sid string, mid uint32, v any)
- func ResponseCode(iActor cfacade.IActor, agentPath, sid string, mid uint32, statusCode int32)
- func Unbind(sid cfacade.SID)
- type Actor
- func (p *Actor) AddConnector(connector cfacade.IConnector)
- func (p *Actor) Connectors() []cfacade.IConnector
- func (p *Actor) Load(app cfacade.IApplication)
- func (p *Actor) OnInit()
- func (*Actor) SetDataCompression(compression bool)
- func (*Actor) SetDictionary(dict map[string]uint16)
- func (*Actor) SetHeartbeat(t time.Duration)
- func (*Actor) SetOnDataRoute(fn DataRouteFunc)
- func (p *Actor) SetOnInitFunc(fn func())
- func (p *Actor) SetOnNewAgent(fn OnNewAgentFunc)
- func (*Actor) SetOnPacket(typ ppacket.Type, fn PacketFunc)
- func (*Actor) SetSysData(key string, value interface{})
- func (*Actor) SetWriteBacklog(size int)
- type ActorBase
- func (p *ActorBase) Kick(session *cproto.Session, reason any, closed bool)
- func (p *ActorBase) Push(session *cproto.Session, route string, v any)
- func (p *ActorBase) PushWithUIDS(agentPath string, uidList []int64, allUID bool, route string, v interface{})
- func (p *ActorBase) Response(session *cproto.Session, v any)
- func (p *ActorBase) ResponseCode(session *cproto.Session, statusCode int32)
- 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, session *cproto.Session) *Agent
- func (a *Agent) AddOnClose(fn OnCloseFunc)
- func (a *Agent) Bind(uid cfacade.UID) (*Agent, error)
- func (a *Agent) Close()
- func (a *Agent) IsBind() bool
- func (a *Agent) IsClosed() bool
- func (a *Agent) Kick(reason interface{}, closed bool)
- func (a *Agent) Push(route string, val interface{})
- func (a *Agent) RemoteAddr() string
- func (a *Agent) Response(session *cproto.Session, v interface{}, isError ...bool)
- func (a *Agent) ResponseCode(session *cproto.Session, statusCode int32, isError ...bool)
- func (a *Agent) ResponseMID(mid uint32, v interface{}, isError ...bool)
- func (a *Agent) Run()
- func (a *Agent) SID() cfacade.SID
- func (a *Agent) SendKick(pkg []byte)
- func (a *Agent) SendPacket(typ pomeloPacket.Type, data []byte)
- func (a *Agent) SendRaw(bytes []byte)
- func (a *Agent) Session() *cproto.Session
- func (a *Agent) SetLastAt()
- func (a *Agent) SetState(state int32) bool
- func (a *Agent) State() int32
- func (a *Agent) UID() cfacade.UID
- func (a *Agent) Unbind()
- type Command
- type DataRouteFunc
- type OnCloseFunc
- type OnNewAgentFunc
- type PacketFunc
Constants ¶
const ( ResponseFuncName = "response" // remote function name for response PushFuncName = "push" // remote function name for push KickFuncName = "kick" // remote function name for kick BroadcastName = "broadcast" // remote function name for broadcast )
const ( AgentInit int32 = 0 // initial state, before handshake AgentWaitAck int32 = 1 // waiting for handshake ack AgentWorking int32 = 2 // normal operation AgentClosed int32 = 4 // closed, no further processing )
Agent lifecycle states.
const ( DataHeartbeat = "heartbeat" // heartbeat interval in seconds DataDict = "dict" // route compression dictionary DataSerializer = "serializer" // serializer name )
System data keys sent in the handshake response.
Variables ¶
This section is empty.
Functions ¶
func ClusterLocalDataRoute ¶
func DefaultDataRoute ¶
DefaultDataRoute default message route handler
func ForeachAgent ¶
func ForeachAgent(fn func(a *Agent))
ForeachAgent iterates over all connected agents.
func Kick ¶ added in v1.3.1
Kick looks up the agent by session id and sends a kick message to the client.
func KickUID ¶ added in v1.4.8
KickUID looks up the agent by user id and sends a kick message to the client.
func LocalDataRoute ¶
func Push ¶ added in v1.3.1
Push looks up the agent by sid or uid and sends a push message to the client.
func PushWithSID ¶ added in v1.4.3
PushWithSID looks up the agent by session id and sends a push message.
func PushWithUID ¶ added in v1.4.3
PushWithUID looks up the agent by user id and sends a push message.
func PushWithUIDS ¶ added in v1.4.8
func PushWithUIDS(iActor cfacade.IActor, agentPath string, uidList []int64, allUID bool, route string, v any)
PushWithUIDS sends a push message to agents matching the uid list or to all connected clients when allUID is true.
func Response ¶ added in v1.3.1
Response looks up the agent by request mid and sends a response payload back to the client.
func ResponseCode ¶ added in v1.3.1
ResponseCode looks up the agent by request mid and sends a status-code response back to the client.
Types ¶
type Actor ¶ added in v1.4.7
func (*Actor) AddConnector ¶ added in v1.4.7
func (p *Actor) AddConnector(connector cfacade.IConnector)
AddConnector registers a connector that will be started when the parser loads.
func (*Actor) Connectors ¶ added in v1.4.7
func (p *Actor) Connectors() []cfacade.IConnector
Connectors returns the list of registered connectors.
func (*Actor) Load ¶ added in v1.4.7
func (p *Actor) Load(app cfacade.IApplication)
Load starts the parser: initializes the command, creates the agent actor, and starts all registered connectors.
func (*Actor) SetDataCompression ¶ added in v1.4.7
SetDataCompression enables or disables data compression for message encoding.
func (*Actor) SetDictionary ¶ added in v1.4.7
SetDictionary sets the route-to-code dictionary used for route compression.
func (*Actor) SetHeartbeat ¶ added in v1.4.7
SetHeartbeat sets the heartbeat interval. Values less than 1 second default to 60 seconds.
func (*Actor) SetOnDataRoute ¶ added in v1.4.7
func (*Actor) SetOnDataRoute(fn DataRouteFunc)
SetOnDataRoute sets the callback that handles routing of incoming data messages. If fn is nil the call is silently ignored and the default handler is kept.
func (*Actor) SetOnInitFunc ¶ added in v1.4.7
func (p *Actor) SetOnInitFunc(fn func())
SetOnInitFunc sets a callback that is invoked during Actor initialization.
func (*Actor) SetOnNewAgent ¶ added in v1.4.7
func (p *Actor) SetOnNewAgent(fn OnNewAgentFunc)
SetOnNewAgent sets the callback that is invoked when a new agent connection is established.
func (*Actor) SetOnPacket ¶ added in v1.4.7
func (*Actor) SetOnPacket(typ ppacket.Type, fn PacketFunc)
SetOnPacket registers a handler for the given packet type. Only installed once per type; subsequent calls for the same type are ignored.
func (*Actor) SetSysData ¶ added in v1.4.7
SetSysData sets a system data key-value pair that is sent during handshake.
func (*Actor) SetWriteBacklog ¶ added in v1.4.7
SetWriteBacklog sets the size of the write and pending channel buffers.
type ActorBase ¶
ActorBase provides convenience methods for actors to respond, push, kick, and broadcast messages to clients. Embed it in your actor to inherit these methods.
func (*ActorBase) PushWithUIDS ¶ added in v1.4.8
func (p *ActorBase) PushWithUIDS(agentPath string, uidList []int64, allUID bool, route string, v interface{})
PushWithUIDS sends a push message to clients matching the given uid list or to all connected clients when allUID is true.
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.3.15
Bind associates a uid with an existing sid. Returns any previously bound agent for the same uid (duplicate login).
func GetAgentWithSID ¶ added in v1.4.3
GetAgentWithSID looks up an agent by session id (public).
func GetAgentWithUID ¶
GetAgentWithUID looks up an agent by user id.
func NewAgent ¶
NewAgent creates an agent for a new connection and binds it into the global sidAgentMap.
func (*Agent) AddOnClose ¶
func (a *Agent) AddOnClose(fn OnCloseFunc)
AddOnClose registers a callback to be invoked during agent teardown. Must be called while the agent is in AgentInit state.
func (*Agent) Bind ¶
Bind associates a uid with this agent's sid. Returns any previously bound agent for the same uid (duplicate login).
func (*Agent) Close ¶
func (a *Agent) Close()
Close signals the agent to shut down by setting state to AgentClosed, closing chDie, and forcing a write deadline to unblock writeChan if it is stuck on conn.Write. Actual teardown (conn.Close, Unbind, callbacks) happens in writeChan's defer via closeClean. Safe to call multiple times — only the first call takes effect.
func (*Agent) IsBind ¶ added in v1.3.4
IsBind returns true if a user has been bound to this session.
func (*Agent) Kick ¶
Kick marshals the reason, encodes a kick packet, and sends it. If closed is true, the agent is torn down after the kick is written.
func (*Agent) RemoteAddr ¶
RemoteAddr returns the client IP address.
func (*Agent) ResponseCode ¶
ResponseCode sends a status-code response for the given session.
func (*Agent) ResponseMID ¶
ResponseMID sends a response payload for the given message id.
func (*Agent) Run ¶
func (a *Agent) Run()
Run starts the two goroutines that manage the agent's lifetime: readChan reads incoming packets, writeChan writes outgoing data and owns teardown.
func (*Agent) SendKick ¶ added in v1.5.3
SendKick enqueues a kick packet via chKick. On success, writeChan writes the bytes then tears down. If the channel buffer is full (writeChan is stuck on conn.Write), it signals Close to unblock and tear down.
func (*Agent) SendPacket ¶ added in v1.3.6
func (a *Agent) SendPacket(typ pomeloPacket.Type, data []byte)
SendPacket encodes data with the given packet type and enqueues it.
func (*Agent) SendRaw ¶
SendRaw enqueues raw bytes for writing. Non-blocking; drops data if the agent is closed or the write buffer is full.
func (*Agent) SetLastAt ¶
func (a *Agent) SetLastAt()
SetLastAt updates the last heartbeat timestamp to now.
func (*Agent) SetState ¶
SetState atomically swaps the agent state. Returns true if the state changed.
type Command ¶
type Command struct {
// contains filtered or unexported fields
}
Command holds the parser-level configuration shared by all agents, including heartbeat timing, handshake payload, and the registered packet/data-route handlers.
type DataRouteFunc ¶
DataRouteFunc is called to route a decoded data message to the target actor.
type OnCloseFunc ¶
type OnCloseFunc func(*Agent)
OnCloseFunc is a callback invoked during agent teardown. Each callback is individually guarded by Try/catch so one panic never skips the rest.
type OnNewAgentFunc ¶
type OnNewAgentFunc func(newAgent *Agent)
OnNewAgentFunc is called when a new agent connection is established.
type PacketFunc ¶
PacketFunc is called when a packet of a registered type arrives.