int aeron_publication_image_send_pending_loss(aeron_publication_image_t *image)
{
    int work_count = 0;

    if (NULL != image->endpoint && AERON_PUBLICATION_IMAGE_STATUS_ACTIVE == image->conductor_fields.status)
    {
        int64_t change_number;
        AERON_GET_VOLATILE(change_number, image->end_loss_change);

        if (change_number != image->last_loss_change_number)
        {
            const int32_t term_id = image->loss_term_id;
            const int32_t term_offset = image->loss_term_offset;
            const int32_t length = (int32_t)image->loss_length;

            aeron_acquire();

            if (change_number == image->begin_loss_change)
            {
                if (image->conductor_fields.is_reliable)
                {
                    int send_nak_result = aeron_receive_channel_endpoint_send_nak(
                        image->endpoint,
                        &image->control_address,
                        image->stream_id,
                        image->session_id,
                        term_id,
                        term_offset,
                        length);

                    aeron_counter_ordered_increment(image->nak_messages_sent_counter, 1);
                    work_count = send_nak_result < 0 ? send_nak_result : 1;
                }
                else
                {
                    const size_t index = aeron_logbuffer_index_by_term(image->initial_term_id, term_id);
                    uint8_t *buffer = image->mapped_raw_log.term_buffers[index].addr;

                    if (aeron_term_gap_filler_try_fill_gap(image->log_meta_data, buffer, term_id, term_offset, length))
                    {
                        aeron_counter_ordered_increment(image->loss_gap_fills_counter, 1);
                    }

                    work_count = 1;
                }

                image->last_loss_change_number = change_number;
            }
        }
    }

    return work_count;
}
int aeron_publication_image_insert_packet(
    aeron_publication_image_t *image, int32_t term_id, int32_t term_offset, const uint8_t *buffer, size_t length)
{
    const bool is_heartbeat = aeron_publication_image_is_heartbeat(buffer, length);
    const int64_t packet_position = aeron_logbuffer_compute_position(
        term_id, term_offset, image->position_bits_to_shift, image->initial_term_id);
    const int64_t proposed_position = is_heartbeat ? packet_position : packet_position + (int64_t)length;

    if (!aeron_publication_image_is_flow_control_under_run(image, packet_position) &&
        !aeron_publication_image_is_flow_control_over_run(image, proposed_position))
    {
        if (is_heartbeat)
        {
            if (!image->is_end_of_stream && aeron_publication_image_is_end_of_stream(buffer, length))
            {
                AERON_PUT_ORDERED(image->is_end_of_stream, true);
                AERON_PUT_ORDERED(image->log_meta_data->end_of_stream_position, packet_position);
            }

            aeron_counter_ordered_increment(image->heartbeats_received_counter, 1);
        }
        else
        {
            const size_t index = aeron_logbuffer_index_by_position(packet_position, image->position_bits_to_shift);
            uint8_t *term_buffer = image->mapped_raw_log.term_buffers[index].addr;

            aeron_term_rebuilder_insert(term_buffer + term_offset, buffer, length);
        }

        AERON_PUT_ORDERED(image->last_packet_timestamp_ns, image->nano_clock());
        aeron_counter_propose_max_ordered(image->rcv_hwm_position.value_addr, proposed_position);
    }

    return (int)length;
}
void aeron_driver_receiver_proxy_offer(aeron_driver_receiver_proxy_t *receiver_proxy, void *cmd)
{
    while (aeron_spsc_concurrent_array_queue_offer(receiver_proxy->command_queue, cmd) != AERON_OFFER_SUCCESS)
    {
        aeron_counter_ordered_increment(receiver_proxy->fail_counter, 1);
        sched_yield();
    }
}
int aeron_publication_image_send_pending_status_message(aeron_publication_image_t *image)
{
    int work_count = 0;

    if (NULL != image->endpoint && AERON_PUBLICATION_IMAGE_STATUS_ACTIVE == image->conductor_fields.status)
    {
        int64_t change_number;
        AERON_GET_VOLATILE(change_number, image->end_sm_change);

        if (change_number != image->last_sm_change_number)
        {
            const int64_t sm_position = image->next_sm_position;
            const int32_t receiver_window_length = image->next_sm_receiver_window_length;

            aeron_acquire();

            if (change_number == image->begin_sm_change)
            {
                const int32_t term_id = aeron_logbuffer_compute_term_id_from_position(
                    sm_position, image->position_bits_to_shift, image->initial_term_id);
                const int32_t term_offset = (int32_t)(sm_position & image->term_length_mask);

                int send_sm_result = aeron_receive_channel_endpoint_send_sm(
                    image->endpoint,
                    &image->control_address,
                    image->stream_id,
                    image->session_id,
                    term_id,
                    term_offset,
                    receiver_window_length,
                    0);

                aeron_counter_ordered_increment(image->status_messages_sent_counter, 1);

                image->last_sm_change_number = change_number;
                image->last_sm_position = sm_position;
                image->last_sm_position_window_limit = sm_position + receiver_window_length;
                work_count = send_sm_result < 0 ? send_sm_result : 1;
            }
        }
    }

    return work_count;
}