aboutsummaryrefslogtreecommitdiff
path: root/src/lib/eventloop.c
blob: bf212b2966ba3e2972fda33b47d28742ba863a2d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
/*
     This file is part of GNUnet.
     (C) 2010, 2011 Christian Grothoff (and other contributing authors)

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 2, or (at your
     option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
*/

/**
 * @file src/lib/eventloop.c
 * @brief code for merging GNUnet scheduler and Gtk Main Loop event loops
 * @author Christian Grothoff
 */
#include "gnunet_gtk.h"

#define DEBUG_EVENTLOOP GNUNET_NO

/**
 * Initial size of our poll array cache.
 *
 * TODO: get some statistics, find the maximum number of fds ever
 * polled during normal gnunet-gtk operation, and set this to that number.
 */
#define INITIAL_POLL_ARRAY_SIZE 30

/**
 * Main context for our event loop.
 */
struct GNUNET_GTK_MainLoop
{

  /**
   * Our configuration.
   */
  const struct GNUNET_CONFIGURATION_Handle *cfg;

  /**
   * Name of the glade file for the main window
   */
  const char *main_window_file;

  /**
   * Initial task to run to setup the system.
   */
  GNUNET_SCHEDULER_Task main_task;

  /**
   * Builder for the main window.
   */
  GtkBuilder *builder;

  /**
   * Gib's Main loop.
   */
  GMainLoop *gml;

  /**
   * GTK's main context.
   */
  GMainContext *gmc;

  /**
   * Read set.
   */
  struct GNUNET_NETWORK_FDSet *rs;

  /**
   * Write set.
   */
  struct GNUNET_NETWORK_FDSet *ws;

  /**
   * Recycled array of polling descriptors.
   */
  GPollFD *cached_poll_array;

  /**
   * Name of the configuration file.
   */
  char *cfgfile;

  /**
   * Size of the 'cached_poll_array'.
   */
  guint cached_poll_array_size;

  /**
   * Task we keep around just to keep the event loop running.
   */
  GNUNET_SCHEDULER_TaskIdentifier dummy_task;


#if WINDOWS
  /**
   * Array to hold pipe handles during a select() call
   */
  struct GNUNET_DISK_FileHandle **read_array;

  /**
   * Allocated length of read_array
   */
  int read_array_length;

  /**
   * Event to fire when a socket is ready for reading
   */
  HANDLE hEventRead;

  /**
   * Event to fire when a socket is ready for writing
   */
  HANDLE hEventWrite;

  /**
   * Event to fire when a socket had an error
   */
  HANDLE hEventException;

  /**
   * Event that is permanently enabled and is used to signal a pipe
   * that is ready for writing (asynchronous pipes are always writable)
   */
  HANDLE hEventPipeWrite;

  /**
   * Event that is permanently enabled and is used to signal a pipe
   * that is ready for reading (used to wake up early on a pipe that
   * is known to be readable)
   */
  HANDLE hEventReadReady;

  /**
   * A list to hold file handles that are ready for reading
   */
  struct GNUNET_CONTAINER_SList *handles_read;

  /**
   * A list to hold file handles that are ready for writing
   */
  struct GNUNET_CONTAINER_SList *handles_write;

  /**
   * A list to hold file handles that are broken
   */
  struct GNUNET_CONTAINER_SList *handles_except;
#endif

};



/**
 * Get the configuration.
 *
 * @param ml handle to the main loop
 * @return handle to the configuration, never NULL
 */
const struct GNUNET_CONFIGURATION_Handle *
GNUNET_GTK_main_loop_get_configuration (struct GNUNET_GTK_MainLoop *ml)
{
  return ml->cfg;
}


/**
 * Trigger shutdown of the GUI and exit the main loop.
 *
 * @param ml handle to the main loop
 */
