Author: cazfi Date: Mon Jun 9 23:43:04 2014 New Revision: 25113 URL: http://svn.gna.org/viewcvs/freeciv?rev=25113&view=rev Log: Handle metaserver reply to client in memory instead of writing it to temporary file.
See patch #4779 Modified: trunk/client/servers.c trunk/server/meta.c trunk/utility/netfile.c trunk/utility/netfile.h Modified: trunk/client/servers.c URL: http://svn.gna.org/viewcvs/freeciv/trunk/client/servers.c?rev=25113&r1=25112&r2=25113&view=diff ============================================================================== --- trunk/client/servers.c (original) +++ trunk/client/servers.c Mon Jun 9 23:43:04 2014 @@ -96,7 +96,7 @@ fc_mutex mutex; const char *urlpath; - FILE *fp; /* temp file */ + struct netfile_write_cb_data mem; } meta; }; @@ -220,7 +220,7 @@ char str[4096]; struct server_list *srvrs; - f = fz_from_stream(scan->meta.fp); + f = fz_from_memory(scan->meta.mem.mem, scan->meta.mem.size, TRUE); if (NULL == f) { fc_snprintf(str, sizeof(str), _("Failed to read the metaserver data from %s."), @@ -236,8 +236,8 @@ scan->srvrs.servers = srvrs; fc_release_mutex(&scan->srvrs.mutex); - /* 'f' (hence 'meta.fp') was closed in parse_metaserver_data(). */ - scan->meta.fp = NULL; + /* 'f' (hence 'meta.mem.mem') was closed in parse_metaserver_data(). */ + scan->meta.mem.mem = NULL; if (NULL == srvrs) { fc_snprintf(str, sizeof(str), @@ -259,45 +259,22 @@ { struct server_scan *scan = arg; - if (!scan->meta.fp) { -#ifdef WIN32_NATIVE - char filename[MAX_PATH]; - - GetTempPath(sizeof(filename), filename); - cat_snprintf(filename, sizeof(filename), "fctmp%d", fc_rand(1000)); - - scan->meta.fp = fc_fopen(filename, "w+b"); -#else - scan->meta.fp = tmpfile(); -#endif /* WIN32_NATIVE */ - - if (!scan->meta.fp) { - scan->error_func(scan, _("Could not open temp file.")); - } - } - - if (scan->meta.fp) { - - if (!begin_metaserver_scan(scan)) { + if (!begin_metaserver_scan(scan)) { + fc_allocate_mutex(&scan->meta.mutex); + scan->meta.status = SCAN_STATUS_ERROR; + } else { + if (!meta_read_response(scan)) { fc_allocate_mutex(&scan->meta.mutex); scan->meta.status = SCAN_STATUS_ERROR; } else { - - rewind(scan->meta.fp); - - if (!meta_read_response(scan)) { - fc_allocate_mutex(&scan->meta.mutex); - scan->meta.status = SCAN_STATUS_ERROR; - } else { - fc_allocate_mutex(&scan->meta.mutex); - if (scan->meta.status == SCAN_STATUS_WAITING) { - scan->meta.status = SCAN_STATUS_DONE; - } + fc_allocate_mutex(&scan->meta.mutex); + if (scan->meta.status == SCAN_STATUS_WAITING) { + scan->meta.status = SCAN_STATUS_DONE; } } - - fc_release_mutex(&scan->meta.mutex); - } + } + + fc_release_mutex(&scan->meta.mutex); } /**************************************************************************** @@ -314,7 +291,7 @@ post = netfile_start_post(); netfile_add_form_str(post, "client_cap", our_capability); - if (!netfile_send_post(metaserver, post, scan->meta.fp, NULL)) { + if (!netfile_send_post(metaserver, post, NULL, &scan->meta.mem, NULL)) { scan->error_func(scan, _("Error connecting to metaserver")); retval = FALSE; } @@ -835,9 +812,9 @@ fc_release_mutex(&scan->srvrs.mutex); } - if (scan->meta.fp) { - fclose(scan->meta.fp); - scan->meta.fp = NULL; + if (scan->meta.mem.mem) { + FC_FREE(scan->meta.mem.mem); + scan->meta.mem.mem = NULL; } } else { if (scan->sock >= 0) { Modified: trunk/server/meta.c URL: http://svn.gna.org/viewcvs/freeciv/trunk/server/meta.c?rev=25113&r1=25112&r2=25113&view=diff ============================================================================== --- trunk/server/meta.c (original) +++ trunk/server/meta.c Mon Jun 9 23:43:04 2014 @@ -230,7 +230,7 @@ addr = srvarg.bind_addr; } - if (!netfile_send_post(srvarg.metaserver_addr, post, NULL, addr)) { + if (!netfile_send_post(srvarg.metaserver_addr, post, NULL, NULL, addr)) { con_puts(C_METAERROR, _("Error connecting to metaserver")); metaserver_failed(); } Modified: trunk/utility/netfile.c URL: http://svn.gna.org/viewcvs/freeciv/trunk/utility/netfile.c?rev=25113&r1=25112&r2=25113&view=diff ============================================================================== --- trunk/utility/netfile.c (original) +++ trunk/utility/netfile.c Mon Jun 9 23:43:04 2014 @@ -49,12 +49,6 @@ return handle; } - -struct netfile_write_cb_data -{ - char *mem; - int size; -}; /********************************************************************** curl write callback to store received file to memory. @@ -223,7 +217,8 @@ Send HTTP POST ***********************************************************************/ bool netfile_send_post(const char *URL, struct netfile_post *post, - FILE *reply_fp, const char *addr) + FILE *reply_fp, struct netfile_write_cb_data *mem_data, + const char *addr) { CURLcode curlret; long http_resp; @@ -236,7 +231,12 @@ curl_easy_setopt(handle, CURLOPT_URL, URL); curl_easy_setopt(handle, CURLOPT_HTTPPOST, post->first); - if (reply_fp == NULL) { + if (mem_data != NULL) { + mem_data->mem = NULL; + mem_data->size = 0; + curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, netfile_memwrite_cb); + curl_easy_setopt(handle, CURLOPT_WRITEDATA, mem_data); + } else if (reply_fp == NULL) { curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, dummy_write); } else { curl_easy_setopt(handle, CURLOPT_WRITEDATA, reply_fp); Modified: trunk/utility/netfile.h URL: http://svn.gna.org/viewcvs/freeciv/trunk/utility/netfile.h?rev=25113&r1=25112&r2=25113&view=diff ============================================================================== --- trunk/utility/netfile.h (original) +++ trunk/utility/netfile.h Mon Jun 9 23:43:04 2014 @@ -20,6 +20,12 @@ struct netfile_post; +struct netfile_write_cb_data +{ + char *mem; + int size; +}; + typedef void (*nf_errmsg)(const char *msg, void *data); struct section_file *netfile_get_section_file(const char *URL, @@ -36,7 +42,8 @@ void netfile_close_post(struct netfile_post *post); bool netfile_send_post(const char *URL, struct netfile_post *post, - FILE *reply_fp, const char *addr); + FILE *reply_fp, struct netfile_write_cb_data *mem_data, + const char *addr); #ifdef __cplusplus } _______________________________________________ Freeciv-commits mailing list Freeciv-commits@gna.org https://mail.gna.org/listinfo/freeciv-commits