- # Gets the message from the client, unpads it and reconstitutes the message
- private fun unpad_message do
- var fin = false
- while not fin do
- var fst_char = client.read_char
- var snd_char = client.read_char
- # First byte in msg is formatted this way :
- # |(fin - 1bit)|(RSV1 - 1bit)|(RSV2 - 1bit)|(RSV3 - 1bit)|(opcode - 4bits)
- # fin = Flag indicating if current frame is the last one
- # RSV1/2/3 = Extension flags, unsupported
- # Opcode values :
- # %x0 denotes a continuation frame
- # %x1 denotes a text frame
- # %x2 denotes a binary frame
- # %x3-7 are reserved for further non-control frames
- # %x8 denotes a connection close
- # %x9 denotes a ping
- # %xA denotes a pong
- # %xB-F are reserved for further control frames
- var fin_flag = fst_char.bin_and(128)
- if fin_flag != 0 then fin = true
- var opcode = fst_char.bin_and(15)
- if opcode == 9 then
- _buffer.add(138.ascii)
- _buffer.add('\0')
- client.write(_buffer.to_s)
- _buffer_pos += 2
+ # Get a frame's information
+ private fun read_frame_info do
+ var fst_byte = origin.read_byte
+ var snd_byte = origin.read_byte
+ if fst_byte < 0 or snd_byte < 0 then
+ last_error = new IOError("Error: bad frame")
+ close
+ return
+ end
+ # First byte in msg is formatted this way :
+ # |(fin - 1bit)|(RSV1 - 1bit)|(RSV2 - 1bit)|(RSV3 - 1bit)|(opcode - 4bits)
+ # fin = Flag indicating if current frame is the last one for the current message
+ # RSV1/2/3 = Extension flags, unsupported
+ # Opcode values :
+ # %x0 denotes a continuation frame
+ # %x1 denotes a text frame
+ # %x2 denotes a binary frame
+ # %x3-7 are reserved for further non-control frames
+ # %x8 denotes a connection close
+ # %x9 denotes a ping
+ # %xA denotes a pong
+ # %xB-F are reserved for further control frames
+ var opcode = fst_byte & 0b0000_1111
+ if opcode == 9 then
+ origin.write_bytes(pong_msg)
+ return
+ end
+ if opcode == 8 then
+ close
+ return
+ end
+ frame_type = opcode
+ # Second byte is formatted this way :
+ # |(mask - 1bit)|(payload length - 7 bits)
+ # As specified, if the payload length is 126 or 127
+ # The next 16 or 64 bits contain an extended payload length
+ var mask_flag = snd_byte & 0b1000_0000
+ var len = snd_byte & 0b0111_1111
+ var payload_ext_len = 0
+ if len == 126 then
+ var tmp = origin.read_bytes(2)
+ if tmp.length != 2 then
+ last_error = new IOError("Error: received interrupted frame")
+ origin.close