void
GNUNET_GTK_main_loop_quit (struct GNUNET_GTK_MainLoop *ml)
{
  g_main_loop_quit (ml->gml);
  ml->gml = NULL;
  if (GNUNET_SCHEDULER_NO_TASK != ml->dummy_task)
  {
    GNUNET_SCHEDULER_cancel (ml->dummy_task);
    ml->dummy_task = GNUNET_SCHEDULER_NO_TASK;
  }
}


/**
 * Obtain the name of the configuration file that is being used.
 * 
 * @param ml handle to the main loop
 * @return name of configuration file
 */
const char * 
GNUNET_GTK_main_loop_get_configuration_file (struct GNUNET_GTK_MainLoop *ml)
{
  return ml->cfgfile;
}


/**
 * Get an object from the main window.
 *
 * @param ml handle to the main loop
 * @param name name of the object
 * @return NULL on error, otherwise the object
 */
GObject *
GNUNET_GTK_main_loop_get_object (struct GNUNET_GTK_MainLoop *ml,
                                 const char *name)
{
  return gtk_builder_get_object (ml->builder, name);
}


/**
 * Task to run Gtk events (within a GNUnet scheduler task).
 *
 * @param cls the main loop handle
 * @param tc scheduler context
 */
static void
dispatch_gtk_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  struct GNUNET_GTK_MainLoop *ml = cls;

  g_main_context_dispatch (ml->gmc);
}


/**
 * Change the size of the cached poll array to the given value.
 *
 * @param ml main loop context with the cached poll array
 * @param new_size desired size of the cached poll array
 */
static void
resize_cached_poll_array (struct GNUNET_GTK_MainLoop *ml, guint new_size)
{
  if (NULL == ml->cached_poll_array)
    ml->cached_poll_array = g_new (GPollFD, new_size);
  else
    ml->cached_poll_array = g_renew (GPollFD, ml->cached_poll_array, new_size);
  ml->cached_poll_array_size = new_size;
}


/**
 * Dummy task to keep our scheduler running.
 */
static void
keepalive_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  struct GNUNET_GTK_MainLoop *ml = cls;

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Dummy task was scheduled\n");
  ml->dummy_task =
      GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
                                    &keepalive_task, ml);
}


#ifndef FD_COPY
#define FD_COPY(s, d) (memcpy ((d), (s), sizeof (fd_set)))
#endif

/**
 * Replacement for the GNUnet scheduler's "select" that integrates the
 * Gtk event loop.  We merge Gtk's events with those from GNUnet's
 * scheduler and then use 'g_poll' on both.  Then we process the Gtk
 * events (by adding a task to do so to the GNUnet scheduler), and, if
 * applicable, return the GNUnet-scheduler events back to GNUnet.
 *
 * @param cls the 'struct GNUNET_GTK_MainLoop'
 * @param rfds set of sockets to be checked for readability
 * @param wfds set of sockets to be checked for writability
 * @param efds set of sockets to be checked for exceptions
 * @param timeout relative value when to return
 * @return number of selected sockets, GNUNET_SYSERR on error
 */
