]> jfr.im git - irc/quakenet/snircd.git/blame - doc/api/msgq.txt
sync undernet upstream ircu changes.
[irc/quakenet/snircd.git] / doc / api / msgq.txt
CommitLineData
189935b1 1Many messages generated by an IRC server are sent to multiple
2recipients. Previous versions of ircd used DBuf to store these
3messages until they could actually be sent. The problem with using a
4DBuf for this, though, is that there are multiple copies of the same
5message hanging around. Another problem is that there is at least one
6strcpy() or equivalent call for each destination the message is sent
7to. A simple solution to this problem is to use messages queues.
8This file documents the MsgQ interface for ircd.
9
10The MsgQ interface is loosely based on the API for DBuf. Although the
11structures are vastly different, most calls, including several of the
12macros, are similar to certain pieces of the DBuf API. This made
13retrofitting ircd with MsgQ support much simpler.
14
15<struct>
16struct MsgCounts {
17 int alloc;
18 int used;
19};
20
21The MsgCounts structure may be used for determining how much memory is
22in use by the MsgQ system. The _alloc_ element is a count of the
23total number of structures (of whatever type) that have been
24allocated; the _used_ element is a count of how many are actually in
25use. MsgQ never releases any of its allocated memory; instead, it
26places unused structures onto a free list.
27</struct>
28
29<struct>
30struct MsgBuf;
31
32The MsgBuf structure contains the actual message, along with a
33reference count and the message's length. None of its fields are
34directly accessible by the application.
35</struct>
36
37<struct>
38struct MsgQ;
39
40The MsgQ structure is a structure allocated by the application that is
41used by the MsgQ system to describe an entire message queue, including
42both normal and priority queues. None of its fields are directly
43accessible by the application.
44</struct>
45
46<global>
47struct MsgCounts msgBufCounts; /* resource count for struct MsgBuf */
48
49This global variable counts the number of MsgBuf structures that have
50been allocated. This may be used to determine how much memory is in
51use by the MsgQ system.
52</global>
53
54<global>
55struct MsgCounts msgCounts; /* resource count for struct Msg */
56
57This global variable counts the number of Msg structures that have
58been allocated. The Msg structure describes the link between a queue
59and a message. It is not accessible to the application, and so not
60further documented here.
61</global>
62
63<function>
64unsigned int MsgQLength(struct MsgQ* mq);
65
66This macro returns the number of bytes in a particular user's message
67queue.
68</function>
69
70<function>
71unsigned int MsgQCount(struct MsgQ* mq);
72
73This macro returns the number of messages in a particular user's
74message queue.
75</function>
76
77<function>
78void MsgQClear(struct MsgQ* mq);
79
80This macro simply clears the content of a particular message queue.
81NOTE: This macro evaluates its argument twice.
82</function>
83
84<function>
85void msgq_init(struct MsgQ *mq);
86
87This function initializes a caller-allocated message queue to be
88empty. Calling this function on a message queue with messages in it
89WILL RESULT IN A MEMORY LEAK.
90</function>
91
92<function>
93void msgq_delete(struct MsgQ *mq, unsigned int length);
94
95This function removes the given number of bytes from the message
96queue. If entire messages have been sent, they will be unlinked from
97the queue. The _length_ parameter does not need to correspond to a
98given message's length; the MsgQ system is able to deal with messages
99that have only partially been sent.
100</function>
101
102<function>
103int msgq_mapiov(const struct MsgQ *mq, struct iovec *iov, int count,
104 unsigned int *len);
105
106The msgq_mapiov() function takes a struct MsgQ (specified by the _mq_
107parameter) and a caller allocated struct iovec array (specified by the
108_iov_ parameter) and maps the contents of the message into the struct
109iovec array. The _count_ parameter must indicate the total number of
110elements available for msgq_mapiov() to use. The _len_ parameter must
111be a pointer to an unsigned int, and upon return from the function
112will contain the total number of bytes that have been mapped into the
113struct iovec array. This function returns the number of struct iovec
114elements that have been filled. For more information about the
115purpose of struct iovec, see your system's man page for the writev()
116function.
117</function>
118
119<function>
120struct MsgBuf *msgq_make(struct Client *dest, const char *format, ...);
121
122This function allocates a struct MsgBuf and calls ircd_vsnprintf()
123with the _dest_ and _format_ parameters to fill it in. Most callers
124should use the send_buffer() function (declared in send.h) to attach
125the struct MsgBuf to a client's message queue.
126</function>
127
128<function>
129struct MsgBuf *msgq_vmake(struct Client *dest, const char *format, va_list vl);
130
131This function is identical to msgq_make() except that it takes a
132va_list (given by the _vl_ parameter) and calls ircd_vsnprintf() to
133format the message.
134</function>
135
136<function>
137void msgq_append(struct Client *dest, struct MsgBuf *mb, const char *format,
138 ...);
139
140Occasionally a caller is not able to completely compute a message
141before calling msgq_make(). When this happens, the msgq_append()
142function may be called to append more text onto the struct MsgBuf
143specified by the _mb_ parameter. As with msgq_make(), the _dest_ and
144_format_ parameters are passed to ircd_vsnprintf(), along with the
145additional arguments.
146</function>
147
148<function>
149void msgq_clean(struct MsgBuf *mb);
150
151As mentioned above, struct MsgBuf includes a reference count. When
152that reference count reaches zero, the structure is released. The
153reference count is set to 1 by msgq_make() and msgq_vmake(). Once a
154given message has been attached to all the queues it needs to be, the
155caller should call the msgq_clean() function to decrement this
156reference count. This function will place the struct MsgBuf back onto
157the free list if it did not get attached to any message queues. The
158msgq_delete() function calls msgq_clean() internally, so the
159application need not call msgq_clean() explicitly afterwards.
160</function>
161
162<function>
163void msgq_add(struct MsgQ *mq, struct MsgBuf *mb, int prio);
164
165This function is used to attach a given struct MsgBuf, as specified by
166the _mb_ parameter, to a given message queue. The _prio_ parameter,
167if non-zero, specifies that the message should be placed on the
168priority queue. This function is called by send_buffer(), defined in
169send.h; most applications should call that function, rather than this
170one.
171</function>
172
173<function>
174void msgq_count_memory(size_t *msg_alloc, size_t *msg_used,
175 size_t *msgbuf_alloc, size_t *msgbuf_used);
176
177This function simply takes the counts kept in msgBufCounts and
178msgCounts and multiplies them by the appropriate structure sizes,
179storing the resulting sizes into its parameters.
180</function>
181
182<function>
183unsigned int msgq_bufleft(struct MsgBuf *mb);
184
185This function is for use in conjunction with msgq_append(). It
186returns the total number of bytes of free storage in the given _mb_.
187</function>
188
189<authors>
190Kev <klmitch@mit.edu>
191</authors>
192
193<changelog>
194[2001-6-15 Kev] Initial documentation for the MsgQ functions.
195</changelog>