// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
movl 0x30(%rdi), %eax
testl %eax, %eax
je 0x2faa2
movq (%rbx), %r14
movq 0x8(%rbx), %r15
subq %r14, %r15
je 0x2fa8b
movq 0x38(%rbx), %r12
leaq (%r12,%r15), %rdi
callq 0xa480
movq %rax, %r13
movq %rax, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0xa090
addq %r13, %r15
movq 0x20(%rbx), %rbp
movq %r15, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0xa090
movq %r14, 0x8(%rbx)
pushq $0x1
popq %rsi
movq %rbp, %rdi
callq 0xa110
movq %r13, 0x20(%rbx)
movq $0x8, 0x38(%rbx)
movl 0x30(%rbx), %eax
cmpl $0xa, %eax
je 0x2fb03
cmpl $0x8, %eax
je 0x2fabe
cmpl $0x9, %eax
je 0x2faee
testl %eax, %eax
jne 0x2fb55
movq 0x8(%rbx), %rsi
movq 0x20(%rbx), %rdx
movq 0x38(%rbx), %rcx
addq %rdx, %rcx
movq %rbx, %rdi
callq 0x3052e
jmp 0x2fb69
cmpl $0x1, 0x398(%rbx)
je 0x2fae4
movq 0x20(%rbx), %rsi
movq 0x38(%rbx), %rdx
pushq $0x8
popq %rcx
movq %rbx, %rdi
callq 0x2fb94
movl $0x1, 0x398(%rbx)
movq %rbx, %rdi
callq 0x2fd0c
jmp 0x2fb69
movq 0x20(%rbx), %rsi
movq 0x38(%rbx), %rdx
pushq $0xa
popq %rcx
movq %rbx, %rdi
callq 0x2fb94
jmp 0x2fb69
movq 0x358(%rbx), %rsi
cmpq %rsi, 0x378(%rbx)
je 0x2fb69
leaq 0x348(%rbx), %r14
movq %rsp, %r15
movq %r15, %rdi
callq 0x3054e
movq %r14, %rdi
callq 0x3056e
callq 0xa980
subq 0x20(%r15), %rax
movl $0xf4240, %ecx          # imm = 0xF4240
cqto
idivq %rcx
movq %r15, %rdi
movq %rax, %rsi
callq 0x30598
movq %rsp, %rdi
callq 0xe8c8
jmp 0x2fb69
leaq 0x2e8(%rbx), %rdi
movq 0x20(%rbx), %rsi
movq 0x38(%rbx), %rdx
callq 0x305b2
movq %rbx, %rdi
callq 0x2f8b0
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq %rsp, %rdi
callq 0xe8c8
movq %rbx, %rdi
callq 0xa6c0
nop