/** * query storage server to upload file * @param $tracker_server the connected tracker server (assoc array) * @param $storage_server return the storage server (assoc array, not connected) * @param $group_name the group to upload file to, can be empty * @return 0 for success, none zero (errno) for fail */ function tracker_query_storage_store($tracker_server, &$storage_server, $group_name) { if (empty($group_name)) { $cmd = TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITHOUT_GROUP; $out_len = 0; } else { $cmd = TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITH_GROUP; $out_len = FDFS_GROUP_NAME_MAX_LEN; } if (($result = fdfs_send_header($tracker_server, $out_len, $cmd, 0)) != 0) { return $result; } if (!empty($group_name)) { $groupname_len = strlen($group_name); if ($groupname_len >= FDFS_GROUP_NAME_MAX_LEN) { $body = substr($group_name, 0, FDFS_GROUP_NAME_MAX_LEN); } else { $body = $group_name; $body .= str_repeat("", FDFS_GROUP_NAME_MAX_LEN - $groupname_len); } if (fwrite($tracker_server['sock'], $body, FDFS_GROUP_NAME_MAX_LEN) != FDFS_GROUP_NAME_MAX_LEN) { error_log("server: {$server['ip_addr']}:{$server['port']}, " . "send data fail"); return FDFS_EIO; } } if (($result = fdfs_recv_response($tracker_server, TRACKER_QUERY_STORAGE_STORE_BODY_LEN, $in_buff, $in_bytes)) != 0) { return $result; } $storage_server = array('ip_addr' => trim(substr($in_buff, FDFS_GROUP_NAME_MAX_LEN, FDFS_IPADDR_SIZE - 1)), 'port' => fdfs_buff2long(substr($in_buff, FDFS_GROUP_NAME_MAX_LEN + FDFS_IPADDR_SIZE - 1, FDFS_PROTO_PKG_LEN_SIZE)), 'store_path_index' => ord(substr($in_buff, FDFS_GROUP_NAME_MAX_LEN + FDFS_IPADDR_SIZE - 1 + FDFS_PROTO_PKG_LEN_SIZE)), 'sock' => -1); return 0; }
/** * recv package header * @param $server connected tracker or storage server (assoc array) * @param $in_bytes return the package length * @return 0 for success, none zero (errno) for fail */ function fdfs_recv_header($server, &$in_bytes) { $tmpServer = $server['sock']; $pkg_len = fread($server['sock'], FDFS_PROTO_PKG_LEN_SIZE); $cmd = fread($server['sock'], 1); $status = fread($server['sock'], 1); if (ord($status) != 0) { $in_bytes = 0; return ord($status); } $in_bytes = fdfs_buff2long($pkg_len); if ($in_bytes < 0) { error_log("server: {$server['ip_addr']}:{$server['port']}, " . "recv package size {$in_bytes} is not correct"); $in_bytes = 0; return FDFS_EINVAL; } return 0; }