static int
gnunet_gtk_select (void *cls, struct GNUNET_NETWORK_FDSet *rfds,
                   struct GNUNET_NETWORK_FDSet *wfds,
                   struct GNUNET_NETWORK_FDSet *efds,
                   const struct GNUNET_TIME_Relative timeout)
{
  struct GNUNET_GTK_MainLoop *ml = cls;
  int max_nfds;
  gint poll_result;
  gint delay;
  guint i;
  guint fd_counter;
  guint need_gfds;
  fd_set aread;
  fd_set awrite;
  fd_set aexcept;
  int result = 0;
  gint max_priority;

#if WINDOWS
  int pre_ret = 0;
  int sock_read = 0;
  int sock_write = 0;
  int sock_err = 0;
  int socks;
  int always_ready_write_fd = -1;
  int select_ret = 0;
  int read_handles = 0;
  DWORD waitstatus;
#endif

  if (TRUE != g_main_loop_is_running (ml->gml))
    return GNUNET_NETWORK_socket_select (rfds, wfds, efds, timeout);
  if (NULL != rfds)
    FD_COPY (&rfds->sds, &aread);
  else
    FD_ZERO (&aread);
  if (NULL != wfds)
    FD_COPY (&wfds->sds, &awrite);
  else
    FD_ZERO (&awrite);
  if (NULL != efds)
    FD_COPY (&efds->sds, &aexcept);
  else
    FD_ZERO (&aexcept);

#if WINDOWS
  ResetEvent (ml->hEventRead);
  ResetEvent (ml->hEventWrite);
  ResetEvent (ml->hEventException);
  GNUNET_CONTAINER_slist_clear (ml->handles_read);
  GNUNET_CONTAINER_slist_clear (ml->handles_write);
  GNUNET_CONTAINER_slist_clear (ml->handles_except);
#endif

  max_nfds = -1;
  if (rfds != NULL)
    max_nfds = GNUNET_MAX (max_nfds, rfds->nsds);
  if (wfds != NULL)
    max_nfds = GNUNET_MAX (max_nfds, wfds->nsds);
  if (efds != NULL)
    max_nfds = GNUNET_MAX (max_nfds, efds->nsds);

  ml->cached_poll_array_size = ml->cached_poll_array_size;
  if (ml->cached_poll_array_size == 0)
    resize_cached_poll_array (ml, INITIAL_POLL_ARRAY_SIZE);

#if !WINDOWS
  fd_counter = 0;
  for (i = 0; i < max_nfds; i++)
  {
    int isset[3];

    isset[0] = (rfds == NULL) ? 0 : FD_ISSET (i, &rfds->sds);
    isset[1] = (wfds == NULL) ? 0 : FD_ISSET (i, &wfds->sds);
    isset[2] = (efds == NULL) ? 0 : FD_ISSET (i, &efds->sds);
    if ((!isset[0]) && (!isset[1]) && (!isset[2]))
      continue;
    if (fd_counter >= ml->cached_poll_array_size)
      resize_cached_poll_array (ml, ml->cached_poll_array_size * 2);
    ml->cached_poll_array[fd_counter].fd = i;
    ml->cached_poll_array[fd_counter].events =
        (isset[0] ? G_IO_IN | G_IO_HUP | G_IO_ERR : 0) | (isset[1] ? G_IO_OUT |
                                                          G_IO_ERR : 0) |
        (isset[2] ? G_IO_ERR : 0);
    fd_counter++;
  }
#else
  /* FIXME: why while(1)  here!? */
  while (1)
  {
    struct GNUNET_CONTAINER_SList_Iterator t;

    fd_counter = 0;
    /* We might overshoot a little, but that won't hurt very much */
    int need_nfds =
        (rfds->sds.fd_count + rfds->sds.fd_count + rfds->sds.fd_count >
         0 ? 3 : 0) + (rfds ==
                       NULL ? 0 : GNUNET_CONTAINER_slist_count (rfds->
                                                                handles)) +
        (wfds == NULL ? 0 : 1) + 1;
    if (need_nfds >= ml->cached_poll_array_size)
      resize_cached_poll_array (ml, need_nfds * 2);
    /* Since there are also gmainloop's own fds, just need_nfds won't be
     * enough, so make it twice as long.
     */
    if (ml->read_array_length < GNUNET_CONTAINER_slist_count (rfds->handles))
    {
      ml->read_array =
          GNUNET_realloc (ml->read_array,
                          GNUNET_CONTAINER_slist_count (rfds->handles) *
                          sizeof (struct GNUNET_DISK_FileHandle *));
      ml->read_array_length = GNUNET_CONTAINER_slist_count (rfds->handles);
    }
    if (rfds != NULL)
    {
      for (t = GNUNET_CONTAINER_slist_begin (rfds->handles), i = 0;
           GNUNET_CONTAINER_slist_end (&t) != GNUNET_YES;
           GNUNET_CONTAINER_slist_next (&t), i += 1)
      {
        struct GNUNET_DISK_FileHandle *fh =
            GNUNET_CONTAINER_slist_get (&t, NULL);
        if (fh->type == GNUNET_PIPE)
        {
          if (!ReadFile (fh->h, NULL, 0, NULL, fh->oOverlapRead))
          {
            DWORD error_code = GetLastError ();

            if (error_code == ERROR_IO_PENDING)
            {
#if DEBUG_EVENTLOOP
              GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                          "Adding the pipe's 0x%x overlapped event to the array as %d\n",
                          fh->h, nhandles);
#endif
              ml->cached_poll_array[fd_counter].fd =
                  (intptr_t) fh->oOverlapRead->hEvent;
              /* On W32 .events makes no sense - g_poll will just OR its
               * contents into .revents when the .fd event fires.
               * So we'll use it in the way that suits us the best.
               */
              ml->cached_poll_array[fd_counter].events = G_IO_IN;
              fd_counter += 1;
              ml->read_array[read_handles] = fh;
              read_handles += 1;
            }
            else
            {
              ml->cached_poll_array[fd_counter].fd =
                  (intptr_t) ml->hEventReadReady;
              ml->cached_poll_array[fd_counter].events = G_IO_HUP;
              fd_counter += 1;
              ml->read_array[read_handles] = fh;
              read_handles += 1;
            }
          }
          else
          {
#if DEBUG_EVENTLOOP
            GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                        "Adding the read ready event to the array as %d\n",
                        nhandles);
#endif
            ml->cached_poll_array[fd_counter].fd =
                (intptr_t) ml->hEventReadReady;
            ml->cached_poll_array[fd_counter].events = G_IO_IN;
            fd_counter += 1;
            ml->read_array[read_handles] = fh;
            read_handles += 1;
          }
        }
        else
        {
          GNUNET_CONTAINER_slist_add (ml->handles_read,
                                      GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
                                      fh,
                                      sizeof (struct GNUNET_DISK_FileHandle));
          pre_ret++;
        }
      }
    }
    if ((wfds != NULL) && (GNUNET_CONTAINER_slist_count (wfds->handles) > 0))
    {
      ml->cached_poll_array[fd_counter].fd = (intptr_t) ml->hEventPipeWrite;
      ml->cached_poll_array[fd_counter].events = G_IO_OUT;
      always_ready_write_fd = fd_counter;
      fd_counter += 1;
    }
    if (efds != NULL)
    {
      for (t = GNUNET_CONTAINER_slist_begin (efds->handles), i = 0;
           GNUNET_CONTAINER_slist_end (&t) != GNUNET_YES;
           GNUNET_CONTAINER_slist_next (&t), i += 1)
      {
        struct GNUNET_DISK_FileHandle *fh =
            GNUNET_CONTAINER_slist_get (&t, NULL);

        DWORD dwBytes;

        if (fh->type == GNUNET_PIPE)
        {
          if (!PeekNamedPipe (fh->h, NULL, 0, NULL, &dwBytes, NULL))
          {
            GNUNET_CONTAINER_slist_add (ml->handles_except,
                                        GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
                                        fh,
                                        sizeof (struct GNUNET_DISK_FileHandle));
            pre_ret++;
          }
        }
      }
    }
    GNUNET_CONTAINER_slist_iter_destroy (&t);

    if ((rfds != NULL) && (rfds->sds.fd_count > 0))
    {
#if DEBUG_EVENTLOOP
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "Adding the socket read event to the array as %d\n",
                  fd_counter);
