// File: king1600[P]cordpp/src/ws.cc
// Function: cordpp::WebsockClient::handle_frame()
void WebsockClient::handle_frame() {
  // prepend fragmented data if any
  if (frame.opcode != WebsockOp::cont && builder.size() > 0) {
    char *data = new char[builder.size() + frame.payload_len];
    std::memcpy(data, &builder[0], builder.size());
    std::memcpy(data + builder.size(), frame.payload, frame.payload_len);
    builder.clear();
    delete frame.payload;
    frame.payload = data;
    frame.payload_len = sizeof(frame.payload);
  }

  // handle what actiont to perform using the frame
  switch (frame.opcode) {

    // append to builder if payload is fragmented
    case WebsockOp::cont:
      builder.insert(builder.end(), frame.payload,
        frame.payload + frame.payload_len);
      break;

    // echo back ping from server if it sends
    case WebsockOp::ping:
      send(frame.payload, frame.payload_len, WebsockOp::pong);
      break;
    
    // handle ping response from server
    case WebsockOp::pong:
      if (!pings.empty()) {
        const WebsockPing task = std::move(pings.front());
        pings.pop_front();
        task.action(std::chrono::duration_cast<std::chrono::milliseconds>(
          websock_clock::now() - task.created).count());
      }
      break;

    // handle closing opcode from server
    case WebsockOp::close:
      if (state != WebsockState::closed) {
        send(frame.payload, frame.payload_len, WebsockOp::close);
        state = WebsockState::closed;
      }
      perform_close();
      break;

    // handle opcodes for binary and text
    default:
      message_cb(frame.payload, frame.payload_len);
      break;
  }

  // read the next frame
  parse_headers();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x80(%rbp)
cmpl $0x0, 0x30(%rax)
je 0x88e84
movq -0x80(%rbp), %rdi
callq 0x191c0
cmpq $0x0, %rax
jbe 0x88e84
movq -0x80(%rbp), %rdi
callq 0x191c0
movq %rax, %rdi
movq -0x80(%rbp), %rax
addq 0x38(%rax), %rdi
callq 0xc640
movq -0x80(%rbp), %rdi
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xa0(%rbp)
xorl %eax, %eax
movl %eax, %esi
callq 0x18d50
movq -0x80(%rbp), %rdi
movq %rax, -0x98(%rbp)
callq 0x191c0
movq -0xa0(%rbp), %rdi
movq -0x98(%rbp), %rsi
movq %rax, %rdx
callq 0xc0d0
movq -0x80(%rbp), %rdi
movq -0x10(%rbp), %rax
movq %rax, -0x90(%rbp)
callq 0x191c0
movq -0x90(%rbp), %rdi
movq %rax, %rcx
movq -0x80(%rbp), %rax
addq %rcx, %rdi
movq 0x20(%rax), %rsi
movq 0x38(%rax), %rdx
callq 0xc0d0
movq -0x80(%rbp), %rdi
callq 0x8c1a0
movq -0x80(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x88(%rbp)
cmpq $0x0, %rax
je 0x88e70
movq -0x88(%rbp), %rdi
movl $0x1, %esi
callq 0xc1b0
movq -0x80(%rbp), %rax
movq -0x10(%rbp), %rcx
movq %rcx, 0x20(%rax)
movq $0x8, 0x38(%rax)
movq -0x80(%rbp), %rax
movl 0x30(%rax), %eax
movq %rax, -0xa8(%rbp)
subq $0xa, %rax
ja 0x89026
movq -0xa8(%rbp), %rax
leaq 0xbd5e(%rip), %rcx      # 0x94c08
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x80(%rbp), %rdi
callq 0x48130
movq %rax, -0x20(%rbp)
leaq -0x18(%rbp), %rdi
leaq -0x20(%rbp), %rsi
callq 0x8c260
movq -0x80(%rbp), %rdi
movq 0x20(%rdi), %rdx
movq 0x20(%rdi), %rcx
addq 0x38(%rdi), %rcx
movq -0x18(%rbp), %rsi
callq 0x8c1c0
movq %rax, -0x28(%rbp)
jmp 0x89041
movq -0x80(%rbp), %rdi
movq 0x20(%rdi), %rsi
movq 0x38(%rdi), %rdx
movl $0xa, %ecx
callq 0x89060
jmp 0x89041
movq -0x80(%rbp), %rdi
addq $0x348, %rdi            # imm = 0x348
callq 0x8c2a0
testb $0x1, %al
jne 0x88fe8
movq -0x80(%rbp), %rdi
addq $0x348, %rdi            # imm = 0x348
movq %rdi, -0xb8(%rbp)
callq 0x8c2d0
movq %rax, %rsi
leaq -0x50(%rbp), %rdi
movq %rdi, -0xc0(%rbp)
callq 0x8c300
movq -0xb8(%rbp), %rdi
callq 0x8c340
callq 0xcc10
movq %rax, -0x68(%rbp)
leaq -0x30(%rbp), %rsi
leaq -0x68(%rbp), %rdi
callq 0x8c420
movq %rax, -0xb0(%rbp)
jmp 0x88f77
movq -0xb0(%rbp), %rax
movq %rax, -0x60(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x8c3f0
movq %rax, -0xc8(%rbp)
jmp 0x88f94
movq -0xc8(%rbp), %rax
movq %rax, -0x58(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x8c470
movq %rax, -0xd0(%rbp)
jmp 0x88fb1
movq -0xd0(%rbp), %rsi
movq -0xc0(%rbp), %rdi
callq 0x8c3b0
jmp 0x88fc6
leaq -0x50(%rbp), %rdi
callq 0x1cd70
jmp 0x88fe8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x1cd70
jmp 0x89053
jmp 0x89041
movq -0x80(%rbp), %rax
cmpl $0x1, 0x398(%rax)
je 0x8901b
movq -0x80(%rbp), %rdi
movq 0x20(%rdi), %rsi
movq 0x38(%rdi), %rdx
movl $0x8, %ecx
callq 0x89060
movq -0x80(%rbp), %rax
movl $0x1, 0x398(%rax)
movq -0x80(%rbp), %rdi
callq 0x893f0
jmp 0x89041
movq -0x80(%rbp), %rax
movq %rax, %rdi
addq $0x2e8, %rdi            # imm = 0x2E8
movq 0x20(%rax), %rsi
movq 0x38(%rax), %rdx
callq 0x8c490
movq -0x80(%rbp), %rdi
callq 0x889b0
addq $0xd0, %rsp
popq %rbp
retq
movq -0x70(%rbp), %rdi
callq 0xc8c0
nopl (%rax)