diff --git a/cmd/tools/modules/testing/common.v b/cmd/tools/modules/testing/common.v index ede787d7078fb8..782d4337340c43 100644 --- a/cmd/tools/modules/testing/common.v +++ b/cmd/tools/modules/testing/common.v @@ -209,6 +209,7 @@ pub fn new_test_session(_vargs string, will_compile bool) TestSession { $if solaris { skip_files << 'examples/gg/gg2.v' skip_files << 'examples/pico/pico.v' + skip_files << 'examples/pico/raw_callback.v' skip_files << 'examples/sokol/fonts.v' skip_files << 'examples/sokol/drawing.v' } diff --git a/examples/pico/raw_callback.v b/examples/pico/raw_callback.v new file mode 100644 index 00000000000000..8794f8d2efe4b7 --- /dev/null +++ b/examples/pico/raw_callback.v @@ -0,0 +1,39 @@ +module main + +import net +import picoev + +const ( + port = 8080 + http_response = 'HTTP/1.1 200 OK\r\nContent-type: text/html\r\nContent-length: 18\r\n\r\nHello from Picoev!' +) + +fn main() { + println('Starting webserver on http://localhost:${port}/ ...') + mut pico := picoev.new( + port: port + raw_cb: handle_conn + ) + pico.serve() +} + +fn handle_conn(mut pv picoev.Picoev, fd int) { + // setup a nonblocking tcp connection + mut conn := &net.TcpConn{ + sock: net.tcp_socket_from_handle_raw(fd) + handle: fd + is_blocking: false + } + + mut buf := []u8{len: 4096} + // read data from the tcp connection + conn.read(mut buf) or { eprintln('could not read data from socket') } + + println('received data:') + println(buf.bytestr()) + + conn.write(http_response.bytes()) or { eprintln('could not write response') } + + // remove the socket from picoev's event loop and close the connection + pv.close_conn(fd) +} diff --git a/vlib/picoev/picoev.v b/vlib/picoev/picoev.v index 4c111126eee64d..dfab5ddcb22a2b 100644 --- a/vlib/picoev/picoev.v +++ b/vlib/picoev/picoev.v @@ -33,7 +33,7 @@ pub: port int = 8080 cb fn (voidptr, picohttpparser.Request, mut picohttpparser.Response) = unsafe { nil } err_cb fn (voidptr, picohttpparser.Request, mut picohttpparser.Response, IError) = default_err_cb - raw_cb fn (voidptr, int) = unsafe { nil } + raw_cb fn (mut Picoev, int) = unsafe { nil } user_data voidptr = unsafe { nil } timeout_secs int = 8 max_headers int = 100 @@ -43,10 +43,9 @@ pub: [heap] pub struct Picoev { - cb fn (voidptr, picohttpparser.Request, mut picohttpparser.Response) = unsafe { nil } - err_cb fn (voidptr, picohttpparser.Request, mut picohttpparser.Response, IError) = default_err_cb - raw_cb fn (voidptr, int) = unsafe { nil } - user_data voidptr = unsafe { nil } + cb fn (voidptr, picohttpparser.Request, mut picohttpparser.Response) = unsafe { nil } + err_cb fn (voidptr, picohttpparser.Request, mut picohttpparser.Response, IError) = default_err_cb + raw_cb fn (mut Picoev, int) = unsafe { nil } timeout_secs int max_headers int = 100 @@ -63,6 +62,8 @@ mut: out &u8 = unsafe { nil } date string +pub: + user_data voidptr = unsafe { nil } } // init fills the `file_descriptors` array @@ -213,7 +214,7 @@ fn raw_callback(fd int, events int, context voidptr) { } else if events & picoev.picoev_read != 0 { pv.set_timeout(fd, pv.timeout_secs) if !isnil(pv.raw_cb) { - pv.raw_cb(pv.user_data, fd) + pv.raw_cb(mut pv, fd) return }