// File: /workspace/github_cpp_makefile_star2_O1/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 %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x40, %rsp
movq %rdi, %rbx
cmpl $0x0, 0x30(%rdi)
je 0x3a07b
movq (%rbx), %r14
movq 0x8(%rbx), %r12
subq %r14, %r12
je 0x3a07b
movq 0x38(%rbx), %r13
leaq (%r12,%r13), %rdi
callq 0xa040
movq %rax, %r15
movq %rax, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0xa4d0
movq %r15, %rdi
addq %r12, %rdi
movq 0x20(%rbx), %r12
movq %r12, %rsi
movq %r13, %rdx
callq 0xa4d0
movq %r14, 0x8(%rbx)
testq %r12, %r12
je 0x3a06f
movl $0x1, %esi
movq %r12, %rdi
callq 0xa5d0
movq %r15, 0x20(%rbx)
movq $0x8, 0x38(%rbx)
movl 0x30(%rbx), %eax
cmpl $0x8, %eax
jg 0x3a0c9
testl %eax, %eax
je 0x3a1ce
cmpl $0x8, %eax
jne 0x3a199
cmpl $0x1, 0x398(%rbx)
je 0x3a0bc
movq 0x20(%rbx), %rsi
movq 0x38(%rbx), %rdx
movq %rbx, %rdi
movl $0x8, %ecx
callq 0x3a24a
movl $0x1, 0x398(%rbx)
movq %rbx, %rdi
callq 0x3a41a
jmp 0x3a1fc
cmpl $0x9, %eax
je 0x3a1e7
cmpl $0xa, %eax
jne 0x3a199
movq 0x358(%rbx), %rax
cmpq %rax, 0x378(%rbx)
je 0x3a1fc
leaq 0x348(%rbx), %rdi
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movq 0x18(%rax), %rcx
movq %rcx, 0x28(%rsp)
movq 0x10(%rax), %rcx
testq %rcx, %rcx
je 0x3a12d
leaq 0x10(%rax), %rdx
movups (%rax), %xmm1
movaps %xmm1, 0x10(%rsp)
movq %rcx, 0x20(%rsp)
movups %xmm0, (%rdx)
movq 0x20(%rax), %rax
movq %rax, 0x30(%rsp)
callq 0x3ae7a
callq 0xa7b0
subq 0x30(%rsp), %rax
movabsq $0x431bde82d7b634db, %rcx # imm = 0x431BDE82D7B634DB
imulq %rcx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x12, %rdx
addq %rax, %rdx
movq %rdx, 0x8(%rsp)
cmpq $0x0, 0x20(%rsp)
je 0x3a217
leaq 0x10(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq *0x18(%rdi)
movq 0x20(%rsp), %rax
testq %rax, %rax
je 0x3a1fc
leaq 0x10(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x3a1fc
movq 0x20(%rbx), %rax
movq 0x38(%rbx), %rcx
movq %rax, 0x10(%rsp)
movq %rcx, 0x8(%rsp)
cmpq $0x0, 0x2f8(%rbx)
je 0x3a212
leaq 0x2e8(%rbx), %rdi
leaq 0x10(%rsp), %rsi
leaq 0x8(%rsp), %rdx
callq *0x300(%rbx)
jmp 0x3a1fc
movq 0x8(%rbx), %rsi
movq 0x20(%rbx), %rdx
movq 0x38(%rbx), %rcx
addq %rdx, %rcx
movq %rbx, %rdi
callq 0x3b2f4
jmp 0x3a1fc
movq 0x20(%rbx), %rsi
movq 0x38(%rbx), %rdx
movq %rbx, %rdi
movl $0xa, %ecx
callq 0x3a24a
movq %rbx, %rdi
callq 0x39e06
addq $0x40, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
callq 0xa240
callq 0xa240
jmp 0x3a242
movq %rax, %rbx
movq 0x20(%rsp), %rax
testq %rax, %rax
je 0x3a23a
leaq 0x10(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq %rbx, %rdi
callq 0xaa30
movq %rax, %rdi
callq 0xd3bb