#endif
      ml->cached_poll_array[fd_counter].fd = (intptr_t) ml->hEventRead;
      ml->cached_poll_array[fd_counter].events = G_IO_IN;
      for (i = 0; i < rfds->sds.fd_count; i++)
        WSAEventSelect (rfds->sds.fd_array[i], ml->hEventRead,
                        FD_ACCEPT | FD_READ | FD_CLOSE);
      fd_counter += 1;
      sock_read = rfds->sds.fd_count;
    }
    if ((wfds != NULL) && (wfds->sds.fd_count > 0))
    {
      int wakeup = 0;

#if DEBUG_EVENTLOOP
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "Adding the socket write event to the array as %d\n",
                  fd_counter);
#endif
      ml->cached_poll_array[fd_counter].fd = (intptr_t) ml->hEventWrite;
      ml->cached_poll_array[fd_counter].events = G_IO_OUT;
      for (i = 0; i < wfds->sds.fd_count; i++)
      {
        DWORD error;
        int status;

        status = send (wfds->sds.fd_array[i], NULL, 0, 0);
        error = GetLastError ();
#if DEBUG_EVENTLOOP
        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                    "pre-send to the socket %d returned %d (%u)\n", i, status,
                    error);
#endif
        if (status == 0 || (error != WSAEWOULDBLOCK && error != WSAENOTCONN))
          wakeup = 1;
        WSAEventSelect (wfds->sds.fd_array[i], ml->hEventWrite,
                        FD_WRITE | FD_CONNECT | FD_CLOSE);
      }
      if (wakeup)
        SetEvent (ml->hEventWrite);
      fd_counter += 1;
      sock_write = wfds->sds.fd_count;
    }
    if ((efds != NULL) && (efds->sds.fd_count > 0))
    {
#if DEBUG_EVENTLOOP
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "Adding the socket error event to the array as %d\n",
                  fd_counter);
