@@ -4,29 +4,29 @@ import net
44import picohttpparser
55import time
66
7- // maximum size of the event queue
7+ // maximum size of the event queue.
88pub const max_queue = 4096
99
10- // event for incoming data ready to be read on a socket
10+ // event for incoming data ready to be read on a socket.
1111pub const picoev_read = 1
1212
13- // event for socket ready for writing
13+ // event for socket ready for writing.
1414pub const picoev_write = 2
1515
16- // event indicating a timeout has occurred
16+ // event indicating a timeout has occurred.
1717pub const picoev_timeout = 4
1818
19- // flag for adding a file descriptor to the event loop
19+ // flag for adding a file descriptor to the event loop.
2020pub const picoev_add = 0x40000000
2121
22- // flag for removing a file descriptor from the event loop
22+ // flag for removing a file descriptor from the event loop.
2323pub const picoev_del = 0x20000000
2424
25- // event read/write
25+ // event read/write.
2626pub const picoev_readwrite = 3
2727
2828// Target is a data representation of everything that needs to be associated with a single
29- // file descriptor (connection)
29+ // file descriptor (connection).
3030pub struct Target {
3131pub mut :
3232 fd int // file descriptor
@@ -37,7 +37,7 @@ pub mut:
3737 backend int
3838}
3939
40- // Config configures the Picoev instance with server settings and callbacks
40+ // Config configures the Picoev instance with server settings and callbacks.
4141pub struct Config {
4242pub :
4343 port int = 8080
8080 user_data voidptr = unsafe { nil }
8181}
8282
83- // init fills the `file_descriptors` array
83+ // init fills the `file_descriptors` array.
8484pub fn (mut pv Picoev) init () {
8585 // assert max_fds > 0
8686 pv.num_loops = 0
@@ -89,7 +89,7 @@ pub fn (mut pv Picoev) init() {
8989 }
9090}
9191
92- // add a file descriptor to the event loop
92+ // add a file descriptor to the event loop.
9393@[direct_array_access]
9494pub fn (mut pv Picoev) add (fd int , events int , timeout int , callback voidptr ) int {
9595 if pv == unsafe { nil } || fd < 0 || fd > = max_fds {
@@ -110,7 +110,7 @@ pub fn (mut pv Picoev) add(fd int, events int, timeout int, callback voidptr) in
110110 return 0
111111}
112112
113- // remove a file descriptor from the event loop
113+ // remove a file descriptor from the event loop.
114114@[direct_array_access]
115115pub fn (mut pv Picoev) delete (fd int ) int {
116116 if fd < 0 || fd > = max_fds {
@@ -146,7 +146,7 @@ fn (mut pv Picoev) loop_once(max_wait_in_sec int) int {
146146}
147147
148148// set_timeout sets the timeout in seconds for a file descriptor. If a timeout occurs
149- // the file descriptors target callback is called with a timeout event
149+ // the file descriptors target callback is called with a timeout event.
150150@[direct_array_access; inline]
151151fn (mut pv Picoev) set_timeout (fd int , secs int ) {
152152 assert fd < max_fds
@@ -159,7 +159,7 @@ fn (mut pv Picoev) set_timeout(fd int, secs int) {
159159
160160// handle_timeout loops over all file descriptors and removes them from the loop
161161// if they are timed out. Also the file descriptors target callback is called with a
162- // timeout event
162+ // timeout event.
163163@[direct_array_access; inline]
164164fn (mut pv Picoev) handle_timeout () {
165165 mut to_remove := []int {}
@@ -176,7 +176,7 @@ fn (mut pv Picoev) handle_timeout() {
176176 }
177177}
178178
179- // accept_callback accepts a new connection from `listen_fd` and adds it to the event loop
179+ // accept_callback accepts a new connection from `listen_fd` and adds it to the event loop.
180180fn accept_callback (listen_fd int , events int , cb_arg voidptr ) {
181181 mut pv := unsafe { & Picoev (cb_arg) }
182182 accepted_fd := accept (listen_fd)
@@ -204,7 +204,7 @@ fn accept_callback(listen_fd int, events int, cb_arg voidptr) {
204204 pv.add (accepted_fd, picoev_read, pv.timeout_secs, raw_callback)
205205}
206206
207- // close_conn closes the socket `fd` and removes it from the loop
207+ // close_conn closes the socket `fd` and removes it from the loop.
208208@[inline]
209209pub fn (mut pv Picoev) close_conn (fd int ) {
210210 if pv.delete (fd) != 0 {
@@ -213,7 +213,7 @@ pub fn (mut pv Picoev) close_conn(fd int) {
213213 close_socket (fd)
214214}
215215
216- // raw_callback handles raw events (read, write, timeout) for a file descriptor
216+ // raw_callback handles raw events (read, write, timeout) for a file descriptor.
217217@[direct_array_access]
218218fn raw_callback (fd int , events int , context voidptr ) {
219219 mut pv := unsafe { & Picoev (context) }
@@ -299,7 +299,7 @@ fn default_error_callback(data voidptr, req picohttpparser.Request, mut res pico
299299 res.end ()
300300}
301301
302- // new creates a `Picoev` struct and initializes the main loop
302+ // new creates a `Picoev` struct and initializes the main loop.
303303pub fn new (config Config) ! & Picoev {
304304 listening_socket_fd := listen (config) or {
305305 elog ('Error during listen: ${err} ' )
@@ -340,7 +340,7 @@ pub fn new(config Config) !&Picoev {
340340 return pv
341341}
342342
343- // serve starts the event loop for accepting new connections
343+ // serve starts the event loop for accepting new connections.
344344// See also picoev.new().
345345pub fn (mut pv Picoev) serve () {
346346 spawn update_date_string (mut pv)
@@ -349,7 +349,7 @@ pub fn (mut pv Picoev) serve() {
349349 }
350350}
351351
352- // update_date updates the date field of the Picoev instance every second for HTTP headers
352+ // update_date updates the date field of the Picoev instance every second for HTTP headers.
353353fn update_date_string (mut pv Picoev) {
354354 for {
355355 // get GMT (UTC) time for the HTTP Date header
0 commit comments