// File: /workspace/github_cpp_makefile_star2_O3/lega911[P]ijson/src/connect.cpp
// Function: Connect::on_recv(char*, int)
void Connect::on_recv(char *buf, int size) {
    if(ping) ping_timeout = get_time_sec() + ping;
    if(!(status == Status::net || (status == Status::worker_wait_result && noid) || (status == Status::worker_mode_async && worker_mode))) {
        if(server->log & 4) std::cout << ltime() << "connect " << (void*)this << ", warning: data is come, but connection is not ready\n";
        buffer.add(buf, size);
        return;
    }
    Slice data;
    if(buffer.size()) {
        buffer.add(buf, size);
        data = Slice(buffer);
    } else {
        data = Slice(buf, size);
    }

    if(http_step == HTTP_READ_BODY) {
        int for_read = content_length - body.size();
        if(for_read > data.size()) {
            body.add(data);
            buffer.clear();
            return;
        } else {
            Slice s = data.pop(for_read);
            body.add(s);
            //buffer.add(data);
        }
        if(body.size() == content_length) {
            try {
                this->header_completed();
            } catch (const error::InvalidData &e) {
                if(server->log & 4) std::cout << ltime() << "Error: Invalid data/json, socket " << fd << " " << (void*)this << std::endl;
                this->send.status("400 Invalid data")->done(-32700);
            }
            http_step = HTTP_START;
        }
        if(data.empty()) return;
    }

    Slice line;
    while(true) {
        line = data.pop_line();
        if(!line.valid()) break;  // wait next package
        line.rstrip();

        if(line.empty()) {
            if(http_step != HTTP_HEADER) throw error::InvalidData("Wrong HTTP request");
            http_step = HTTP_REQUEST_COMPLETED;
            if(content_length) {
                int for_read = content_length;
                if(for_read > data.size()) for_read = data.size();
                Slice body_data = data.pop(for_read);
                body.set(body_data);

                if(body.size() < content_length) {
                    http_step = HTTP_READ_BODY;
                };
            };
            //if(data.size()) buffer.set(data);
            buffer.clear();
            if(http_step == HTTP_REQUEST_COMPLETED) {
                try {
                    this->header_completed();
                } catch (const error::InvalidData &e) {
                    this->send.status("400 Invalid data")->done(-32700);
                }

                if(data.size()) {
                    if(status == Status::net) {
                        http_step = HTTP_START;
                        continue;
                    } else THROW("previous request is not finished");
                }
            }
            break;
        }
        if(http_step == HTTP_START) {
            body.clear();
            id.clear();
            header_option.reset();
            info.reset();
            json.reset();
            if(!worker_mode) name.clear();
            content_length = 0;
            priority = 0;
            required_worker.clear();
            no_response = false;
            type = RequestType::none;
            if(status != Status::worker_wait_result) {
                if(worker_mode) {
                    if(status != Status::worker_mode_async) THROW("Wrong status for worker");
                } else {
                    fail_on_disconnect = false;
                    if(client) client->unlink();
                    client = NULL;
                    noid = false;
                }
            } else {
                if(!noid) THROW("noid is false");
            }
            if(this->read_method(line) != 0) {
                if(server->log & 2) std::cout << ltime() << "Wrong http header\n";
                this->close();
                return;
            }
            http_step = HTTP_HEADER;
        } else if(http_step == HTTP_HEADER) {
            try {
                this->read_header(line);
            } catch (const error::InvalidData &e) {
                this->send.status("400 Invalid data")->done(-32700);
            }
        }
    }

    if(http_step == HTTP_REQUEST_COMPLETED) {
        http_step = HTTP_START;
    } else if(http_step == HTTP_READ_BODY) {
    } else if(http_step == HTTP_START || http_step == HTTP_HEADER) {
        buffer.add(data);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %edx, %r15d
movq %rsi, %r12
movq %rdi, %rbx
cmpl $0x0, 0xf8(%rdi)
je 0x8d04
callq 0x163d5
addl 0xf8(%rbx), %eax
movl %eax, 0xfc(%rbx)
movl 0xcc(%rbx), %eax
testl %eax, %eax
je 0x8d68
cmpl $0x6, %eax
je 0x8d5f
cmpl $0x2, %eax
jne 0x8d21
cmpb $0x0, 0xf1(%rbx)
jne 0x8d68
movq 0x48(%rbx), %rax
testb $0x4, 0x20(%rax)
je 0x917e
callq 0x163f5
testq %rax, %rax
je 0x9126
movq %rax, %r14
movq %rax, %rdi
callq 0x8130
movq 0x15281(%rip), %rdi     # 0x1dfd0
movq %r14, %rsi
movq %rax, %rdx
callq 0x8350
jmp 0x9144
cmpb $0x0, 0xf3(%rbx)
je 0x8d21
leaq 0x88(%rbx), %rdi
cmpl $0x0, 0x90(%rbx)
movq %rdi, -0x60(%rbp)
je 0x8d95
movq %r12, %rsi
movl %r15d, %edx
callq 0xc102
movq 0x88(%rbx), %r12
movl 0x90(%rbx), %r15d
movq %r12, -0x38(%rbp)
movl %r15d, -0x30(%rbp)
cmpl $0x2, 0x78(%rbx)
jne 0x8e15
movslq 0x7c(%rbx), %r14
leaq 0xd0(%rbx), %r13
movslq 0xd8(%rbx), %rax
subq %rax, %r14
cmpl %r15d, %r14d
jle 0x8dd0
movq %r13, %rdi
movq %r12, %rsi
movl %r15d, %edx
callq 0xc102
jmp 0x91a8
leaq -0x38(%rbp), %r15
movq %r15, %rdi
movl %r14d, %esi
callq 0xc6fc
addq %r14, (%r15)
subl %r14d, 0x8(%r15)
movq %r13, %rdi
movq %rax, %rsi
callq 0xc102
movl 0xd8(%rbx), %eax
cmpl 0x7c(%rbx), %eax
jne 0x8e0b
movq %rbx, %rdi
callq 0x93c6
movl $0x0, 0x78(%rbx)
cmpl $0x0, -0x30(%rbp)
je 0x91b2
leaq -0x38(%rbp), %rdi
callq 0xc1da
movq %rax, -0x50(%rbp)
movl %edx, -0x48(%rbp)
testq %rax, %rax
je 0x90f4
leaq 0x18(%rbx), %rcx
movq %rcx, -0x40(%rbp)
leaq 0x14c(%rbx), %rcx
movq %rcx, -0x58(%rbp)
leaq 0xd0(%rbx), %rcx
movq %rcx, -0x68(%rbp)
movabsq $0x100002400, %r13      # imm = 0x100002400
xorl %r12d, %r12d
leaq -0x38(%rbp), %r15
testl %edx, %edx
jle 0x8e8d
movl %edx, %edx
movzbl -0x1(%rax,%rdx), %ecx
cmpq $0x20, %rcx
ja 0x8e8d
btq %rcx, %r13
jae 0x8e8d
decq %rdx
leaq 0x1(%rdx), %rcx
cmpq $0x1, %rcx
ja 0x8e63
movl $0x0, -0x48(%rbp)
jmp 0x8f58
movl %edx, -0x48(%rbp)
testl %edx, %edx
je 0x8f58
movl 0x78(%rbx), %eax
cmpl $0x1, %eax
je 0x9019
testl %eax, %eax
jne 0x8ffc
movl %r12d, 0xd8(%rbx)
movl %r12d, 0xe8(%rbx)
movq %r12, 0xa8(%rbx)
movl %r12d, 0xb0(%rbx)
movq %r12, 0x168(%rbx)
movl %r12d, 0x170(%rbx)
movq %r12, 0x128(%rbx)
movl %r12d, 0x130(%rbx)
movq %r12, 0x138(%rbx)
movl %r12d, 0x140(%rbx)
movl %r12d, 0x160(%rbx)
movq -0x58(%rbp), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movb 0xf3(%rbx), %al
testb %al, %al
jne 0x8f17
movl $0x0, 0xc0(%rbx)
movl %r12d, 0x7c(%rbx)
movl %r12d, 0xf4(%rbx)
movl %r12d, 0x118(%rbx)
movb $0x0, 0xf2(%rbx)
movl %r12d, 0xc8(%rbx)
movl 0xcc(%rbx), %ecx
cmpl $0x2, %ecx
jne 0x9027
cmpb $0x0, 0xf1(%rbx)
jne 0x905f
jmp 0x9273
cmpl $0x1, 0x78(%rbx)
jne 0x91fb
movl $0x3, 0x78(%rbx)
movl 0x7c(%rbx), %eax
testl %eax, %eax
je 0x8fcc
movl -0x30(%rbp), %r14d
cmpl %r14d, %eax
cmovll %eax, %r14d
movq %r15, %rdi
movl %r14d, %esi
callq 0xc6fc
movslq %r14d, %rcx
addq %rcx, -0x38(%rbp)
subl %ecx, -0x30(%rbp)
movl $0x0, 0xd8(%rbx)
movq -0x68(%rbp), %rdi
movq %rax, %rsi
callq 0xc102
movl 0xd8(%rbx), %eax
cmpl 0x7c(%rbx), %eax
jl 0x91a1
movl 0x78(%rbx), %eax
movl $0x0, 0x90(%rbx)
cmpl $0x3, %eax
je 0x8fd6
jmp 0x90f7
movl $0x0, 0x90(%rbx)
movq %rbx, %rdi
callq 0x93c6
cmpl $0x0, -0x30(%rbp)
je 0x90f4
cmpl $0x0, 0xcc(%rbx)
jne 0x922e
movl $0x0, 0x78(%rbx)
movq %r15, %rdi
callq 0xc1da
movq %rax, -0x50(%rbp)
movl %edx, -0x48(%rbp)
testq %rax, %rax
jne 0x8e5d
jmp 0x90f4
movq %rbx, %rdi
leaq -0x50(%rbp), %rsi
callq 0xa0b2
jmp 0x8ffc
testb %al, %al
je 0x9035
cmpl $0x6, %ecx
je 0x905f
jmp 0x92a4
movb $0x0, 0xf0(%rbx)
movq 0x120(%rbx), %rdi
testq %rdi, %rdi
je 0x904d
callq 0x898c
movq $0x0, 0x120(%rbx)
movb $0x0, 0xf1(%rbx)
movq %rbx, %rdi
leaq -0x50(%rbp), %rsi
callq 0xa00a
testl %eax, %eax
jne 0x91c1
movl $0x1, 0x78(%rbx)
jmp 0x8ffc
movq %rax, %r14
cmpl $0x1, %edx
jne 0x93bd
movq %r14, %rdi
callq 0x8120
movq -0x40(%rbp), %rdi
leaq 0xe070(%rip), %rsi      # 0x1710b
callq 0x9ec6
movq -0x40(%rbp), %rdi
movl $0xffff8044, %esi       # imm = 0xFFFF8044
callq 0x9f3a
callq 0x8450
jmp 0x8ffc
movq %rax, %r14
cmpl $0x1, %edx
jne 0x93bd
movq %r14, %rdi
callq 0x8120
movq -0x40(%rbp), %rdi
leaq 0xe034(%rip), %rsi      # 0x1710b
callq 0x9ec6
movq -0x40(%rbp), %rdi
movl $0xffff8044, %esi       # imm = 0xFFFF8044
callq 0x9f3a
callq 0x8450
jmp 0x8fde
movl 0x78(%rbx), %eax
cmpl $0x2, %eax
jae 0x9111
movq -0x38(%rbp), %rsi
movl -0x30(%rbp), %edx
movq -0x60(%rbp), %rdi
callq 0xc102
jmp 0x91b2
cmpl $0x3, %eax
jne 0x91b2
movl $0x0, 0x78(%rbx)
jmp 0x91b2
movq 0x14ea3(%rip), %rax     # 0x1dfd0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
leaq (%rax,%rcx), %rdi
movl 0x20(%rax,%rcx), %esi
orl $0x1, %esi
callq 0x8470
movq 0x14e85(%rip), %r14     # 0x1dfd0
leaq 0xdf58(%rip), %rsi      # 0x170aa
movl $0x8, %edx
movq %r14, %rdi
callq 0x8350
movq %r14, %rdi
movq %rbx, %rsi
callq 0x8250
leaq 0xdf42(%rip), %rsi      # 0x170b3
movl $0x35, %edx
movq %rax, %rdi
callq 0x8350
addq $0x88, %rbx
movq %rbx, %rdi
movq %r12, %rsi
movl %r15d, %edx
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xc102
movl $0x2, 0x78(%rbx)
movl $0x0, 0x90(%rbx)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x48(%rbx), %rax
testb $0x2, 0x20(%rax)
je 0x91f3
callq 0x163f5
movq 0x14df9(%rip), %rdi     # 0x1dfd0
movq %rax, %rsi
callq 0x8300
leaq 0xdfb3(%rip), %rsi      # 0x17199
movl $0x12, %edx
movq %rax, %rdi
callq 0x8350
movl $0xffffffff, (%rbx)     # imm = 0xFFFFFFFF
jmp 0x91b2
movl $0x30, %edi
callq 0x8170
movq %rax, %rbx
leaq 0xdf0d(%rip), %rsi      # 0x1711c
movq %rax, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0xc234
leaq 0x14a4b(%rip), %rsi     # 0x1dc70
leaq 0x40e4(%rip), %rdx      # 0xd310
jmp 0x926b
movl $0x30, %edi
callq 0x8170
movq %rax, %rbx
leaq 0xdeed(%rip), %rsi      # 0x1712f
leaq 0xde08(%rip), %rdx      # 0x17051
leaq 0xdf00(%rip), %r8       # 0x17150
movq %rax, %rdi
movl $0x8c, %ecx
callq 0xcd04
leaq 0x14ab4(%rip), %rsi     # 0x1dd18
leaq 0x40a5(%rip), %rdx      # 0xd310
movq %rbx, %rdi
callq 0x8480
movl $0x30, %edi
callq 0x8170
movq %rax, %rbx
leaq 0xdf04(%rip), %rsi      # 0x1718b
leaq 0xddc3(%rip), %rdx      # 0x17051
leaq 0xdebb(%rip), %r8       # 0x17150
movq %rax, %rdi
movl $0xa7, %ecx
callq 0xcd04
jmp 0x925d
movl $0x30, %edi
callq 0x8170
movq %rax, %rbx
leaq 0xdebb(%rip), %rsi      # 0x17173
leaq 0xdd92(%rip), %rdx      # 0x17051
leaq 0xde8a(%rip), %r8       # 0x17150
movq %rax, %rdi
movl $0x9f, %ecx
callq 0xcd04
jmp 0x925d
movq %rax, %r14
callq 0x8450
jmp 0x93bd
movq %rax, %r14
callq 0x8450
jmp 0x93bd
jmp 0x93b2
movq %rax, %r14
cmpl $0x1, %edx
jne 0x93bd
movq %r14, %rdi
callq 0x8120
movq 0x48(%rbx), %rax
testb $0x4, 0x20(%rax)
je 0x9372
callq 0x163f5
movq 0x14cb2(%rip), %rdi     # 0x1dfd0
movq %rax, %rsi
callq 0x8300
movq %rax, %r15
leaq 0xddb9(%rip), %rsi      # 0x170e9
movl $0x21, %edx
movq %rax, %rdi
callq 0x8350
movl 0xc(%rbx), %esi
movq %r15, %rdi
callq 0x8490
movq %rax, %r14
leaq 0xe357(%rip), %rsi      # 0x176a9
movl $0x1, %edx
movq %rax, %rdi
callq 0x8350
movq %r14, %rdi
movq %rbx, %rsi
callq 0x8250
movq %rax, %rdi
callq 0x8160
leaq 0x18(%rbx), %r14
leaq 0xdd8e(%rip), %rsi      # 0x1710b
movq %r14, %rdi
callq 0x9ec6
movq %r14, %rdi
movl $0xffff8044, %esi       # imm = 0xFFFF8044
callq 0x9f3a
callq 0x8450
jmp 0x8e04
movq %rax, %r14
callq 0x8450
jmp 0x93bd
movq %rax, %rdi
callq 0xc1cb
jmp 0x93b2
jmp 0x93b2
movq %rax, %r14
movq %rbx, %rdi
callq 0x8220
movq %r14, %rdi
callq 0x84a0
nop