#endif
      ml->cached_poll_array[fd_counter].fd = (intptr_t) ml->hEventException;
      ml->cached_poll_array[fd_counter].events = G_IO_ERR;
      for (i = 0; i < efds->sds.fd_count; i++)
        WSAEventSelect (efds->sds.fd_array[i], ml->hEventException,
                        FD_OOB | FD_CLOSE);
      fd_counter++;
      sock_err = efds->sds.fd_count;
    }
    break;
  }
  socks = sock_read + sock_write + sock_err;
#endif

  /* combine with Gtk events */
  if (NULL != ml->gmc)
  {
    g_main_context_prepare (ml->gmc, &max_priority);
    while (1)
    {
      need_gfds =
          g_main_context_query (ml->gmc, max_priority, &delay,
                                &ml->cached_poll_array[fd_counter],
                                ml->cached_poll_array_size - fd_counter);
      if (ml->cached_poll_array_size >= need_gfds + fd_counter)
        break;
      resize_cached_poll_array (ml, fd_counter + need_gfds);
    }
  }
  if (timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
  {
    if (delay >= 0)
      delay =
          GNUNET_MIN (timeout.rel_value /
                      GNUNET_TIME_UNIT_MILLISECONDS.rel_value, delay);
    else
      delay = timeout.rel_value / GNUNET_TIME_UNIT_MILLISECONDS.rel_value;
  }

#if WINDOWS
  if (pre_ret > 0)
  {
#if DEBUG_EVENTLOOP
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pre_ret is %d, setting delay to 0\n",
                pre_ret);
#endif
    delay = 0;
  }
#endif

#if DEBUG_EVENTLOOP
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "We have %d of our FDs and %d of GMC ones, going to wait %6dms\n",
              fd_counter, need_gfds, delay);
#endif

  poll_result = g_poll (ml->cached_poll_array, fd_counter + need_gfds, delay);
#if DEBUG_EVENTLOOP
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "g_poll returned : %d\n", poll_result);
#endif
  if (-1 == poll_result)
    return GNUNET_SYSERR;

  /* Take care of GUI events.
   * Dispatching the events here will eventually crash the scheduler, must do this
   * from within a task (currently we're not in a task, but in a select() call, remember)
   * Startup reason is used to pass the scheduler sanity check.
   */
  if (NULL != ml->gmc)
  {
    if (TRUE ==
        g_main_context_check (ml->gmc, max_priority,
                              &ml->cached_poll_array[fd_counter], need_gfds))
      GNUNET_SCHEDULER_add_continuation (&dispatch_gtk_task, ml,
                                         GNUNET_SCHEDULER_REASON_STARTUP);
  }
  /* Now map back GNUnet scheduler events ... */
