Exemple #1
0
/*
 * 函 数:mq_sm_db_write_msg
 * 功 能:把msg_item消息写入到mq_queue队列中
 * 返回值:成功返回true,失败返回false
 * 
 * 步骤:1.检查文件剩余空间是否足够,如果不够则创建新的文件
 *   
 */
bool mq_sm_db_write_msg(mq_queue_t *mq_queue, msg_item_t *msg_item)
{
    char*     p;
    uint32_t  temp_write_pos;
    //1.检查文件剩余空间是否足够,如果不够则创建新的文件
    if (mq_queue->cur_wdb.pos + msg_item->len + MSG_HEAD_LEN > MAX_DB_FILE_SIZE)
    {
        log_debug("Current DB file write full; Get next DB file for write");
        /* Write current status to data file befor close it */
        //因为在文件创建时向文件写了个消息用于占位,所以在关闭文件之前会再次在文件头部点位的地方重写一条消息,记录当前文件总共写了多少条消息
        if (mq_queue->cur_wdb.map_mem != NULL){
            mq_sm_db_write_file_head(mq_queue);
        }
        
        pthread_mutex_lock(&(mq_queue->mutex));
        if (!get_next_write_file(mq_queue))
        {
            pthread_mutex_unlock(&(mq_queue->mutex));
            log_error("Get next DB file fail");
            return false;
        }
        pthread_mutex_unlock(&(mq_queue->mutex));
    }
    log_debug("Write msg begin!");
    temp_write_pos = mq_queue->cur_wdb.pos;
    p = mq_queue->cur_wdb.map_mem + temp_write_pos;

    //写入magic_num1
    memcpy(p, &(msg_item->magic_num1), MSG_HEAD_OF_ONE_MAGIC_NUM_LEN);
    p += MSG_HEAD_OF_ONE_MAGIC_NUM_LEN;
    //写入magic_num2
    memcpy(p, &(msg_item->magic_num2), MSG_HEAD_OF_ONE_MAGIC_NUM_LEN);
    p += MSG_HEAD_OF_ONE_MAGIC_NUM_LEN;
    //写入delay
    memcpy(p, &(msg_item->delay), MSG_HEAD_OF_DELAY_LEN);
    p += MSG_HEAD_OF_DELAY_LEN;
    //写入crc16
    memcpy(p, &(msg_item->crc16), MSG_HEAD_OF_CRC16_LEN);
    p += MSG_HEAD_OF_CRC16_LEN;
    //写入消息长度值
    memcpy(p, &(msg_item->len), MSG_HEAD_OF_MSGLEN_LEN);
    p += MSG_HEAD_OF_MSGLEN_LEN;
    //写入消息体
    memcpy(p, msg_item->msg, msg_item->len);
    //修改偏移指针
    mq_queue->cur_wdb.pos       += (msg_item->len + MSG_HEAD_LEN);
    //增加写入的次数
    mq_queue->cur_wdb.opt_count += 1;
    //增加写入的次数
    mq_queue->put_num           += 1;
    log_debug("Write msg seccuss!");

    return true;
}
Exemple #2
0
/* Write mssage to DB file */
bool mq_sm_db_write_msg(mq_queue_t *mq_queue, msg_item_t *msg_item)
{
    char*     p;
    uint32_t  temp_write_pos;

    if (mq_queue->cur_wdb.pos + msg_item->len + MSG_HEAD_LEN > MAX_DB_FILE_SIZE)
    {
        log_debug("Current DB file write full; Get next DB file for write");
        /* Write current status to data file befor close it */
        
        if (mq_queue->cur_wdb.map_mem != NULL){
            mq_sm_db_write_file_head(mq_queue);
        }
        
        if (!get_next_write_file(mq_queue))
        {
            log_error("Get next DB file fail");
            return false;
        }
    }
    log_debug("Write msg begin!");
    temp_write_pos = mq_queue->cur_wdb.pos;
    p = mq_queue->cur_wdb.map_mem + temp_write_pos;

    memcpy(p, &(msg_item->magic_num1), MSG_HEAD_OF_ONE_MAGIC_NUM_LEN);
    p += MSG_HEAD_OF_ONE_MAGIC_NUM_LEN;
    memcpy(p, &(msg_item->magic_num2), MSG_HEAD_OF_ONE_MAGIC_NUM_LEN);
    p += MSG_HEAD_OF_ONE_MAGIC_NUM_LEN;
    memcpy(p, &(msg_item->delay), MSG_HEAD_OF_DELAY_LEN);
    p += MSG_HEAD_OF_DELAY_LEN;
    memcpy(p, &(msg_item->crc16), MSG_HEAD_OF_CRC16_LEN);
    p += MSG_HEAD_OF_CRC16_LEN;
    memcpy(p, &(msg_item->len), MSG_HEAD_OF_MSGLEN_LEN);
    p += MSG_HEAD_OF_MSGLEN_LEN;
    memcpy(p, msg_item->msg, msg_item->len);

    mq_queue->cur_wdb.pos       += (msg_item->len + MSG_HEAD_LEN);
    mq_queue->cur_wdb.opt_count += 1;
    mq_queue->put_num           += 1;
    log_debug("Write msg seccuss!");

    return true;
}