- fix
[oweals/gnunet.git] / src / util / client.c
index 337eade0cc945639820f5a40b8e082906e6ec236..8b4776201eaeb06bf489803dc41dda75199dad1d 100644 (file)
@@ -236,6 +236,11 @@ struct GNUNET_CLIENT_Connection
    */
   int in_receive;
 
+  /**
+   * Is this the first message we are sending to the service?
+   */
+  int first_message;
+
   /**
    * How often have we tried to connect?
    */
@@ -419,6 +424,7 @@ GNUNET_CLIENT_connect (const char *service_name,
     return NULL;
   connection = do_connect (service_name, cfg, 0);
   client = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_Connection));
+  client->first_message = GNUNET_YES;
   client->attempts = 1;
   client->connection = connection;
   client->service_name = GNUNET_strdup (service_name);
@@ -937,10 +943,19 @@ client_delayed_retry (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct GNUNET_CLIENT_TransmitHandle *th = cls;
   struct GNUNET_TIME_Relative delay;
-
+  
   th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
+  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
+  {
+    /* give up, was shutdown */
+    th->client->th = NULL;
+    th->notify (th->notify_cls, 0, NULL);
+    GNUNET_free (th);
+    return;
+  }
   th->client->connection =
       do_connect (th->client->service_name, th->client->cfg, th->client->attempts++);
+  th->client->first_message = GNUNET_YES;
   if (NULL == th->client->connection)
   {
     /* could happen if we're out of sockets */
@@ -992,6 +1007,7 @@ client_notify (void *cls, size_t size, void *buf)
   size_t ret;
   struct GNUNET_TIME_Relative delay;
 
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "client_notify is running\n");
   th->th = NULL;
   client->th = NULL;
   if (NULL == buf)
@@ -999,7 +1015,8 @@ client_notify (void *cls, size_t size, void *buf)
     delay = GNUNET_TIME_absolute_get_remaining (th->timeout);
     delay.rel_value /= 2;
     if ((GNUNET_YES != th->auto_retry) || (0 == --th->attempts_left) ||
-        (delay.rel_value < 1))
+        (delay.rel_value < 1)||
+       (0 != (GNUNET_SCHEDULER_get_reason() & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
     {
       LOG (GNUNET_ERROR_TYPE_DEBUG,
            "Transmission failed %u times, giving up.\n",
@@ -1080,7 +1097,9 @@ GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *client,
   th->client = client;
   th->size = size;
   th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
-  th->auto_retry = auto_retry;
+  /* always auto-retry on first message to service */
+  th->auto_retry = (GNUNET_YES == client->first_message) ? GNUNET_YES : auto_retry;
+  client->first_message = GNUNET_NO;
   th->notify = notify;
   th->notify_cls = notify_cls;
   th->attempts_left = MAX_ATTEMPTS;