// File: /workspace/github_cpp_makefile_star2_O3/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 %r12
pushq %rbx
subq $0x38, %rsp
movq %rcx, %r14
movq %rdx, %r15
movq %rdi, %rbx
xorl %r12d, %r12d
xorl %edx, %edx
callq 0x1fc3e
leaq 0x31e0f(%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 %r12, 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 0x333cb(%rip), %rax     # 0x46d00
lock
incq 0x80(%rax)
lock
incq 0x88(%rax)
movq %r12, 0x8(%rsp)
movq %rbx, (%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)
leaq 0xdf0(%rip), %rcx       # 0x1476c
movq %rcx, 0x150(%rbx)
movq 0x158(%rbx), %rcx
movq %rcx, 0x18(%rsp)
leaq 0xc14(%rip), %rcx       # 0x145aa
movq %rcx, 0x158(%rbx)
testq %rax, %rax
je 0x139af
movq %rsp, %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
leaq 0x160(%rbx), %rax
movq $0x0, 0x8(%rsp)
movq %rbx, (%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)
leaq 0xdff(%rip), %rcx       # 0x147e8
movq %rcx, 0x170(%rbx)
movq 0x178(%rbx), %rcx
movq %rcx, 0x18(%rsp)
leaq 0xd8f(%rip), %rcx       # 0x14792
movq %rcx, 0x178(%rbx)
testq %rax, %rax
je 0x13a1c
movq %rsp, %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
jmp 0x13a2a
movq %rax, %rdi
callq 0x8f80