blocking_write.c 793 Bytes
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
/* blocking_write.c
 *
 */

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

static int do_write(struct pad_processor *closure,
		    struct simple_packet *packet)
{
  UINT32 left = packet->length;
  UINT8 *p = packet->data;

  while(left)
    {
      int written = write(closure->fd, p, left);

      if ( (written < 0)
	   && (errno == EINTR) )
	continue;

      if (written <= 0)
	return 0;

      left -= written;
      p += written;
    }

  simple_packet_free(packet);
  return 1;
}

struct packet_processor *make_blocking_write_procesor(int fd)
{
  struct blocking_write_processor *closure
    = xalloc(sizeof(struct blocking_write_processor_processor));

  closure->p->f = (raw_processor_function) do_write;
  closure->fd = fd;

  return (struct packet_processor *) closure;
}