io.c 4.08 KB
Newer Older
Niels Möller's avatar
Niels Möller 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
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
/* io.c
 *
 */

#include "io.h"
#include <unistd.h>
#include <poll.h>
#include <errno.h>

#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>

/* A little more than an hour */
#define MAX_TIMEOUT 4000

struct fd_read
{
  struct abstract_read a;
  int fd;
};

static int do_read(struct fd_read *closure, UINT8 *buffer, UINT32 length)
{
  return read(closure->fd, buffer, length);
};

void io_run(struct io_backend *b)
{
  while(1)
    {
      struct pollfd *fds;
      int i;
      nfds_t nfds;
      int timeout;
      int res;
      
      nfds = b->ninput + b->noutput + b->nlisten + n->nconnect;

      if (b->callouts)
	{
	  time_t now = time();
	  if (now >= b->callout->when)
	    timeout = 0;
	  else
	    {
	      if (b->callout->when > now + MAX_TIMEOUT)
		timeout = MAX_TIMEOUT * 1000;
	      else
		timeout = (b->callout->when - now) * 1000;
	    }
	}
      else
	{
	  if (!nfds)
	    /* All done */
	    break;
	  timeout = -1;
	}
      
      fds = alloca(sizeof(struct pollfd) * nfds);

      /* Handle fds in order: read, accept, connect, write, */
      i = 0;
      {
	struct input_fd *fd = b->input;
	for( ; fd; fd = fd->next, i++)
	  {
	    fds[i]->fd = fd->hold_on ? -1 : fd->fd;
	    fds[i]->events = POLLIN;
	  }
      }
      {
	struct accept_fd *fd = b->accept;
	for( ; fd; fd = fd->next, i++)
	  {
	    fds[i]->fd = fd->fd;
	    fds[i]->events = POLLIN;
	  }
      }
      {
	struct connect_fd *fd = b->connect;
	for( ; fd; fd = fd->next, i++)
	  {
	    fds[i]->fd = fd->fd;
	    fds[i]->events = POLLOUT;
	  }
      }
      {
	struct output_fd *fd = b->output;
	for( ; fd; fd = fd->next, i++)
	  {
	    write_buffer_pre_write(fd->buffer);

	    fds[i]->fd = fd->buffer->empty ? -1 : fd->fd;
	    fds[i]->events = POLLOUT;
	  }
      }

      res = poll(fds, nfds, timeout);

      if (!res)
	{
	  /* Timeout. Run the callout */
	  if (!CALLBACK(b->callouts->callout);)
	    fatal("What now?");
	  b->callouts = b->callouts->next;
	}
      if (res<0)
	{
	  switch(errno)
	    {
	    case EAGAIN:
	    case EINTR:
	      continue;
	    default:
	      fatal("io_run:poll failed: %s", strerror(errno));
	    }
	}
      else
	{ /* Process files */
	  i = 0;
	  {
	    struct input_fd *fd = b->input;
	    for( ; fd; fd = fd->next, i++)
	      {
		if (fds[i]->revents & POLLIN)
		  {
		    struct fd_read r =
		    { { (abstract_read_f) do_read }, fd->fd };
		    
		    if (!fd->callback->handler(fd->callback, &r))
		      /* FIXME: Remove fd, or close, or? */
		      fatal("What now?");
		  }
	      }
	  
	    {
	      struct accept_fd *fd = b->accept;
	      for( ; fd; fd = fd->next, i++)
		{
		  if (fds[i]->revents & POLLIN)
		    if (!CALLBACK(fd->callback))
		      fatal("What now?");
		}
	    }
	    {
	      struct connect_fd *fd = b->connect;
	      for( ; fd; fd = fd->next, i++)
		{
		  if (fds[i]->revents & POLLOUT)
		    if (!CALLBACK(fd->callback))
		      fatal("What now?");
		}
	    }
	    {
	      struct output_fd *fd = b->output;
	      for( ; fd; fd = fd->next, i++)
		{
		  if (fds[i]->revents & POLLOUT)
		    {
		      UINT32 size = MIN(fd->buffer->end - fd->buffer->start,
					fd->buffer->block_size);
		      int res = write(fd->fd, fd->buffer->data + fd->buffer->start,
				      size);
		      if (!res)
			fatal("Closed?");
		      if (res < 0)
			switch(errno)
			  {
			  case EINTR:
			  case EAGAIN:
			    break;
			  default:
			    CALLBACK(fd->close_Callback);
			  }
		      else
			fd->buffer->start += res;
		    }
		}
	    }
	  }
	}
    }
}

void io_set_nonblocking(int fd)
{
  if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
    fatal("io_set_nonblocking: fcntl() failed, %s", strerror(errno));
}

int io_connect(struct sockaddr *sa, struct callback *f)
{
  struct connect_fd *file;
  int fd = socket(AF_INET, SOCK_STREAM, ...);
  
  if (fd<0)
    fatal("io_connect: socket() failed, %s", strerror(errno));

  io_set_nonblocking(fd);

  if (connect(fd, sa, ...) < 0)
      ...;
      
  file = xalloc(sizeof(struct connect_fd));
  info->next = ...;
  info->fd = fd;

  /* FIXME: The fd must somehow be passed to the callback. */
  info->callback = callback;

  return info;
}

int io_listen()