#if !WINDOWS
  if (NULL != rfds)
    GNUNET_NETWORK_fdset_zero (rfds);
  if (NULL != wfds)
    GNUNET_NETWORK_fdset_zero (wfds);
  if (NULL != efds)
    GNUNET_NETWORK_fdset_zero (efds);
  for (i = 0; i < fd_counter; i++)
  {
    int set = 0;

    if ((NULL != rfds) &&
        (set |=
         (FD_ISSET (ml->cached_poll_array[i].fd, &aread) &&
          (0 !=
           (ml->cached_poll_array[i].
            revents & (G_IO_IN | G_IO_HUP | G_IO_ERR))))))
      GNUNET_NETWORK_fdset_set_native (rfds, ml->cached_poll_array[i].fd);
    if ((NULL != wfds) &&
        (set |=
         (FD_ISSET (ml->cached_poll_array[i].fd, &awrite) &&
          (0 != (ml->cached_poll_array[i].revents & (G_IO_OUT | G_IO_ERR))))))
      GNUNET_NETWORK_fdset_set_native (wfds, ml->cached_poll_array[i].fd);
    if ((NULL != efds) &&
        (set |=
         (FD_ISSET (ml->cached_poll_array[i].fd, &aexcept) &&
          (0 != (ml->cached_poll_array[i].revents & G_IO_ERR)))))
      GNUNET_NETWORK_fdset_set_native (efds, ml->cached_poll_array[i].fd);
    if (set)
      result++;
  }
#else
  if (socks > 0)
  {
    struct timeval tvslice;

    tvslice.tv_sec = 0;
    tvslice.tv_usec = 0;
    select_ret = select (max_nfds, &aread, &awrite, &aexcept, &tvslice);
    if (select_ret == -1)
      select_ret = 0;
#if DEBUG_EVENTLOOP
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "select() returned %d\n", select_ret);
#endif
  }
  if (always_ready_write_fd >= 0 &&
      ml->cached_poll_array[always_ready_write_fd].revents & G_IO_OUT)
  {
    GNUNET_CONTAINER_slist_append (ml->handles_write, wfds->handles);
    result += GNUNET_CONTAINER_slist_count (ml->handles_write);
#if DEBUG_EVENTLOOP
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added write pipe\n");
#endif
  }
  for (i = 0; i < read_handles; i++)
  {
    DWORD error;
    BOOL bret;

    if (!(ml->cached_poll_array[i].revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)))
      continue;
    SetLastError (0);
    waitstatus = 0;
    bret =
        PeekNamedPipe (ml->read_array[i]->h, NULL, 0, NULL, &waitstatus, NULL);
    error = GetLastError ();
#if DEBUG_EVENTLOOP
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "Peek at read pipe %d (0x%x) returned %d (%d bytes available) GLE %u\n",
                i, ml->read_array[i]->h, bret, waitstatus, error);
#endif
    if (bret == 0 || (ml->cached_poll_array[i].revents & G_IO_ERR))
    {
      if (efds != NULL)
      {
        struct GNUNET_CONTAINER_SList_Iterator t;

        for (t = GNUNET_CONTAINER_slist_begin (efds->handles), i = 0;
             GNUNET_CONTAINER_slist_end (&t) != GNUNET_YES;
             GNUNET_CONTAINER_slist_next (&t), i += 1)
        {
          struct GNUNET_DISK_FileHandle *fh =
              GNUNET_CONTAINER_slist_get (&t, NULL);
          if (fh == ml->read_array[i])
          {
            GNUNET_CONTAINER_slist_add (ml->handles_except,
                                        GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
                                        fh,
                                        sizeof (struct GNUNET_DISK_FileHandle));
            break;
          }
        }
        GNUNET_CONTAINER_slist_iter_destroy (&t);
      }
    }
    else if (waitstatus <= 0)
      continue;
    GNUNET_CONTAINER_slist_add (ml->handles_read,
                                GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
                                ml->read_array[i],
                                sizeof (struct GNUNET_DISK_FileHandle));
    result += 1;
