// File: cybozu[P]yrmcds/src/counter/sockets.cpp
// Function: yrmcds::counter::counter_socket::counter_socket(int, std::function<cybozu::worker* ()> const&, cybozu::hash_map<yrmcds::counter::object>&)
counter_socket::counter_socket(int fd,
                                   const std::function<cybozu::worker*()>& finder,
                                   cybozu::hash_map<object>& hash)
    : cybozu::tcp_socket(fd),
      m_busy(false),
      m_finder(finder),
      m_hash(hash),
      m_pending(0) {
    g_stats.curr_connections.fetch_add(1);
    g_stats.total_connections.fetch_add(1);

    m_recvjob = [this](cybozu::dynbuf& buf) {
        // load pending data
        if( ! m_pending.empty() ) {
            buf.append(m_pending.data(), m_pending.size());
            m_pending.reset();
        }

        while( true ) {
            auto res = receive(buf, MAX_RECVSIZE);
            if( res == recv_result::AGAIN )
                break;
            if( res == recv_result::RESET || res == recv_result::NONE ) {
                buf.reset();
                release_all();
                break;
            }

            const char* head = buf.data();
            std::size_t len = buf.size();
            while( len > 0 ) {
                counter::request parser(head, len);
                std::size_t c = parser.length();
                if( c == 0 ) break;
                head += c;
                len -= c;
                execute(parser);
            }
            if( len > MAX_REQUEST_LENGTH ) {
                cybozu::logger::warning() << "denied too large request of "
                                          << len << " bytes.";
                buf.reset();
                release_all();
                invalidate_and_close();
                break;
            }
            buf.erase(head - buf.data());
        }

        // recv returns EAGAIN, or some error happens.
        if( buf.size() > 0 )
            m_pending.append(buf.data(), buf.size());

        m_busy.store(false, std::memory_order_release);
    };

    m_sendjob = [this](cybozu::dynbuf& buf) {
        with_fd([=](int fd) -> bool {
            return write_pending_data(fd);
        });
    };
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x58(%rbp)
movl %esi, -0x5c(%rbp)
movq %rdx, -0x68(%rbp)
movq %rcx, -0x70(%rbp)
movq -0x58(%rbp), %rdi
movq %rdi, -0xa0(%rbp)
movl -0x5c(%rbp), %esi
xorl %edx, %edx
movl %edx, -0x94(%rbp)
callq 0x55720
movq -0xa0(%rbp), %rdi
movl -0x94(%rbp), %esi
leaq 0x7f77f(%rip), %rax     # 0xae6b8
movq %rax, (%rdi)
addq $0x100, %rdi            # imm = 0x100
callq 0x22f60
movq -0xa0(%rbp), %rdi
movl -0x94(%rbp), %edx
movq -0x68(%rbp), %rax
movq %rax, 0x108(%rdi)
movq -0x70(%rbp), %rax
movq %rax, 0x110(%rdi)
addq $0x118, %rdi            # imm = 0x118
movl %edx, %esi
callq 0x22f90
jmp 0x2ef7b
movq -0xa0(%rbp), %rdi
addq $0x140, %rdi            # imm = 0x140
callq 0x23000
movq -0xa0(%rbp), %rdi
addq $0x160, %rdi            # imm = 0x160
callq 0x23000
movq -0xa0(%rbp), %rdi
addq $0x180, %rdi            # imm = 0x180
callq 0x327c0
leaq 0x80e85(%rip), %rax     # 0xafe40
subq $-0x80, %rax
movq %rax, -0x8(%rbp)
movq $0x1, -0x10(%rbp)
movl $0x5, -0x14(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xb0(%rbp)
movl -0x14(%rbp), %eax
movq -0x10(%rbp), %rcx
movq %rcx, -0x20(%rbp)
decl %eax
movl %eax, %ecx
movq %rcx, -0xa8(%rbp)
subl $0x4, %eax
ja 0x2f00f
movq -0xa8(%rbp), %rax
leaq 0x4bf3e(%rip), %rcx     # 0x7af44
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0xb0(%rbp), %rcx
movq -0x20(%rbp), %rax
lock
xaddq %rax, (%rcx)
movq %rax, -0x28(%rbp)
jmp 0x2f07b
movq -0xb0(%rbp), %rcx
movq -0x20(%rbp), %rax
lock
xaddq %rax, (%rcx)
movq %rax, -0x28(%rbp)
jmp 0x2f07b
movq -0xb0(%rbp), %rcx
movq -0x20(%rbp), %rax
lock
xaddq %rax, (%rcx)
movq %rax, -0x28(%rbp)
jmp 0x2f07b
movq -0xb0(%rbp), %rcx
movq -0x20(%rbp), %rax
lock
xaddq %rax, (%rcx)
movq %rax, -0x28(%rbp)
jmp 0x2f07b
movq -0xb0(%rbp), %rcx
movq -0x20(%rbp), %rax
lock
xaddq %rax, (%rcx)
movq %rax, -0x28(%rbp)
leaq 0x80dbe(%rip), %rax     # 0xafe40
addq $0x88, %rax
movq %rax, -0x30(%rbp)
movq $0x1, -0x38(%rbp)
movl $0x5, -0x3c(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0xc0(%rbp)
movl -0x3c(%rbp), %eax
movq -0x38(%rbp), %rcx
movq %rcx, -0x48(%rbp)
decl %eax
movl %eax, %ecx
movq %rcx, -0xb8(%rbp)
subl $0x4, %eax
ja 0x2f0d8
movq -0xb8(%rbp), %rax
leaq 0x4be89(%rip), %rcx     # 0x7af58
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0xc0(%rbp), %rcx
movq -0x48(%rbp), %rax
lock
xaddq %rax, (%rcx)
movq %rax, -0x50(%rbp)
jmp 0x2f144
movq -0xc0(%rbp), %rcx
movq -0x48(%rbp), %rax
lock
xaddq %rax, (%rcx)
movq %rax, -0x50(%rbp)
jmp 0x2f144
movq -0xc0(%rbp), %rcx
movq -0x48(%rbp), %rax
lock
xaddq %rax, (%rcx)
movq %rax, -0x50(%rbp)
jmp 0x2f144
movq -0xc0(%rbp), %rcx
movq -0x48(%rbp), %rax
lock
xaddq %rax, (%rcx)
movq %rax, -0x50(%rbp)
jmp 0x2f144
movq -0xc0(%rbp), %rcx
movq -0x48(%rbp), %rax
lock
xaddq %rax, (%rcx)
movq %rax, -0x50(%rbp)
movq -0xa0(%rbp), %rdi
movq %rdi, -0x88(%rbp)
addq $0x140, %rdi            # imm = 0x140
leaq -0x88(%rbp), %rsi
callq 0x2f1b0
movq -0xa0(%rbp), %rdi
movq %rdi, -0x90(%rbp)
addq $0x160, %rdi            # imm = 0x160
leaq -0x90(%rbp), %rsi
callq 0x2f200
addq $0xc0, %rsp
popq %rbp
retq
movq -0xa0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
callq 0x33280
movq -0x78(%rbp), %rdi
callq 0x8bc0