Class: Ruflet::WebSocketConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/ruflet/server/web_socket_connection.rb

Constant Summary collapse

TASK_IO_POLL_INTERVAL =
0.01
MAX_FRAME_PAYLOAD_BYTES =

Ruflet control messages are small; anything much larger is invalid or hostile.

16 * 1024 * 1024

Instance Method Summary collapse

Constructor Details

#initialize(socket) ⇒ WebSocketConnection

Returns a new instance of WebSocketConnection.



9
10
11
12
# File 'lib/ruflet/server/web_socket_connection.rb', line 9

def initialize(socket)
  @socket = socket
  @write_mutex = Mutex.new
end

Instance Method Details

#closeObject



77
78
79
80
81
82
83
84
85
# File 'lib/ruflet/server/web_socket_connection.rb', line 77

def close
  return if closed?

  begin
    @socket.close
  rescue IOError
    nil
  end
end

#closed?Boolean

Returns:

  • (Boolean)


18
19
20
21
22
# File 'lib/ruflet/server/web_socket_connection.rb', line 18

def closed?
  @socket.closed?
rescue IOError
  true
end

#read_messageObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ruflet/server/web_socket_connection.rb', line 34

def read_message
  frame = read_frame
  return nil if frame.nil?

  opcode = frame[:opcode]
  payload = frame[:payload]

  case opcode
  when 0x8
    close
    nil
  when 0x9
    send_frame(0xA, payload)
    read_message
  when 0xA
    read_message
  when 0x1, 0x2
    return traced_incoming(payload) if frame[:fin]

    message = payload.dup
    loop do
      continuation = read_frame
      return nil if continuation.nil?

      case continuation[:opcode]
      when 0x9
        send_frame(0xA, continuation[:payload])
        next
      when 0xA
        next
      when 0x0
        message << continuation[:payload]
        return traced_incoming(message) if continuation[:fin]
        return nil if message.bytesize > MAX_FRAME_PAYLOAD_BYTES
      else
        return nil
      end
    end
  else
    read_message
  end
end

#send_binary(payload) ⇒ Object



24
25
26
27
28
# File 'lib/ruflet/server/web_socket_connection.rb', line 24

def send_binary(payload)
  bytes = payload.to_s.b
  Ruflet::WireCodec.trace("ruby->client conn=#{session_key}", bytes)
  send_frame(0x2, bytes)
end

#send_text(payload) ⇒ Object



30
31
32
# File 'lib/ruflet/server/web_socket_connection.rb', line 30

def send_text(payload)
  send_frame(0x1, payload.to_s.b)
end

#session_keyObject



14
15
16
# File 'lib/ruflet/server/web_socket_connection.rb', line 14

def session_key
  @socket.object_id
end