Пример #1
0
static void roadmap_http_async_connect_cb (RoadMapSocket socket, void *context, roadmap_result err) {

	HttpAsyncContext *hcontext = (HttpAsyncContext *)context;
   RoadMapHttpAsyncCallbacks *callbacks = hcontext->callbacks;

   if (!ROADMAP_NET_IS_VALID(socket)) {
      callbacks->error(hcontext->cb_context, 1, "Can't connect to server.");
      free (hcontext);
      return;
   }

   hcontext->io.subsystem = ROADMAP_IO_NET;
   hcontext->io.context = context;
   hcontext->io.os.socket = socket;

   if (roadmap_io_write(&hcontext->io, "\r\n", 2, 0) == -1) {
      roadmap_io_close(&hcontext->io);
      callbacks->error(hcontext->cb_context, 1, "Error sending request.");
      free (hcontext);
      return;
   }

   hcontext->header_buffer[0] = '\0';
   hcontext->is_parsing_headers = 1;
   hcontext->download_size_current = 0;
   hcontext->received_status = 0;
   hcontext->content_length = -1;

   roadmap_main_set_input(&hcontext->io, roadmap_http_async_has_data_cb);
   callbacks->progress (hcontext->cb_context, NULL, 0);
}
Пример #2
0
void roadmap_http_async_copy_abort (HttpAsyncContext *context) {
   /*
    * The context can be deallocated earlier by callback, however this function
    * can be called with no consideration on the callback execution.
    * The responsibility of the API callbacks to null the pointer on error
    */
   if ( context != NULL )
   {
      if (ROADMAP_NET_IS_VALID(context->io.os.socket)) {
	   roadmap_main_remove_input(&context->io);
	   roadmap_io_close (&context->io);
	  }
      free (context);
   }
}
Пример #3
0
RoadMapSocket roadmap_gpsd2_connect (const char *name) {

   RoadMapSocket socket = roadmap_net_connect ("tcp", name, 0, 2947, NULL);

   if (ROADMAP_NET_IS_VALID(socket)) {

      /* Start watching what happens. */

      static const char request[] = "w+\n";

      if (roadmap_net_send
            (socket, request, sizeof(request)-1, 1) != sizeof(request)-1) {

         roadmap_log (ROADMAP_WARNING, "Lost gpsd server session");
         roadmap_net_close (socket);

         return ROADMAP_INVALID_SOCKET;
      }
   }

   return socket;
}
Пример #4
0
static int roadmap_httpcopy (RoadMapDownloadCallbacks *callbacks,
                             const char *source,
                             const char *destination) {

   RoadMapSocket fd;
   RoadMapFile file;
   int size;
   int loaded;
   int received;

   char buffer[ROADMAP_HTTP_MAX_CHUNK];


   fd = roadmap_net_connect("http_get", source, 80, NULL);
   if (!ROADMAP_NET_IS_VALID(fd)) return 0;
   if (roadmap_net_send(fd, "\r\n", 2, 0) == -1) return 0;

   received = sizeof(buffer);
   size = roadmap_http_decode_header
             (fd, buffer, &received, callbacks->error);
   if (size <= 0) {
      roadmap_net_close (fd);
      return 0; /* We did not get the size. */
   }

   if (! callbacks->size (size)) {
      roadmap_net_close (fd);
      return 0;
   }

   callbacks->progress (received);
   roadmap_file_remove (NULL, destination);
   file = roadmap_file_open(destination, "w");
   if (!ROADMAP_FILE_IS_VALID(file)) {
      roadmap_net_close (fd);
      return 0;
   }

   if (received > 0) {
      if (roadmap_file_write(file, buffer, received) != received) {
         callbacks->error ("Error writing data");
         goto cancel_download;
      }
   }
   loaded = received;

   while (loaded < size) {

      received = roadmap_net_receive (fd, buffer, sizeof(buffer));

      if (received <= 0) {
         callbacks->error ("Receive error after %d data bytes", loaded);
         goto cancel_download;
      }
      
      if (roadmap_file_write(file, buffer, received) != received) {
         callbacks->error ("Error writing data");
         goto cancel_download;
      }

      loaded += received;

      callbacks->progress (loaded);
   }

   if (loaded != size) {
      callbacks->error ("Receive error after %d data bytes", loaded);
      goto cancel_download;   
   }
   roadmap_net_close (fd);
   roadmap_file_close(file);

   return 1;

cancel_download:

   roadmap_file_close(file);
   roadmap_file_remove (NULL, destination);
   roadmap_net_close (fd);

   return 0;
}