Class: Steam::Networking::Connection

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

Overview

Represents a connection to Steam. Holds the session key, yields packets to the caller

Examples:

Creating a Connection

connection = Connection.new(Server.new(ip, port))
connection.each_packet do |packet|
end

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server, socket = nil) ⇒ Connection

Returns a new instance of Connection.



20
21
22
23
24
25
26
27
# File 'lib/steam/networking/connection.rb', line 20

def initialize(server, socket = nil)
  @session_key = nil
  @packets = PacketList.new
  @socket = socket
  @mutex = Mutex.new
  @server = server
  self
end

Instance Attribute Details

#packetsObject (readonly)

The internal list of Packet objects read from the TCP socket



18
19
20
# File 'lib/steam/networking/connection.rb', line 18

def packets
  @packets
end

Instance Method Details

#disconnectObject

Closes the socket



37
38
39
40
41
# File 'lib/steam/networking/connection.rb', line 37

def disconnect
  self.session_key = nil
  self.disconnecting = true
  socket.close
end

#disconnected?Bool

Determine if the connection is disconnected. This is determined by the socket being opened or close.

Returns:

  • (Bool)


47
48
49
# File 'lib/steam/networking/connection.rb', line 47

def disconnected?
  socket.closed?
end

#each_packet(&block) ⇒ Object

Yields each Packet from the socket to the calling block.

Starts a thread for reading from the socket into a queue. And another thread to consume that queue. Packets that are consumed from the queue will be yielded to the block

See Also:



78
79
80
81
# File 'lib/steam/networking/connection.rb', line 78

def each_packet(&block)
  start_read_thread
  start_listen_thread(&block)
end

#openObject

Opens the underlying socket



30
31
32
33
34
# File 'lib/steam/networking/connection.rb', line 30

def open
  self.session_key = nil
  self.disconnecting = false
  self.socket = TCPSocket.open(@server.host, @server.port)
end

#send_msg(msg) ⇒ Object

Send a Message to the Steam server. If we are in an encrypted session the Packet body is encrypted before it is sent.

Parameters:

  • msg (Networking::Message)

    the message to send

See Also:



63
64
65
66
67
68
69
# File 'lib/steam/networking/connection.rb', line 63

def send_msg(msg)
  data = msg.encode
  data = Crypto.encrypt(data, session_key) if session_key

  packet = Packet.new(data)
  write_socket(packet.encode)
end

#session_key=(v) ⇒ Object

Sets the connections session key. Used for crypto



52
53
54
# File 'lib/steam/networking/connection.rb', line 52

def session_key=(v)
  @mutex.synchronize { @session_key = v }
end