pomelo

package
v1.5.3 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2026 License: MIT Imports: 19 Imported by: 7

Documentation

Index

Constants

View Source
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
)
View Source
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.

View Source
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 BindSID

func BindSID(agent *Agent)

BindSID registers an agent in the sid lookup map.

func BuildSession

func BuildSession(agent *Agent, msg *pmessage.Message) *cproto.Session

func ClusterLocalDataRoute

func ClusterLocalDataRoute(agent *Agent, session *cproto.Session, route *pmessage.Route, msg *pmessage.Message, nodeID, targetPath string) error

func Count

func Count() int

Count returns the total number of connected agents.

func DefaultDataRoute

func DefaultDataRoute(agent *Agent, route *pmessage.Route, msg *pmessage.Message)

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

func Kick(iActor cfacade.IActor, agentPath, sid string, reason any, closed bool)

Kick looks up the agent by session id and sends a kick message to the client.

func KickUID added in v1.4.8

func KickUID(iActor cfacade.IActor, agentPath string, uid cfacade.UID, reason any, closed bool)

KickUID looks up the agent by user id and sends a kick message to the client.

func LocalDataRoute

func LocalDataRoute(agent *Agent, session *cproto.Session, route *pmessage.Route, msg *pmessage.Message, targetPath string)

func Push added in v1.3.1

func Push(iActor cfacade.IActor, agentPath, sid string, uid cfacade.UID, route string, v any)

Push looks up the agent by sid or uid and sends a push message to the client.

func PushWithSID added in v1.4.3

func PushWithSID(iActor cfacade.IActor, agentPath, sid, route string, v any)

PushWithSID looks up the agent by session id and sends a push message.

func PushWithUID added in v1.4.3

func PushWithUID(iActor cfacade.IActor, agentPath string, uid cfacade.UID, route string, v any)

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

func Response(iActor cfacade.IActor, agentPath, sid string, mid uint32, v any)

Response looks up the agent by request mid and sends a response payload back to the client.

func ResponseCode added in v1.3.1

func ResponseCode(iActor cfacade.IActor, agentPath, sid string, mid uint32, statusCode int32)

ResponseCode looks up the agent by request mid and sends a status-code response back to the client.

func Unbind

func Unbind(sid cfacade.SID)

Unbind removes the sid→agent mapping and cleans up the uid→sid entry if it still points to this sid.

Types

type Actor added in v1.4.7

type Actor struct {
	cactor.Base
	// contains filtered or unexported fields
}

func NewActor

func NewActor(agentActorID string) *Actor

NewActor creates a new pomelo parser Actor with the given agent actor id.

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) OnInit added in v1.4.7

func (p *Actor) OnInit()

OnInit Actor初始化前触发该函数

func (*Actor) SetDataCompression added in v1.4.7

func (*Actor) SetDataCompression(compression bool)

SetDataCompression enables or disables data compression for message encoding.

func (*Actor) SetDictionary added in v1.4.7

func (*Actor) SetDictionary(dict map[string]uint16)

SetDictionary sets the route-to-code dictionary used for route compression.

func (*Actor) SetHeartbeat added in v1.4.7

func (*Actor) SetHeartbeat(t time.Duration)

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

func (*Actor) SetSysData(key string, value interface{})

SetSysData sets a system data key-value pair that is sent during handshake.

func (*Actor) SetWriteBacklog added in v1.4.7

func (*Actor) SetWriteBacklog(size int)

SetWriteBacklog sets the size of the write and pending channel buffers.

type ActorBase

type ActorBase struct {
	cactor.Base
}

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) Kick

func (p *ActorBase) Kick(session *cproto.Session, reason any, closed bool)

Kick sends a kick message to the client identified by the session's sid.

func (*ActorBase) Push

func (p *ActorBase) Push(session *cproto.Session, route string, v any)

Push sends a push message to the client identified by the session's sid.

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.

func (*ActorBase) Response

func (p *ActorBase) Response(session *cproto.Session, v any)

Response sends a response payload back to the client for the given session.

func (*ActorBase) ResponseCode

func (p *ActorBase) ResponseCode(session *cproto.Session, statusCode int32)

ResponseCode sends a status-code response back to the client for the given session.

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

func Bind(sid cfacade.SID, uid cfacade.UID) (*Agent, error)

Bind associates a uid with an existing sid. Returns any previously bound agent for the same uid (duplicate login).

func GetAgent

func GetAgent(sid string, uid cfacade.UID) (*Agent, bool)

GetAgent resolves an agent by sid first, falling back to uid.

func GetAgentWithSID added in v1.4.3

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

GetAgentWithSID looks up an agent by session id (public).

func GetAgentWithUID

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

GetAgentWithUID looks up an agent by user id.

func NewAgent

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

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

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

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

func (a *Agent) IsBind() bool

IsBind returns true if a user has been bound to this session.

func (*Agent) IsClosed added in v1.5.3

func (a *Agent) IsClosed() bool

IsClosed returns true if the agent has been closed.

func (*Agent) Kick

func (a *Agent) Kick(reason interface{}, closed bool)

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) Push

func (a *Agent) Push(route string, val interface{})

Push sends a server-push message to the client on the given route.

func (*Agent) RemoteAddr

func (a *Agent) RemoteAddr() string

RemoteAddr returns the client IP address.

func (*Agent) Response

func (a *Agent) Response(session *cproto.Session, v interface{}, isError ...bool)

Response sends a response payload for the given session.

func (*Agent) ResponseCode

func (a *Agent) ResponseCode(session *cproto.Session, statusCode int32, isError ...bool)

ResponseCode sends a status-code response for the given session.

func (*Agent) ResponseMID

func (a *Agent) ResponseMID(mid uint32, v interface{}, isError ...bool)

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) SID

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

SID returns the session id.

func (*Agent) SendKick added in v1.5.3

func (a *Agent) SendKick(pkg []byte)

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

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

SendRaw enqueues raw bytes for writing. Non-blocking; drops data if the agent is closed or the write buffer is full.

func (*Agent) Session

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

Session returns the session associated with this agent.

func (*Agent) SetLastAt

func (a *Agent) SetLastAt()

SetLastAt updates the last heartbeat timestamp to now.

func (*Agent) SetState

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

SetState atomically swaps the agent state. Returns true if the state changed.

func (*Agent) State

func (a *Agent) State() int32

State returns the current agent lifecycle state.

func (*Agent) UID

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

UID returns the bound user id.

func (*Agent) Unbind

func (a *Agent) Unbind()

Unbind removes this agent from the global sid and uid maps.

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

type DataRouteFunc func(agent *Agent, route *pmessage.Route, msg *pmessage.Message)

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

type PacketFunc func(agent *Agent, packet *ppacket.Packet)

PacketFunc is called when a packet of a registered type arrives.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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