// File: 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
subq $0x170, %rsp            # imm = 0x170
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xf8(%rbp)
cmpl $0x0, 0xf8(%rax)
je 0x19ec4
callq 0x36940
movl %eax, %ecx
movq -0xf8(%rbp), %rax
addl 0xf8(%rax), %ecx
movl %ecx, 0xfc(%rax)
movq -0xf8(%rbp), %rax
cmpl $0x0, 0xcc(%rax)
je 0x19f92
movq -0xf8(%rbp), %rax
cmpl $0x2, 0xcc(%rax)
jne 0x19efc
movq -0xf8(%rbp), %rax
testb $0x1, 0xf1(%rax)
jne 0x19f92
movq -0xf8(%rbp), %rax
cmpl $0x6, 0xcc(%rax)
jne 0x19f1c
movq -0xf8(%rbp), %rax
testb $0x1, 0xf3(%rax)
jne 0x19f92
movq -0xf8(%rbp), %rax
movq 0x48(%rax), %rax
movl 0x20(%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x19f73
callq 0x36960
movq %rax, %rsi
movq 0x2d08f(%rip), %rdi     # 0x46fd0
callq 0x192e0
movq %rax, %rdi
leaq 0x1e152(%rip), %rsi     # 0x380a2
callq 0x192e0
movq -0xf8(%rbp), %rsi
movq %rax, %rdi
callq 0x19250
movq %rax, %rdi
leaq 0x1e13d(%rip), %rsi     # 0x380ab
callq 0x192e0
movq -0xf8(%rbp), %rdi
addq $0x88, %rdi
movq -0x10(%rbp), %rsi
movl -0x14(%rbp), %edx
callq 0x1e8b0
jmp 0x1a964
leaq -0x28(%rbp), %rdi
callq 0x1e9d0
movq -0xf8(%rbp), %rdi
addq $0x88, %rdi
callq 0x1e6d0
cmpl $0x0, %eax
je 0x19ff4
movq -0xf8(%rbp), %rdi
addq $0x88, %rdi
movq -0x10(%rbp), %rsi
movl -0x14(%rbp), %edx
callq 0x1e8b0
movq -0xf8(%rbp), %rsi
addq $0x88, %rsi
leaq -0x38(%rbp), %rdi
callq 0x1e9f0
movq -0x38(%rbp), %rax
movq %rax, -0x28(%rbp)
movl -0x30(%rbp), %eax
movl %eax, -0x20(%rbp)
jmp 0x1a012
movq -0x10(%rbp), %rsi
movl -0x14(%rbp), %edx
leaq -0x48(%rbp), %rdi
callq 0x1ea20
movq -0x48(%rbp), %rax
movq %rax, -0x28(%rbp)
movl -0x40(%rbp), %eax
movl %eax, -0x20(%rbp)
movq -0xf8(%rbp), %rax
cmpl $0x2, 0x78(%rax)
jne 0x1a27a
movq -0xf8(%rbp), %rdi
movl 0x7c(%rdi), %eax
movl %eax, -0x100(%rbp)
addq $0xd0, %rdi
callq 0x1e6d0
movl %eax, %ecx
movl -0x100(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movl %eax, -0xfc(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x1e6d0
movl %eax, %ecx
movl -0xfc(%rbp), %eax
cmpl %ecx, %eax
jle 0x1a099
movq -0xf8(%rbp), %rdi
addq $0xd0, %rdi
leaq -0x28(%rbp), %rsi
callq 0x1ea60
movq -0xf8(%rbp), %rdi
addq $0x88, %rdi
callq 0x1eab0
jmp 0x1a964
movl -0x4c(%rbp), %esi
leaq -0x28(%rbp), %rdi
callq 0x1ead0
movq -0xf8(%rbp), %rdi
movl %edx, -0x68(%rbp)
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0x60(%rbp)
movl -0x68(%rbp), %eax
movl %eax, -0x58(%rbp)
addq $0xd0, %rdi
leaq -0x60(%rbp), %rsi
callq 0x1ea60
movq -0xf8(%rbp), %rdi
addq $0xd0, %rdi
callq 0x1e6d0
movq -0xf8(%rbp), %rcx
cmpl 0x7c(%rcx), %eax
jne 0x1a264
movq -0xf8(%rbp), %rdi
callq 0x1a980
jmp 0x1a102
jmp 0x1a24f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x1a96d
movq -0x78(%rbp), %rdi
callq 0x19120
movq %rax, %rcx
movq -0xf8(%rbp), %rax
movq %rcx, -0x88(%rbp)
movq 0x48(%rax), %rax
movl 0x20(%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x1a217
callq 0x36960
movq %rax, -0x108(%rbp)
jmp 0x1a15e
movq -0x108(%rbp), %rsi
movq 0x2ce64(%rip), %rdi     # 0x46fd0
callq 0x192e0
movq %rax, -0x110(%rbp)
jmp 0x1a17a
movq -0x110(%rbp), %rdi
leaq 0x1df59(%rip), %rsi     # 0x380e1
callq 0x192e0
movq %rax, -0x118(%rbp)
jmp 0x1a196
movq -0x118(%rbp), %rdi
movq -0xf8(%rbp), %rax
movl 0xc(%rax), %esi
callq 0x19490
movq %rax, -0x120(%rbp)
jmp 0x1a1b5
movq -0x120(%rbp), %rdi
leaq 0x1e4e1(%rip), %rsi     # 0x386a4
callq 0x192e0
movq %rax, -0x128(%rbp)
jmp 0x1a1d1
movq -0xf8(%rbp), %rsi
movq -0x128(%rbp), %rdi
callq 0x19250
movq %rax, -0x130(%rbp)
jmp 0x1a1ed
movq -0x130(%rbp), %rdi
movq 0x2cdb5(%rip), %rsi     # 0x46fb0
callq 0x19320
jmp 0x1a202
jmp 0x1a217
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
callq 0x19450
jmp 0x1a25f
movq -0xf8(%rbp), %rdi
addq $0x18, %rdi
leaq 0x1deda(%rip), %rsi     # 0x38103
callq 0x1b6d0
movq %rax, -0x138(%rbp)
jmp 0x1a237
movq -0x138(%rbp), %rdi
movl $0xffff8044, %esi       # imm = 0xFFFF8044
callq 0x1b770
jmp 0x1a24a
callq 0x19450
movq -0xf8(%rbp), %rax
movl $0x0, 0x78(%rax)
jmp 0x1a264
jmp 0x1a96d
leaq -0x28(%rbp), %rdi
callq 0x1eb40
testb $0x1, %al
jne 0x1a273
jmp 0x1a278
jmp 0x1a964
jmp 0x1a27a
leaq -0x98(%rbp), %rdi
callq 0x1e9d0
leaq -0x28(%rbp), %rdi
callq 0x1eb70
movl %edx, -0xb0(%rbp)
movq %rax, -0xb8(%rbp)
movq -0xb8(%rbp), %rax
movq %rax, -0xa8(%rbp)
movl -0xb0(%rbp), %eax
movl %eax, -0xa0(%rbp)
movq -0xa8(%rbp), %rax
movq %rax, -0x98(%rbp)
movl -0xa0(%rbp), %eax
movl %eax, -0x90(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x1ec10
testb $0x1, %al
jne 0x1a2e5
jmp 0x1a903
leaq -0x98(%rbp), %rdi
callq 0x1ec30
leaq -0x98(%rbp), %rdi
callq 0x1eb40
testb $0x1, %al
jne 0x1a306
jmp 0x1a5a9
movq -0xf8(%rbp), %rax
cmpl $0x1, 0x78(%rax)
je 0x1a377
movl $0x30, %edi
callq 0x19180
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x140(%rbp)
leaq 0x1dde3(%rip), %rsi     # 0x38114
xorl %ecx, %ecx
movl %ecx, %r8d
movq %r8, %rdx
callq 0x1ecb0
jmp 0x1a340
movq -0x140(%rbp), %rdi
leaq 0x2c90a(%rip), %rsi     # 0x46c58
leaq 0x49ab(%rip), %rdx      # 0x1ed00
callq 0x19480
movq -0x140(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
callq 0x19210
jmp 0x1a96d
movq -0xf8(%rbp), %rax
movl $0x3, 0x78(%rax)
cmpl $0x0, 0x7c(%rax)
je 0x1a44e
movq -0xf8(%rbp), %rax
movl 0x7c(%rax), %eax
movl %eax, -0xbc(%rbp)
movl -0xbc(%rbp), %eax
movl %eax, -0x144(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x1e6d0
movl %eax, %ecx
movl -0x144(%rbp), %eax
cmpl %ecx, %eax
jle 0x1a3cf
leaq -0x28(%rbp), %rdi
callq 0x1e6d0
movl %eax, -0xbc(%rbp)
movl -0xbc(%rbp), %esi
leaq -0x28(%rbp), %rdi
callq 0x1ead0
movq -0xf8(%rbp), %rdi
movl %edx, -0xd8(%rbp)
movq %rax, -0xe0(%rbp)
movq -0xe0(%rbp), %rax
movq %rax, -0xd0(%rbp)
movl -0xd8(%rbp), %eax
movl %eax, -0xc8(%rbp)
addq $0xd0, %rdi
leaq -0xd0(%rbp), %rsi
callq 0x1ed20
movq -0xf8(%rbp), %rdi
addq $0xd0, %rdi
callq 0x1e6d0
movq -0xf8(%rbp), %rcx
cmpl 0x7c(%rcx), %eax
jge 0x1a44c
movq -0xf8(%rbp), %rax
movl $0x2, 0x78(%rax)
jmp 0x1a44e
movq -0xf8(%rbp), %rdi
addq $0x88, %rdi
callq 0x1eab0
movq -0xf8(%rbp), %rax
cmpl $0x3, 0x78(%rax)
jne 0x1a5a4
movq -0xf8(%rbp), %rdi
callq 0x1a980
jmp 0x1a480
jmp 0x1a4e6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x1a96d
movq -0x78(%rbp), %rdi
callq 0x19120
movq -0xf8(%rbp), %rdi
movq %rax, -0xe8(%rbp)
addq $0x18, %rdi
leaq 0x1dc43(%rip), %rsi     # 0x38103
callq 0x1b6d0
movq %rax, -0x150(%rbp)
jmp 0x1a4ce
movq -0x150(%rbp), %rdi
movl $0xffff8044, %esi       # imm = 0xFFFF8044
callq 0x1b770
jmp 0x1a4e1
callq 0x19450
leaq -0x28(%rbp), %rdi
callq 0x1e6d0
cmpl $0x0, %eax
je 0x1a5a2
movq -0xf8(%rbp), %rax
cmpl $0x0, 0xcc(%rax)
jne 0x1a533
movq -0xf8(%rbp), %rax
movl $0x0, 0x78(%rax)
jmp 0x1a286
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
callq 0x19450
jmp 0x1a52e
jmp 0x1a96d
movl $0x30, %edi
callq 0x19180
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x158(%rbp)
leaq 0x1dbd6(%rip), %rsi     # 0x38127
leaq 0x1daf1(%rip), %rdx     # 0x38049
leaq 0x1dbe9(%rip), %r8      # 0x38148
movl $0x8c, %ecx
callq 0x22ba0
jmp 0x1a56b
movq -0x158(%rbp), %rdi
leaq 0x2c787(%rip), %rsi     # 0x46d00
leaq 0x8b90(%rip), %rdx      # 0x23110
callq 0x19480
movq -0x158(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
callq 0x19210
jmp 0x1a96d
jmp 0x1a5a4
jmp 0x1a903
movq -0xf8(%rbp), %rax
cmpl $0x0, 0x78(%rax)
jne 0x1a859
movq -0xf8(%rbp), %rdi
addq $0xd0, %rdi
callq 0x1eab0
movq -0xf8(%rbp), %rdi
addq $0xe0, %rdi
callq 0x1eab0
movq -0xf8(%rbp), %rdi
addq $0xa8, %rdi
callq 0x1ed70
movq -0xf8(%rbp), %rdi
addq $0x168, %rdi            # imm = 0x168
callq 0x1ed70
movq -0xf8(%rbp), %rdi
addq $0x128, %rdi            # imm = 0x128
callq 0x1eda0
movq -0xf8(%rbp), %rax
testb $0x1, 0xf3(%rax)
jne 0x1a63c
movq -0xf8(%rbp), %rdi
addq $0xb8, %rdi
callq 0x1eab0
movq -0xf8(%rbp), %rdi
movl $0x0, 0x7c(%rdi)
movl $0x0, 0xf4(%rdi)
addq $0x110, %rdi            # imm = 0x110
callq 0x1eab0
movq -0xf8(%rbp), %rax
movb $0x0, 0xf2(%rax)
movl $0x0, 0xc8(%rax)
cmpl $0x2, 0xcc(%rax)
je 0x1a763
movq -0xf8(%rbp), %rax
testb $0x1, 0xf3(%rax)
je 0x1a71a
movq -0xf8(%rbp), %rax
cmpl $0x6, 0xcc(%rax)
je 0x1a718
movl $0x30, %edi
callq 0x19180
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x160(%rbp)
leaq 0x1daa4(%rip), %rsi     # 0x3816b
leaq 0x1d97b(%rip), %rdx     # 0x38049
leaq 0x1da73(%rip), %r8      # 0x38148
movl $0x9f, %ecx
callq 0x22ba0
jmp 0x1a6e1
movq -0x160(%rbp), %rdi
leaq 0x2c611(%rip), %rsi     # 0x46d00
leaq 0x8a1a(%rip), %rdx      # 0x23110
callq 0x19480
movq -0x160(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
callq 0x19210
jmp 0x1a96d
jmp 0x1a75e
movq -0xf8(%rbp), %rax
movb $0x0, 0xf0(%rax)
cmpq $0x0, 0x120(%rax)
je 0x1a745
movq -0xf8(%rbp), %rax
movq 0x120(%rax), %rdi
callq 0x19950
movq -0xf8(%rbp), %rax
movq $0x0, 0x120(%rax)
movb $0x0, 0xf1(%rax)
jmp 0x1a7e4
movq -0xf8(%rbp), %rax
testb $0x1, 0xf1(%rax)
jne 0x1a7e2
movl $0x30, %edi
callq 0x19180
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x168(%rbp)
leaq 0x1d9f2(%rip), %rsi     # 0x38183
leaq 0x1d8b1(%rip), %rdx     # 0x38049
leaq 0x1d9a9(%rip), %r8      # 0x38148
movl $0xa7, %ecx
callq 0x22ba0
jmp 0x1a7ab
movq -0x168(%rbp), %rdi
leaq 0x2c547(%rip), %rsi     # 0x46d00
leaq 0x8950(%rip), %rdx      # 0x23110
callq 0x19480
movq -0x168(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
callq 0x19210
jmp 0x1a96d
jmp 0x1a7e4
movq -0xf8(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x1b880
cmpl $0x0, %eax
je 0x1a846
movq -0xf8(%rbp), %rax
movq 0x48(%rax), %rax
movl 0x20(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x1a835
callq 0x36960
movq %rax, %rsi
movq 0x2c7af(%rip), %rdi     # 0x46fd0
callq 0x192e0
movq %rax, %rdi
leaq 0x1d961(%rip), %rsi     # 0x38191
callq 0x192e0
movq -0xf8(%rbp), %rdi
callq 0x1e9b0
jmp 0x1a964
movq -0xf8(%rbp), %rax
movl $0x1, 0x78(%rax)
jmp 0x1a8fe
movq -0xf8(%rbp), %rax
cmpl $0x1, 0x78(%rax)
jne 0x1a8fc
movq -0xf8(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x1b9e0
jmp 0x1a87f
jmp 0x1a8e5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x1a96d
movq -0x78(%rbp), %rdi
callq 0x19120
movq -0xf8(%rbp), %rdi
movq %rax, -0xf0(%rbp)
addq $0x18, %rdi
leaq 0x1d844(%rip), %rsi     # 0x38103
callq 0x1b6d0
movq %rax, -0x170(%rbp)
jmp 0x1a8cd
movq -0x170(%rbp), %rdi
movl $0xffff8044, %esi       # imm = 0xFFFF8044
callq 0x1b770
jmp 0x1a8e0
callq 0x19450
jmp 0x1a8fc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
callq 0x19450
jmp 0x1a8fa
jmp 0x1a96d
jmp 0x1a8fe
jmp 0x1a286
movq -0xf8(%rbp), %rax
cmpl $0x3, 0x78(%rax)
jne 0x1a920
movq -0xf8(%rbp), %rax
movl $0x0, 0x78(%rax)
jmp 0x1a964
movq -0xf8(%rbp), %rax
cmpl $0x2, 0x78(%rax)
jne 0x1a92f
jmp 0x1a962
movq -0xf8(%rbp), %rax
cmpl $0x0, 0x78(%rax)
je 0x1a949
movq -0xf8(%rbp), %rax
cmpl $0x1, 0x78(%rax)
jne 0x1a960
movq -0xf8(%rbp), %rdi
addq $0x88, %rdi
leaq -0x28(%rbp), %rsi
callq 0x1ea60
jmp 0x1a962
jmp 0x1a964
addq $0x170, %rsp            # imm = 0x170
popq %rbp
retq
movq -0x78(%rbp), %rdi
callq 0x194a0
movq %rax, %rdi
callq 0x1eb30
nop