Class: Wamp::Router::Connection

Inherits:
Client
  • Object
show all
Includes:
WebSocket::Driver::EventEmitter
Defined in:
lib/wamp/router/connection.rb

Overview

TOP Level Doc

Constant Summary collapse

CONNECTING =
0
OPEN =
1
CLOSING =
2
CLOSED =
3

Instance Attribute Summary collapse

Attributes inherited from Client

#router

Instance Method Summary collapse

Methods inherited from Client

#on_message, #realm, #send_message, #session_id

Constructor Details

#initialize(socket, &cleanup) ⇒ Connection

Returns a new instance of Connection.



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/wamp/router/connection.rb', line 24

def initialize(socket, &cleanup)
  super()
  @cleanup = cleanup
  @socket = socket
  @driver = WebSocket::Driver.server(self)
  @driver.on(:open) { on_open(_1) }
  @driver.on(:message) { on_message(_1.data) }
  @driver.on(:close) { |evt| begin_close(evt.reason, evt.code) }
  @driver.on(:connect) { on_connect }
  @ready_state = CONNECTING
end

Instance Attribute Details

#acceptorObject (readonly)

Returns the value of attribute acceptor.



22
23
24
# File 'lib/wamp/router/connection.rb', line 22

def acceptor
  @acceptor
end

#sessionObject (readonly)

Returns the value of attribute session.



22
23
24
# File 'lib/wamp/router/connection.rb', line 22

def session
  @session
end

#socketObject (readonly)

Returns the value of attribute socket.



22
23
24
# File 'lib/wamp/router/connection.rb', line 22

def socket
  @socket
end

Instance Method Details

#begin_close(reason, code) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/wamp/router/connection.rb', line 46

def begin_close(reason, code)
  return if @ready_state == CLOSED

  @ready_state = CLOSING
  @close_params = [reason, code]

  @cleanup&.call(self)
  finalize_close
end

#connectionObject



42
43
44
# File 'lib/wamp/router/connection.rb', line 42

def connection
  self
end

#finalize_closeObject



56
57
58
59
60
61
62
# File 'lib/wamp/router/connection.rb', line 56

def finalize_close
  return if @ready_state == CLOSED

  @ready_state = CLOSED
  @driver.close
  socket.close
end

#listen(&block) ⇒ Object

rubocop:disable Metrics/MethodLength



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/wamp/router/connection.rb', line 64

def listen(&block) # rubocop:disable Metrics/MethodLength
  return unless [CONNECTING, OPEN].include?(@ready_state)

  begin
    data = socket.read_nonblock(4096, exception: false)
    case data
    when :wait_readable
      # do nothing
    when nil
      block&.call
      @driver.close
    else
      receive_data(data)
    end
  rescue StandardError => e
    puts e.message
    puts e.backtrace
    begin
      block&.call
      @driver.close
    rescue StandardError
      # Errno::ECONNRESET
      puts "Failed to handle: Errno::ECONNRESET"
    end
  end
end

#on_connectObject



36
37
38
39
40
# File 'lib/wamp/router/connection.rb', line 36

def on_connect
  @driver.start if WebSocket::Driver.websocket?(@driver.env)
  choose_serializer_from @driver.env["HTTP_SEC_WEBSOCKET_PROTOCOL"]
  @acceptor = Wampproto::Acceptor.new(serializer, Authenticator)
end

#receive_data(data) ⇒ Object

triggers on_message



92
93
94
95
96
# File 'lib/wamp/router/connection.rb', line 92

def receive_data(data)
  return unless [OPEN, CONNECTING].include?(@ready_state)

  @driver.parse(data)
end

#transmit(message) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/wamp/router/connection.rb', line 103

def transmit(message)
  # return false if @ready_state > OPEN

  case message
  when Wampproto::Message::Base then transmit(serializer.serialize(message))
  when Numeric then @driver.text(message.to_s)
  when String  then @driver.text(message)
  when Array   then @driver.binary(message)
  else false
  end
end

#write(data) ⇒ Object

called when @driver.text is invoked



99
100
101
# File 'lib/wamp/router/connection.rb', line 99

def write(data)
  socket.write(data)
end