Class: Steam::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/steam/client.rb

Overview

Represents a Client on the Steam Network. The Client creates a connection to the Steam networks, and send and receives Message objects to that connection.

The Client holds a collection of handlers that react to received packets, if they care about the packet data.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection = nil) ⇒ Client

Returns a new instance of Client.



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/steam/client.rb', line 34

def initialize(connection = nil)
  connection ||= Networking::Connection.new(ServerList.new.to_a.sample)
  @connect_tokens = []
  @steam_user = nil
  @steam_apps = nil
  @game_coordinator = nil
  @steam_id = default_steam_id
  @connection = connection
  @plugins = Plugins.new(self)
  init_handlers
end

Instance Attribute Details

#connect_tokensObject (readonly)

A list of tokens requires for the game coordinator to launch games



23
24
25
# File 'lib/steam/client.rb', line 23

def connect_tokens
  @connect_tokens
end

#connectionObject (readonly)

The internal connection to Steam



11
12
13
# File 'lib/steam/client.rb', line 11

def connection
  @connection
end

#game_coordinatorObject (readonly)

The Handler for the game coordinator events (playing game, etc)



20
21
22
# File 'lib/steam/client.rb', line 20

def game_coordinator
  @game_coordinator
end

#pluginsObject (readonly)

The plugin engine



32
33
34
# File 'lib/steam/client.rb', line 32

def plugins
  @plugins
end

#session_idObject (readonly)

The session id for the current user, nilt if not logged in



29
30
31
# File 'lib/steam/client.rb', line 29

def session_id
  @session_id
end

#steam_appsObject (readonly)

The Handler for Steam app related events (play tokens, etc)



17
18
19
# File 'lib/steam/client.rb', line 17

def steam_apps
  @steam_apps
end

#steam_idObject (readonly)

The SteamId for the current user, nil if not logged in



26
27
28
# File 'lib/steam/client.rb', line 26

def steam_id
  @steam_id
end

#steam_userObject (readonly)

The Handler for Steam user related events (changing name, login, etc)



14
15
16
# File 'lib/steam/client.rb', line 14

def steam_user
  @steam_user
end

Instance Method Details

#create_session(steam_id, session_id) ⇒ Object

Creates a new session for the Client

Parameters:



112
113
114
115
# File 'lib/steam/client.rb', line 112

def create_session(steam_id, session_id)
  @steam_id = CommunityId.new(steam_id)
  @session_id = session_id
end

#gc_message(msg) ⇒ Object

Passes each GC message to any plugins.

Parameters:



151
152
153
154
155
# File 'lib/steam/client.rb', line 151

def gc_message(msg)
  @plugins.loaded_plugins.each do |plugin|
    send(plugin).gc_message(msg)
  end
end

#on_logon(_msg) ⇒ Object

Called by SteamUser handler when the logon message is to be handled. This must be implemented by the subclass

Raises:

  • (NotImplementedError)


48
49
50
# File 'lib/steam/client.rb', line 48

def on_logon(_msg)
  raise NotImplementedError
end

#plugin(plugin) ⇒ Object

Registers a plugin with the Client via the plugin name

Parameters:

  • plugin (Symbol, String)

    the plugin name



61
62
63
# File 'lib/steam/client.rb', line 61

def plugin(plugin)
  @plugins.plugin(plugin.to_sym)
end

#readyObject

Called by SteamUser handler when the user is fully logged in. This must be implemented by the subclass.

Raises:

  • (NotImplementedError)


54
55
56
# File 'lib/steam/client.rb', line 54

def ready
  raise NotImplementedError
end

#restartObject

Stop and start the Client



66
67
68
69
# File 'lib/steam/client.rb', line 66

def restart
  stop
  start
end

#send_msg(msg) ⇒ Object

Sends a Message to the underlying Steam connection. If we have an active session, the session id and steam id is automatically associated with the request

Parameters:



97
98
99
100
101
102
103
104
105
106
# File 'lib/steam/client.rb', line 97

def send_msg(msg)
  Steam.logger.debug("Sending #{msg.body.class.name} to Steam")

  if msg.proto?
    msg.session_id = session_id
    msg.steam_id = steam_id.to_i
  end

  connection.send_msg(msg).nonzero?
end

#session_key=(key) ⇒ Object

Sets the Client's connection session key. Used for encrypting and decrypting packets

Parameters:



130
131
132
# File 'lib/steam/client.rb', line 130

def session_key=(key)
  connection.session_key = key
end

#startObject

Create a new Connection object, and start listening and handling received packets.

Returns:

  • nil



83
84
85
86
87
88
89
90
# File 'lib/steam/client.rb', line 83

def start
  @plugins.load

  connection.open
  connection.each_packet do |packet|
    handle_net_msg(packet)
  end
end

#start_heartbeat(tick) ⇒ Object

Starts the background heartbeat thread.

Parameters:

  • tick (Integer)

    the time to wait between heartbeat messages



138
139
140
141
142
143
144
145
146
# File 'lib/steam/client.rb', line 138

def start_heartbeat(tick)
  Thread.new do |t|
    t.abort_on_exception = true
    until connection.disconnected?
      send_msg(heartbeat_message)
      sleep(tick)
    end
  end
end

#stopObject

Disconnect the Client



72
73
74
75
76
77
# File 'lib/steam/client.rb', line 72

def stop
  steam_user.logoff if @steam_id && @session_id
  connection.disconnect
  @steam_id = default_steam_id
  @session_id = nil
end

#update_connect_tokens(tokens, max) ⇒ Object

Update the Client's internal connection token list

Parameters:

  • tokens (Array<String>)

    the new list of tokens

  • max (Integer)

    the amount of tokens to keep



121
122
123
124
# File 'lib/steam/client.rb', line 121

def update_connect_tokens(tokens, max)
  tokens.each { |t| @connect_tokens.insert(0, t) }
  @connect_tokens = @connect_tokens.take(max)
end