// File: /workspace/github_cpp_makefile_star2_O2/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 0x1bcf6
leaq 0x287f0(%rip), %rax     # 0x3a6c0
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)
andq $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 0x29dab(%rip), %rax     # 0x3bd00
lock
incq 0x80(%rax)
lock
incq 0x88(%rax)
leaq 0x10(%rsp), %r14
andq $0x0, 0x8(%r14)
movq %rbx, (%r14)
movaps (%r14), %xmm0
movaps %xmm0, (%rsp)
movups 0x140(%rbx), %xmm1
movaps %xmm1, (%r14)
movups %xmm0, 0x140(%rbx)
movaps 0x150(%rbx), %xmm0
leaq 0x99c(%rip), %rax       # 0x12936
movq %rax, 0x150(%rbx)
movaps %xmm0, 0x10(%r14)
leaq 0x823(%rip), %rax       # 0x127d0
movq %rax, 0x158(%rbx)
movq %r14, %rdi
callq 0x8d90
andq $0x0, 0x8(%r14)
movq %rbx, (%r14)
movaps (%r14), %xmm0
movaps %xmm0, (%rsp)
movups 0x160(%rbx), %xmm1
movaps %xmm1, (%r14)
movups %xmm0, 0x160(%rbx)
movaps 0x170(%rbx), %xmm0
leaq 0x9ca(%rip), %rax       # 0x129b6
movq %rax, 0x170(%rbx)
movaps %xmm0, 0x10(%r14)
leaq 0x95d(%rip), %rax       # 0x1295c
movq %rax, 0x178(%rbx)
leaq 0x10(%rsp), %rdi
callq 0x8d90
addq $0x30, %rsp
popq %rbx
popq %r14
popq %r15
retq