isc_wait.c 3.47 KB
Newer Older
Per Cederqvist's avatar
Per Cederqvist committed
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
/*
** isc_wait.c				wait for connects to succeed
**
** Copyright (c) 1996 Peter Eriksson and Per Cederqvist of the
**                    Lysator Academic Computer Association.
**
** history:
**    960511 ceder     initial coding.
*/

#ifdef TIME_WITH_SYS_TIME
#  include <time.h>
#endif
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <errno.h>
#ifdef HAVE_STDDEF_H
#  include <stddef.h>
#endif

#include "isc.h"
#include "intern.h"

/* This return NULL if there is no connecting session,
   or an event of type ISC_EVENT_ERROR, ISC_EVENT_TIMEOUT,
   ISC_EVENT_REJECTED or ISC_EVENT_CONNECTED.
   */
IscEvent *
isc_wait(IscMaster *mcb, long timeout)
{
  struct timeval    wait;
  fd_set            write_set;
  IscEvent        * event = NULL;
  IscSessionList  * isl;
  IscSession      * scb;
  IscAddress      * ia;
  int               nfds;
  
  /* Set up the list of file descriptors to wait on */
  FD_ZERO(&write_set);
  nfds = -1;

  /*
   * Go thru the list of sessions, add to read_set and handle LOGOUT
   */
  for (isl = mcb->sessions; isl != NULL; isl = isl->next)
  {
    scb = isl->scb;

    /*
     * Add to the write set if in state CONNECTING.
     */
    if (scb->state == ISC_STATE_CONNECTING)
    {
      FD_SET(scb->fd, &write_set);
      if (nfds <= scb->fd)
	nfds = scb->fd + 1;
    }

    /*
     * Terminate at end of circular ring
     */
    if (isl->next == mcb->sessions)
      break;
  }

  /*
   * Return immediately if there are no CONNECTING sessions.
   */
  if (nfds == -1)
  {
    return NULL;
  }

  /* Allocate an event */
  ISC_XNEW(event);
  event->msg     = NULL;
  event->session = NULL;

  /*
   * Set up the timeout structure.
   */
  wait.tv_sec  = timeout / 1000;
  wait.tv_usec = (timeout % 1000L) * 1000L;

  /* Wait for event */
  nfds = select(FD_SETSIZE, NULL, &write_set, NULL, &wait);

  /* Error in select? */
  if (nfds < 0)
  {
    event->event = ISC_EVENT_ERROR;
    event->msg   = isc_mkstrmsg("Error in select()");
    return event;
  }
  
  /* Timeout? */
  if (nfds == 0)
  {
    event->event = ISC_EVENT_TIMEOUT;
    return event;
  }
  
  for (isl = mcb->sessions; isl != NULL; isl = isl->next)
  {
    scb = isl->scb;

    
    if (FD_ISSET(scb->fd, &write_set))
      switch (scb->state)
      {
	case ISC_STATE_CONNECTING:
	  ia = isc_getraddress(scb);
	  if (ia == NULL)
	  {
	    /*
	     * Connect request failed
	     */
	    scb->state = ISC_STATE_CLOSING;
	    scb->isc_errno = errno;
	    event->event = ISC_EVENT_REJECTED;
	    event->session = scb;
	  }
	  else
	  {
	    /*
	     * Connect request acknowledged
	     */
	    isc_free(ia);
	    scb->state = ISC_STATE_RUNNING;
	    event->event = ISC_EVENT_CONNECTED;
	    event->session = scb;
	  }
	  
	  /* Move the session pointer to the next in the circular queue */
	  mcb->sessions = mcb->sessions->next;
      
	  return event;

	default:
	  /* Major fuck-up somewhere, this should not happen... */
	  event->event   = ISC_EVENT_ERROR;
	  event->session = scb;
	  event->msg     = isc_mkstrmsg(
		       "Should not happen; illegal state for write FD_SET");
	  
	  /* Move the session pointer to the next in the circular queue */
	  mcb->sessions = mcb->sessions->next;
	  
	  return event;
      }
    
    if (isl->next == mcb->sessions)
      break;
  }
  
  /* Major fuck-up somewhere, something else should have happened... */
  event->event   = ISC_EVENT_ERROR;
  event->session = NULL;
  event->msg     = isc_mkstrmsg(
	"Should not happen; nothing happend in isc_wait");
  return event;
}