#if DEBUG_EVENTLOOP
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added read Pipe 0x%x (0x%x)\n",
                ml->read_array[i], ml->read_array[i]->h);
#endif
  }
  waitstatus = WaitForSingleObject (ml->hEventWrite, 0);
#if DEBUG_EVENTLOOP
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Wait for the write event returned %d\n",
              waitstatus);
#endif
  if (waitstatus == WAIT_OBJECT_0)
  {
    for (i = 0; i < wfds->sds.fd_count; i++)
    {
      DWORD error;
      int status;
      int so_error = 0;
      int sizeof_so_error = sizeof (so_error);
      int gso_result = getsockopt (wfds->sds.fd_array[i],
                                   SOL_SOCKET, SO_ERROR,
                                   (char *) &so_error, &sizeof_so_error);

      status = send (wfds->sds.fd_array[i], NULL, 0, 0);
      error = GetLastError ();
#if DEBUG_EVENTLOOP
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "send to the socket %d returned %d (%u)\n", i, status, error);
#endif
      if (status == 0 || (error != WSAEWOULDBLOCK && error != WSAENOTCONN) ||
          (status == -1 && gso_result == 0 && error == WSAENOTCONN &&
           so_error == WSAECONNREFUSED))
      {
        FD_SET (wfds->sds.fd_array[i], &awrite);
        result += 1;
      }
    }
  }
  if (rfds)
  {
    struct GNUNET_CONTAINER_SList_Iterator t;

    for (i = 0; i < rfds->sds.fd_count; i++)
      WSAEventSelect (rfds->sds.fd_array[i], ml->hEventRead, 0);
    for (t = GNUNET_CONTAINER_slist_begin (rfds->handles);
         GNUNET_CONTAINER_slist_end (&t) != GNUNET_YES;
         GNUNET_CONTAINER_slist_next (&t))
    {
      struct GNUNET_DISK_FileHandle *fh = GNUNET_CONTAINER_slist_get (&t, NULL);

      if (fh->type == GNUNET_PIPE)
        CancelIo (fh->h);
    }
    GNUNET_CONTAINER_slist_iter_destroy (&t);
    GNUNET_NETWORK_fdset_zero (rfds);
    if (select_ret != -1 && socks > 0)
      GNUNET_NETWORK_fdset_copy_native (rfds, &aread, select_ret);
    GNUNET_CONTAINER_slist_append (rfds->handles, ml->handles_read);
  }
  if (wfds)
  {
    for (i = 0; i < wfds->sds.fd_count; i++)
      WSAEventSelect (wfds->sds.fd_array[i], ml->hEventWrite, 0);
    GNUNET_NETWORK_fdset_zero (wfds);
    if (select_ret != -1 && socks > 0)
      GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, select_ret);
    GNUNET_CONTAINER_slist_append (wfds->handles, ml->handles_write);
  }
  if (efds)
  {
    for (i = 0; i < efds->sds.fd_count; i++)
      WSAEventSelect (efds->sds.fd_array[i], ml->hEventException, 0);
    GNUNET_NETWORK_fdset_zero (efds);
    if (select_ret != -1 && socks > 0)
      GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, select_ret);
    GNUNET_CONTAINER_slist_append (efds->handles, ml->handles_except);
    result += GNUNET_CONTAINER_slist_count (ml->handles_except);
  }
  if (fd_counter > 0)
    /* This is not accurate (select_ret counts write-ready sockets,
     * and result does as well. Anything out there actually cares
     * about this?
     */
    result = select_ret + result;
  else
    result = 0;
#endif
  return result;
}


/**
 * Actual main function run right after GNUnet's scheduler
 * is initialized.  Initializes up GTK and Glade and starts the
 * combined event loop.
 *
 * @param cls the 'struct GNUNET_GTK_MainLoop'
 * @param args leftover command line arguments (go to gtk)
 * @param cfgfile name of the configuration file
 * @param cfg handle to the configuration
 */
