Class: Steam::Networking::Connection
- Inherits:
-
Object
- Object
- Steam::Networking::Connection
- Defined in:
- lib/steam/networking/connection.rb
Overview
Represents a connection to Steam. Holds the session key, yields packets to the caller
Instance Attribute Summary collapse
-
#packets ⇒ Object
readonly
The internal list of Packet objects read from the TCP socket.
Instance Method Summary collapse
-
#disconnect ⇒ Object
Closes the socket.
-
#disconnected? ⇒ Bool
Determine if the connection is disconnected.
-
#each_packet(&block) ⇒ Object
Yields each Packet from the socket to the calling block.
-
#initialize(server, socket = nil) ⇒ Connection
constructor
A new instance of Connection.
-
#open ⇒ Object
Opens the underlying socket.
-
#send_msg(msg) ⇒ Object
Send a Message to the Steam server.
-
#session_key=(v) ⇒ Object
Sets the connections session key.
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
#packets ⇒ Object (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
#disconnect ⇒ Object
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.
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
78 79 80 81 |
# File 'lib/steam/networking/connection.rb', line 78 def each_packet(&block) start_read_thread start_listen_thread(&block) end |
#open ⇒ Object
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.
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 |