picoev: improve raw mode, change fn signature for the raw_cb field to fn (mut Picoev, int) (#19817)

This commit is contained in:
Casper Küthe 2023-11-11 08:36:52 +01:00 committed by GitHub
parent e7cad4f55d
commit a176021afe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 6 deletions

View file

@ -209,6 +209,7 @@ pub fn new_test_session(_vargs string, will_compile bool) TestSession {
$if solaris { $if solaris {
skip_files << 'examples/gg/gg2.v' skip_files << 'examples/gg/gg2.v'
skip_files << 'examples/pico/pico.v' skip_files << 'examples/pico/pico.v'
skip_files << 'examples/pico/raw_callback.v'
skip_files << 'examples/sokol/fonts.v' skip_files << 'examples/sokol/fonts.v'
skip_files << 'examples/sokol/drawing.v' skip_files << 'examples/sokol/drawing.v'
} }

View file

@ -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)
}

View file

@ -33,7 +33,7 @@ pub:
port int = 8080 port int = 8080
cb fn (voidptr, picohttpparser.Request, mut picohttpparser.Response) = 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 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 } user_data voidptr = unsafe { nil }
timeout_secs int = 8 timeout_secs int = 8
max_headers int = 100 max_headers int = 100
@ -43,10 +43,9 @@ pub:
[heap] [heap]
pub struct Picoev { pub struct Picoev {
cb fn (voidptr, picohttpparser.Request, mut picohttpparser.Response) = 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 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 timeout_secs int
max_headers int = 100 max_headers int = 100
@ -63,6 +62,8 @@ mut:
out &u8 = unsafe { nil } out &u8 = unsafe { nil }
date string date string
pub:
user_data voidptr = unsafe { nil }
} }
// init fills the `file_descriptors` array // 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 { } else if events & picoev.picoev_read != 0 {
pv.set_timeout(fd, pv.timeout_secs) pv.set_timeout(fd, pv.timeout_secs)
if !isnil(pv.raw_cb) { if !isnil(pv.raw_cb) {
pv.raw_cb(pv.user_data, fd) pv.raw_cb(mut pv, fd)
return return
} }