Class: Slack::RealTime::Concurrency::Celluloid::Socket

Inherits:
Socket
  • Object
show all
Extended by:
Forwardable
Includes:
Celluloid::IO, Celluloid::Internals::Logger
Defined in:
lib/slack/real_time/concurrency/celluloid.rb

Defined Under Namespace

Classes: Actor

Constant Summary collapse

BLOCK_SIZE =
4096

Instance Attribute Summary collapse

Attributes inherited from Socket

#driver, #options, #url

Instance Method Summary collapse

Methods inherited from Socket

#disconnect!, #send_data, #start_sync

Constructor Details

#initialize(*args) ⇒ Socket

Returns a new instance of Socket.



22
23
24
# File 'lib/slack/real_time/concurrency/celluloid.rb', line 22

def initialize(*args)
  super
end

Instance Attribute Details

#socketObject (readonly)

Returns the value of attribute socket.



20
21
22
# File 'lib/slack/real_time/concurrency/celluloid.rb', line 20

def socket
  @socket
end

Instance Method Details

#closeObject



48
49
50
51
52
# File 'lib/slack/real_time/concurrency/celluloid.rb', line 48

def close
  @closing = true
  driver.close
  super
end

#connect!Object



26
27
28
29
# File 'lib/slack/real_time/concurrency/celluloid.rb', line 26

def connect!
  super
  run_loop
end

#connected?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/slack/real_time/concurrency/celluloid.rb', line 79

def connected?
  !@connected.nil?
end

#handle_read(buffer) ⇒ Object



60
61
62
63
# File 'lib/slack/real_time/concurrency/celluloid.rb', line 60

def handle_read(buffer)
  logger.debug("#{self.class}##{__method__}") { buffer }
  driver.parse buffer
end

#readObject

Raises:

  • (EOFError)


54
55
56
57
58
# File 'lib/slack/real_time/concurrency/celluloid.rb', line 54

def read
  buffer = socket.readpartial(BLOCK_SIZE)
  raise EOFError unless buffer && !buffer.empty?
  async.handle_read(buffer)
end

#run_client_loopObject



75
76
77
# File 'lib/slack/real_time/concurrency/celluloid.rb', line 75

def run_client_loop
  @client.run_loop
end

#run_loopObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/slack/real_time/concurrency/celluloid.rb', line 31

def run_loop
  @closing = false
  @socket = build_socket
  @connected = @socket.connect
  driver.start
  loop { read } if socket
rescue EOFError, Errno::ECONNRESET, Errno::EPIPE => e
  logger.debug("#{self.class}##{__method__}") { e }
  driver.emit(:close, WebSocket::Driver::CloseEvent.new(1001, 'server closed connection')) unless @closing
ensure
  begin
    current_actor.terminate if current_actor.alive?
  rescue StandardError
    nil
  end
end

#start_async(client) ⇒ Object



70
71
72
73
# File 'lib/slack/real_time/concurrency/celluloid.rb', line 70

def start_async(client)
  @client = client
  Actor.new(future.run_client_loop)
end

#write(data) ⇒ Object



65
66
67
68
# File 'lib/slack/real_time/concurrency/celluloid.rb', line 65

def write(data)
  logger.debug("#{self.class}##{__method__}") { data }
  socket.write(data)
end