fastdfs函数tracker_query_storage_update1分析

粘贴第一个函数

tracker_query_storage_update1(pTrackerServer, &storageServer, file_id);

函数解析:函数名:tracker_query_storage_update1,参数:in(pTrackerServer),in(file_id) ,out(storageServer)。
函数意义:传入:pTrackerServer,和file_id,返回storageServer


粘贴第二个函数

int tracker_query_storage_update1(ConnectionInfo *pTrackerServer,ConnectionInfo *pStorageServer,const char *file_id)
{
    `FDFS_SPLIT_GROUP_NAME_AND_FILENAME`(file_id)
    return tracker_query_storage_update(pTrackerServer, 
    pStorageServer, group_name, filename);
}

函数意义:将函数tracker_query_storage_update1传进来的file_id进行剥离。


剥离函数:

#define FDFS_SPLIT_GROUP_NAME_AND_FILENAME(file_id) \
    char new_file_id[FDFS_GROUP_NAME_MAX_LEN + 128]; \
    char *group_name; \
    char *filename; \
    char *pSeperator; \
    \
    snprintf(new_file_id, sizeof(new_file_id), "%s", file_id); \
    pSeperator = strchr(new_file_id, FDFS_FILE_ID_SEPERATOR); \
    if (pSeperator == NULL) \
    { \
        return EINVAL; \
    } \
    \
    *pSeperator = '\0'; \
    group_name = new_file_id; \
    filename =  pSeperator + 1; \

将file_id剥离出:group_name,例如:group1
剥离出:filename,例如:/M00/00/01/wKgBIFVjCM*.jpg


粘贴函数

#define tracker_query_storage_update(pTrackerServer, 
pStorageServer, group_name, filename) tracker_do_query_storage(pTrackerServer, pStorageServer, RACKER_PROTO_CMD_SERVICE_QUERY_UPDATE,
group_name, filename)

函数解析:
/**
* query storage server to update (delete file or set meta data)
* params:
* pTrackerServer: tracker server
* pStorageServer: return storage server
* group_name: the group name of storage server
* ilename: filename on storage server
* return: 0 success, !=0 fail, return the error code
**/

int tracker_do_query_storage(ConnectionInfo *pTrackerServer, 
ConnectionInfo *pStorageServer, const byte cmd, 
const char *group_name, const char *filename)
{
    TrackerHeader *pHeader;//定义头部结构体
    ConnectionInfo *conn;
    bool new_connection;//是否为新的连接
    char out_buff[sizeof(TrackerHeader) + FDFS_GROUP_NAME_MAX_LEN + 128];
    char in_buff[sizeof(TrackerHeader) + TRACKER_QUERY_STORAGE_FETCH_BODY_LEN];
    char *pInBuff;
    int64_t in_bytes;
    int result;
    int filename_len;

    CHECK_CONNECTION(pTrackerServer, conn, result, new_connection);//检查连接是否正常,不正常,建立新连接

    memset(pStorageServer, 0, sizeof(ConnectionInfo));
    pStorageServer->sock = -1;

    memset(out_buff, 0, sizeof(out_buff));
    pHeader = (TrackerHeader *)out_buff;
    snprintf(out_buff + sizeof(TrackerHeader), sizeof(out_buff) - 
sizeof(TrackerHeader),  "%s", group_name);
    filename_len = snprintf(out_buff + sizeof(TrackerHeader) + 
FDFS_GROUP_NAME_MAX_LEN,sizeof(out_buff) - sizeof(TrackerHeader) - FDFS_GROUP_NAME_MAX_LEN,  "%s", filename);//拷贝filename 到out_buff,返回拷贝的字节数。

//以下是构建头部数据[long2buff](http://blog.csdn.net/akakakak250/article/details/45672019)
    long2buff(FDFS_GROUP_NAME_MAX_LEN + filename_len, pHeader->pkg_len);
    pHeader->cmd = cmd;
    if ((result=tcpsenddata_nb(conn->sock, out_buff, \
        sizeof(TrackerHeader) + FDFS_GROUP_NAME_MAX_LEN + filename_len, g_fdfs_network_timeout)) != 0)//成功返回0
    {
        logError("file: "__FILE__", line: %d, " \
            "send data to tracker server %s:%d fail, " 
            "errno: %d, error info: %s", __LINE__, \
            pTrackerServer->ip_addr, \
            pTrackerServer->port, \
            result, STRERROR(result));
    }
    else
    {
        pInBuff = in_buff;
        result = fdfs_recv_response(conn, \
            &pInBuff, sizeof(in_buff), &in_bytes);
    }//in_byte,返回字节数。fdfs_recv_response,里面有两个函数,一个是获取头,和数据

    if (new_connection)
    {
        tracker_disconnect_server_ex(conn, result != 0);
    }

    if (result != 0)
    {
        return result;
    }

    if (in_bytes != TRACKER_QUERY_STORAGE_FETCH_BODY_LEN)//in_bytes是固定大小
    {
        logError("file: "__FILE__", line: %d, " \
            "tracker server %s:%d response data " \
            "length: %"PRId64" is invalid, " \
            "expect length: %d", __LINE__, \
            pTrackerServer->ip_addr, \
            pTrackerServer->port, in_bytes, \
            TRACKER_QUERY_STORAGE_FETCH_BODY_LEN);
        return EINVAL;
    }

    memcpy(pStorageServer->ip_addr, in_buff + \
            FDFS_GROUP_NAME_MAX_LEN, IP_ADDRESS_SIZE-1);//获取storage的ip
    pStorageServer->port = (int)buff2long(in_buff + \
            FDFS_GROUP_NAME_MAX_LEN + IP_ADDRESS_SIZE - 1);//获取storage的port
    return 0;
}

整体解释:根据传入pTrackerServer, file_id,查询storageServer, 返回ip,port。

刚接触fastdfs不是很熟,有不对的地方提出,共同进步

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值