Class: SteamMist::Rcon::Listener

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/steam_mist/rcon/listener.rb

Overview

Listens on a TCP stream for packets. This is completely synchronus.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ip, port) ⇒ Listener

Initialize the listener.

Parameters:

  • ip (String)

    the ip address to bind to.

  • port (Numeric)

    the port to bind to.



28
29
30
31
32
# File 'lib/steam_mist/rcon/listener.rb', line 28

def initialize(ip, port)
  @ip     = ip
  @port   = port
  @closed = false
end

Instance Attribute Details

#connectionTCPSocket (readonly)

The connection the listener is using.

Returns:

  • (TCPSocket)


22
23
24
# File 'lib/steam_mist/rcon/listener.rb', line 22

def connection
  @connection
end

#ipString

The ip address this listener is bound to.

Returns:

  • (String)


12
13
14
# File 'lib/steam_mist/rcon/listener.rb', line 12

def ip
  @ip
end

#portNumeric

The port the listener is bound to.

Returns:

  • (Numeric)


17
18
19
# File 'lib/steam_mist/rcon/listener.rb', line 17

def port
  @port
end

Instance Method Details

#bind!void

This method returns an undefined value.

Connect to the set port and ip address.



37
38
39
# File 'lib/steam_mist/rcon/listener.rb', line 37

def bind!
  @connection = TCPSocket.new ip, port
end

#closevoid

This method returns an undefined value.

Closes the connection.



85
86
87
88
89
90
# File 'lib/steam_mist/rcon/listener.rb', line 85

def close
  unless @closed
    @closed = true
    connection.close
  end
end

#closed?Boolean

This returns true or false depending on whether or not the connection is closed.

Returns:

  • (Boolean)


96
97
98
# File 'lib/steam_mist/rcon/listener.rb', line 96

def closed?
  @closed
end

#on_data {|socket| ... } ⇒ void

This method returns an undefined value.

Listens to the connection for any data; when there is some, it’ll call the block and then return.

Yield Parameters:

  • socket (TCPSocket)

Yield Returns:

  • (void)

Raises:



72
73
74
75
76
77
78
79
80
# File 'lib/steam_mist/rcon/listener.rb', line 72

def on_data
  return false if closed?

  result = IO.select [connection], [], [], 10

  raise TimeoutError, "timeout" unless result

  yield connection
end

#write(*args, &block) ⇒ Object

This passes the write through to the connection if the connection isn’t closed.



102
103
104
105
106
# File 'lib/steam_mist/rcon/listener.rb', line 102

def write(*args, &block)
  return false if closed?
  
  connection.write(*args, &block)
end