1ivykis(3)                 ivykis programmer's manual                 ivykis(3)
2
3
4

NAME

6       iv_examples - ivykis examples
7

EXAMPLE

9       ivykis  is  initialised  by  calling  iv_init(3).  This function is the
10       first function to call when dealing with ivykis -- it has to be  called
11       before registering file descriptors or timers.
12
13       The  ivykis  main  event  loop  is started by calling iv_main(3).  This
14       function generally does not return, except when  iv_quit(3)  is  called
15       somewhere during execution of the program.
16
17       An  application  asks  ivykis  to  monitor a certain file descriptor by
18       filling out a structure of type 'struct iv_fd' with a  file  descriptor
19       number  and  a callback function, and calling the function iv_fd_regis‐
20       ter.
21
22       The first example program waits  for  data  from  standard  input,  and
23       writes a message to standard out whenever something is received:
24
25       #include <stdio.h>
26       #include <stdlib.h>
27       #include <iv.h>
28
29       struct iv_fd fd_stdin;
30
31       static void callback(void *dummy)
32       {
33               char buf[1024];
34               int len;
35
36               len = read(fd_stdin.fd, buf, sizeof(buf));
37               if (len <= 0) {
38                       if (len < 0) {
39                               if (errno == EAGAIN)
40                                       return;
41                               perror("read");
42                       }
43                       exit(1);
44               }
45
46               printf("read %d bytes of data from stdin\n", len);
47       }
48
49       int main()
50       {
51               iv_init();
52
53               IV_FD_INIT(&fd_stdin);
54               fd_stdin.fd = 0;
55               fd_stdin.handler_in = callback;
56               iv_fd_register(&fd_stdin);
57
58               iv_main();
59
60               iv_deinit();
61
62               return 0;
63       }
64
65       The application is responsible for memory management of 'struct iv_fd's
66       passed to ivykis.  For example, it should not free memory that contains
67       such structures that are still registered with ivykis (i.e. haven't had
68       iv_fd_unregister called on them).
69
70       iv_fd_register transparently sets the passed file  descriptor  to  non‐
71       blocking mode, in anticipation of its future usage.
72
73       File  descriptor  callbacks  are  called  in a level-triggered fashion.
74       Therefore, the way of dealing with fd_stdin  in  the  example  callback
75       function  is safe.  In case there arrives data between read and detect‐
76       ing EAGAIN, ivykis will re-call the callback function after it returns.
77       Also,  if  there  are more than 1024 bytes waiting in the input buffer,
78       ivykis will re-call the callback function until  all  data  from  stdin
79       have been drained.
80

EXAMPLE 2

82       The  second  example accepts connections on TCP port 6667, and waits on
83       each of the connections for data.  When data is received on any connec‐
84       tion, a message is prin