Class: SmartFox::Socket::Connection
- Inherits:
-
Object
- Object
- SmartFox::Socket::Connection
- Defined in:
- lib/smartfox/socket/connection.rb
Constant Summary collapse
- DEFAULT_PORT =
9339
Instance Attribute Summary collapse
-
#connected ⇒ Object
(also: #connected?)
readonly
Returns the value of attribute connected.
Instance Method Summary collapse
- #connect ⇒ Object
- #data_available? ⇒ Boolean
- #disconnect ⇒ Object
- #event_loop ⇒ Object
-
#initialize(client) ⇒ Connection
constructor
A new instance of Connection.
- #inspect ⇒ Object
- #read_packet ⇒ Object
- #send_data(data) ⇒ Object
Constructor Details
#initialize(client) ⇒ Connection
8 9 10 11 12 13 14 |
# File 'lib/smartfox/socket/connection.rb', line 8 def initialize(client) @client = client @connected = false @event_thread = nil @disconnecting = false @buffer = String.new end |
Instance Attribute Details
#connected ⇒ Object (readonly) Also known as: connected?
Returns the value of attribute connected.
5 6 7 |
# File 'lib/smartfox/socket/connection.rb', line 5 def connected @connected end |
Instance Method Details
#connect ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/smartfox/socket/connection.rb', line 16 def connect begin SmartFox::Logger.info "SmartFox::Socket::Connection#connect began" @socket = TCPSocket.new(@client.server, @client.port || DEFAULT_PORT) # SmartFox sockets will send a cross domain policy, receive this packet # before we do anything else or the connection will hang @socket.readpartial(4096) @connected = true @event_thread = Thread.start(self) { |connection| connection.event_loop } @client.connect_succeeded rescue => e SmartFox::Logger.error "In SmartFox::Socket::Connection#connect:" SmartFox::Logger.error " #{e.inspect}" end end |
#data_available? ⇒ Boolean
44 45 46 |
# File 'lib/smartfox/socket/connection.rb', line 44 def data_available? not @buffer.empty? end |
#disconnect ⇒ Object
33 34 35 36 37 38 |
# File 'lib/smartfox/socket/connection.rb', line 33 def disconnect @disconnecting = true while @connected sleep end end |
#event_loop ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/smartfox/socket/connection.rb', line 54 def event_loop SmartFox::Logger.info "SmartFox::Socket::Connection#event_loop began" ticks = 0 until @disconnecting SmartFox::Logger.debug "SmartFox::Socket::Connection#event_loop tick #{ticks}" @buffer << @socket.readpartial(4096) ticks += 1 end @connected = false end |
#inspect ⇒ Object
68 69 70 |
# File 'lib/smartfox/socket/connection.rb', line 68 def inspect "#<#{self.class.name}:#{object_id} server:#{@client.server} port:#{@client.port || DEFAULT_PORT}>" end |
#read_packet ⇒ Object
48 49 50 51 52 |
# File 'lib/smartfox/socket/connection.rb', line 48 def read_packet packet = @buffer @buffer = "" return packet end |
#send_data(data) ⇒ Object
40 41 42 |
# File 'lib/smartfox/socket/connection.rb', line 40 def send_data(data) @socket.write(data) end |