static void
run_main_loop (void *cls, char *const *args, const char *cfgfile,
	       const struct GNUNET_CONFIGURATION_Handle *cfg)
{
  struct GNUNET_GTK_MainLoop *ml = cls;
  int argc;

  /* command-line processing for Gtk arguments */
  argc = 0;
  while (args[argc] != NULL)
    argc++;
  gtk_init (&argc, (char ***) &args);

  /* setup main context */
  ml->builder = GNUNET_GTK_get_new_builder (ml->main_window_file);
  if (ml->builder == NULL)
    return;
  ml->rs = GNUNET_NETWORK_fdset_create ();
  ml->ws = GNUNET_NETWORK_fdset_create ();
  ml->gml = g_main_loop_new (NULL, TRUE);
  ml->gmc = g_main_loop_get_context (ml->gml);
  ml->cfg = cfg;
  ml->cfgfile = GNUNET_strdup (cfgfile);
#if WINDOWS
  ml->hEventRead = CreateEvent (NULL, TRUE, FALSE, NULL);
  ml->hEventReadReady = CreateEvent (NULL, TRUE, TRUE, NULL);
  ml->hEventWrite = CreateEvent (NULL, TRUE, FALSE, NULL);
  ml->hEventException = CreateEvent (NULL, TRUE, FALSE, NULL);
  ml->hEventPipeWrite = CreateEvent (NULL, TRUE, TRUE, NULL);
  ml->handles_read = GNUNET_CONTAINER_slist_create ();
  ml->handles_write = GNUNET_CONTAINER_slist_create ();
  ml->handles_except = GNUNET_CONTAINER_slist_create ();
  ml->read_array = NULL;
  ml->read_array_length = 0;
#endif

  /* run main task of the application */
  GNUNET_SCHEDULER_add_continuation (ml->main_task, ml,
                                     GNUNET_SCHEDULER_REASON_STARTUP);

  /* start the Gtk event loop */
  GNUNET_assert (TRUE == g_main_context_acquire (ml->gmc));
  GNUNET_SCHEDULER_set_select (&gnunet_gtk_select, ml);

  /* keep Gtk event loop running even if there are no GNUnet tasks */
  ml->dummy_task =
      GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
                                    &keepalive_task, ml);

}


/**
 * Initialize the main loop.
 *
 * @param binary_name binary name
 * @param binary_help help text for the binary
 * @param argc number of command line options
 * @param argv command line options
 * @param options allowed command line options
 * @param main_window_file glade file for the main window
 * @param main_task first task to run, closure will be set to the 'struct GNUNET_GTK_MainLoop'
 * @return GNUNET_OK on success, GNUNET_SYSERR on error (i.e. bad command-line options, etc)
 */
int
GNUNET_GTK_main_loop_start (const char *binary_name, const char *binary_help,
                            int argc, char *const *argv,
                            struct GNUNET_GETOPT_CommandLineOption *options,
                            const char *main_window_file,
                            GNUNET_SCHEDULER_Task main_task)
{
  struct GNUNET_GTK_MainLoop ml;
  int ret;

  memset (&ml, 0, sizeof (ml));
  ml.main_window_file = main_window_file;
  ml.main_task = main_task;
  ret =
    GNUNET_PROGRAM_run (argc, argv, binary_name, binary_help,
			options, &run_main_loop, &ml);
  if (NULL != ml.cached_poll_array)
    g_free (ml.cached_poll_array);
  if (NULL != ml.rs)
    GNUNET_NETWORK_fdset_destroy (ml.rs);
  if (NULL != ml.ws)
    GNUNET_NETWORK_fdset_destroy (ml.ws);
  if (NULL != ml.builder)
    g_object_unref (G_OBJECT (ml.builder));
  if (NULL != ml.gml)
    g_main_loop_unref (ml.gml);
  GNUNET_free_non_null (ml.cfgfile);
  return ret;
}


/* end of eventloop.c */