This document describes the foundational methods of the Dota2 client struct that provide essential functionality for client initialization, message handling, state management, and communication with the Game Coordinator (GC). These are manually-written methods that form the basis of the client's operation, in contrast to the ~170 generated API methods.
The Dota2 client struct maintains several key components that enable its operation:
| Field | Type | Purpose |
|---|---|---|
le | logrus.FieldLogger | Structured logging client.go33 |
client | *steam.Client | Underlying Steam client for network transport client.go34 |
cache | *socache.SOCache | Shared Object Cache for game state client.go35 |
connectionCtx | context.Context | Active session context, nil when disconnected client.go38 |
connectionCtxCancel | context.CancelFunc | Cancellation function for active session client.go39 |
state | state.Dota2State | Current client state (connection status, etc.) client.go42 |
handlers | handlerMap | Map of message type IDs to handler functions client.go44 |
pendReq | map[uint32]map[uint32]responseHandler | Pending request-response handlers client.go48 |
pendReqID | uint32 | Request ID counter for request-response tracking client.go47 |
Sources: client.go32-49
The New() function constructs a new Dota2 client instance. It performs the following initialization:
SOCache instance for shared object management client.go55GCConnectionStatus_GCConnectionStatus_NO_SESSION client.go59-61buildHandlerMap() client.go63The returned client is ready to receive packets but has no active session until SayHello() is called.
Sources: client.go51-66
Title: "Dota2 Client Initialization Sequence"
Sources: client.go51-66 client.go82-113
Returns the client's SOCache instance, which manages subscribed shared objects such as lobbies, parties, and other game state. Applications use this to subscribe to cache changes and query current state.
Sources: client.go68-71
Terminates all ongoing operations by canceling the connection context. This causes all pending request-response operations to fail with context cancellation errors. The method acquires the connection context mutex to safely cancel the context if it exists.
Sources: client.go73-80
Informs Steam whether the client is currently "playing" Dota 2 (AppID 570).
playing is true, it calls client.GC.SetGamesPlayed(AppID) client_session.go16playing is false, it clears the games played status and resets the internal client state via ns.ClearState() client_session.go18-21Sources: client_session.go14-24
Sends a CMsgClientHello message to the Game Coordinator to initiate a session. It specifies the Source 2 engine (k_ESE_Source2) and provides any existing cache versions to the GC client_session.go27-38
Sources: client_session.go27-38
Processes incoming Game Coordinator packets. This is the central dispatch point for all GC messages. The method:
AppId == 570 client.go157-159d.handlers map using packet.MsgType client.go162handleResponsePacket() to check if this is a response to a pending request client.go170UnhandledGCPacket event if no handler processed the packet client.go173-176Sources: client.go155-177
Title: "GCPacket Processing Pipeline"
Sources: client.go155-177
Internal method that sends a protobuf message to the Game Coordinator via client.GC.Write. Wraps the message in a GCMsgProtobuf with AppID 570.
Sources: client.go115-118
Internal method that emits an event to the Steam client's event bus. Applications can subscribe to these events using the Steam client's event system.
Sources: client.go120-123
Thread-safe state accessor that manages the Dota2State struct. If the callback returns changed=true, it emits a ClientStateChanged event containing both the OldState and NewState.
Sources: client.go125-142
Unmarshals a packet's body into a protobuf message. Includes error logging with the message type for debugging.
Sources: client.go144-153
Title: "Dota2State Access and Event Emission"
Sources: client.go125-142
Constructs the handlerMap that routes incoming message type IDs to handler functions. The map includes:
k_EMsgGCClientWelcome, k_EMsgGCClientConnectionStatus client.go86-87k_ESOMsg_CacheSubscribed, k_ESOMsg_UpdateMultiple, etc. client.go90-93k_EMsgGCPingRequest client.go96getEventEmitter client.go99-109The method concludes by calling registerGeneratedHandlers() to add generated API handlers. client.go112
Sources: client.go82-113
Returns a handler function that unmarshals a packet into an event object and emits it. This factory function is used to create simple handlers for messages that only need to be converted to events.
Sources: client.go185-196
Handles ping requests from the GC by immediately sending a CMsgGCClientPing response to keep the session alive.
Sources: client.go179-183
Internal method to update the connection status in the state. If the status transitions to HAVE_SESSION, it initializes a new connectionCtx with cancellation client_session.go125-128 It also clears the internal state (lobbies, parties) whenever the status changes client_session.go117
Sources: client_session.go98-131
The core client methods provide the foundation for all Dota 2 Game Coordinator operations:
| Method | Purpose | Public |
|---|---|---|
New() | Initialize client with handlers and cache | Yes client.go52-66 |
GetCache() | Access SOCache for game state | Yes client.go68-71 |
Close() | Cancel ongoing operations | Yes client.go73-80 |
SetPlaying() | Inform Steam of Dota 2 app status | Yes client_session.go14-24 |
SayHello() | Request session from GC | Yes client_session.go27-38 |
HandleGCPacket() | Dispatch incoming GC messages | Yes client.go156-177 |
write() | Send protobuf messages to GC | Internal client.go115-118 |
emit() | Emit events to application | Internal client.go120-123 |
accessState() | Thread-safe state access | Internal client.go125-142 |
unmarshalBody() | Deserialize packet bodies | Internal client.go144-153 |
Sources: client.go1-196 client_session.go1-131
Refresh this wiki