]> jfr.im git - irc/rqf/shadowircd.git/blame - doc/technical/network.txt
Remove additional wrong declaration for rb_kill().
[irc/rqf/shadowircd.git] / doc / technical / network.txt
CommitLineData
212380e3 1Overview of the network subsystem
2Adrian Chadd <adrian@creative.net.au>
3
4$Id: network.txt 6 2005-09-10 01:02:21Z nenolod $
5
6
7This document is an overview of the new and hopefully improved network
8subsystem.
9
10The code is based loosely upon the network core found in the Squid web cache
11server, with some optimizations for ircd-specific IO patterns.
12
13
14
15Filedescriptor IO
16-----------------
17
18Filedescriptor IO is initiated using comm_setselect(). comm_setselect()
19registers interest in reading from or writing to a file descriptor.
20When a filedescriptor is ready for the required IO a callback is called
21from the IO loop.
22
23The comm_setselect() usage is:
24
25void
26comm_setselect(int fd, fdlist_t list, int type, PF *callback, void *cbdata,
27 int timeout)
28
29where:
30 fd filedescriptor
31 list Which list the FD should be put on
32 type IO type. Can currently include:
33 COMM_SELECT_READ - register for read
34 COMM_SELECT_WRITE - register for write
35 callback Function to call when the FD is ready
36 cbdata Data to be passed to above function
37 timeout Update the timeout value. 0 is "don't update".
38
39
40A typical use is:
41
42..
43
44/* Register interest in the FD for a read event */
45comm_setselect(fd, FDLIST_SERVICE, COMM_SELECT_READ, read_callback, read_data,
46 0);
47
48..
49
50(FD becomes ready for read in the IO loop)
51
52void
53read_callback(int fd, void *data)
54{
55 /* called when the FD becomes ready for read */
56 retval = read(fd, buf, len);
57
58 ..
59 /* Ok, we need to read some more when its ready */
60 comm_setselect(fd, FDLIST_SERVICE, COMM_SELECT_READ, read_callback, data,
61 0);
62}
63
64
65
66
67Socket timeouts
68---------------
69
70A "socket timeout" is a callback registered to be called when a certain
71amount of time has elapsed. Think of it as an event, but against a FD.
72
73A good example of socket timeouts is in the comm_connect_tcp() code.
74When the connect() begins, comm_settimeout() is called to call
75comm_connect_timeout() if the timeout occurs. Once the connect() completes,
76comm_settimeout() is called with a timeout of 0 and callback of NULL
77to deregister the timeout. If the timeout occurs, comm_connect_timeout()
78is called and the connection attempt is aborted.
79
80
81
82
83Functions
84---------
85
86comm_open() - a socket() wrapper, enforcing fd limitations and tagging the
87 file descriptor with a note
88
89comm_accept() - an accept() wrapper, enforcing fd limitations and tagging
90 the file descriptor with a note
91
92comm_connect_tcp() - attempt an async connect(). Handles DNS lookups if
93 required, and will call the given callback at completion or error
94
95comm_settimeout() - set a callback to be called after a given time period.
96 This is good to implement things like PING checks and connect() timeouts.
97
98Notes:
99
100* All socket creation should go through comm_open() / comm_accept().
101* All socket closing should go through fd_close(). comm_close() isn't
102 implemented yet.
103* comm_connect_tcp() is your best friend. :-)
104* *ALL* network sockets should be non-blocking. If your OS doesn't support
105 non-blocking sockets, you shouldn't be here.