// File: /workspace/github_cpp_makefile_star2_O1/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 %r15
pushq %r14
pushq %rbx
subq $0x30, %rsp
movq %rcx, %r14
movq %rdx, %r15
movq %rdi, %rbx
xorl %edx, %edx
callq 0x20460
leaq 0x31e18(%rip), %rax     # 0x456c0
movq %rax, (%rbx)
movb $0x0, 0x100(%rbx)
movq %r15, 0x108(%rbx)
movq %r14, 0x110(%rbx)
leaq 0x1b0(%rbx), %rax
xorps %xmm0, %xmm0
movups %xmm0, 0x118(%rbx)
movups %xmm0, 0x121(%rbx)
movups %xmm0, 0x138(%rbx)
movups %xmm0, 0x148(%rbx)
movups %xmm0, 0x158(%rbx)
movups %xmm0, 0x168(%rbx)
movq $0x0, 0x178(%rbx)
movq %rax, 0x180(%rbx)
movq $0x1, 0x188(%rbx)
movaps %xmm0, 0x190(%rbx)
movl $0x3f800000, 0x1a0(%rbx) # imm = 0x3F800000
movups %xmm0, 0x1a8(%rbx)
leaq 0x333d0(%rip), %rax     # 0x46d00
lock
incq 0x80(%rax)
lock
incq 0x88(%rax)
movaps %xmm0, (%rsp)
movaps %xmm0, 0x10(%rsp)
movq %rbx, (%rsp)
leaq 0xd02(%rip), %rcx       # 0x14656
movq %rcx, 0x18(%rsp)
leaq 0xeb6(%rip), %rdx       # 0x14816
movq %rdx, 0x10(%rsp)
movaps (%rsp), %xmm0
movaps %xmm0, 0x20(%rsp)
movups 0x140(%rbx), %xmm1
movaps %xmm1, (%rsp)
movups %xmm0, 0x140(%rbx)
movq 0x150(%rbx), %rax
movq %rax, 0x10(%rsp)
movq %rdx, 0x150(%rbx)
movq 0x158(%rbx), %rdx
movq %rdx, 0x18(%rsp)
movq %rcx, 0x158(%rbx)
testq %rax, %rax
je 0x139b8
movq %rsp, %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
leaq 0x160(%rbx), %rax
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movaps %xmm0, 0x10(%rsp)
movq %rbx, (%rsp)
leaq 0xe66(%rip), %rcx       # 0x1483c
movq %rcx, 0x18(%rsp)
leaq 0xeb0(%rip), %rdx       # 0x14892
movq %rdx, 0x10(%rsp)
movaps (%rsp), %xmm0
movaps %xmm0, 0x20(%rsp)
movups (%rax), %xmm1
movaps %xmm1, (%rsp)
movups %xmm0, (%rax)
movq 0x170(%rbx), %rax
movq %rax, 0x10(%rsp)
movq %rdx, 0x170(%rbx)
movq 0x178(%rbx), %rdx
movq %rdx, 0x18(%rsp)
movq %rcx, 0x178(%rbx)
testq %rax, %rax
je 0x13a32
movq %rsp, %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
addq $0x30, %rsp
popq %rbx
popq %r14
popq %r15
retq
jmp 0x13a3e
movq %rax, %rdi
callq 0x8fc6