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
SUPPORTED_PROTOCOLS =
["wamp.2.msgpack", "wamp.2.cbor", "wamp.2.json"].freeze

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

rubocop:disable Lint/MissingSuper



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

def initialize(socket, &cleanup) # rubocop:disable Lint/MissingSuper
  # super() # on_connect() does what super() does
  @cleanup = cleanup
  @socket = socket
  @driver = WebSocket::Driver.server(self, protocols: SUPPORTED_PROTOCOLS)
  @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.



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

def acceptor
  @acceptor
end

#sessionObject (readonly)

Returns the value of attribute session.



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

def session
  @session
end

#socketObject (readonly)

Returns the value of attribute socket.



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

def socket
  @socket
end

Instance Method Details

#begin_close(reason, code) ⇒ Object



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

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

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

  @cleanup&.call(self)
  finalize_close
end

#connectionObject



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

def connection
  self
end

#finalize_closeObject



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

def finalize_close
  return if @ready_state == CLOSED

  @ready_state = CLOSED
  @driver.close(*@close_params)
  socket.close
end

#listen(&block) ⇒ Object

rubocop:disable Metrics/MethodLength



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
90
# File 'lib/wamp/router/connection.rb', line 65

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



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

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



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

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

  @driver.parse(data)
end

#transmit(message) ⇒ Object



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

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



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

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