Skip to content

Commit

Permalink
picoev: improve raw mode, change fn signature for the raw_cb field …
Browse files Browse the repository at this point in the history
…to `fn (mut Picoev, int)` (#19817)
  • Loading branch information
Casper64 committed Nov 11, 2023
1 parent e7cad4f commit a176021
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
1 change: 1 addition & 0 deletions cmd/tools/modules/testing/common.v
Expand Up @@ -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'
}
Expand Down
39 changes: 39 additions & 0 deletions 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)
}
13 changes: 7 additions & 6 deletions vlib/picoev/picoev.v
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -63,6 +62,8 @@ mut:
out &u8 = unsafe { nil }

date string
pub:
user_data voidptr = unsafe { nil }
}

// init fills the `file_descriptors` array
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit a176021

Please sign in to comment.