Skip to content

Commit

Permalink
fix raw mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Casper64 committed Nov 9, 2023
1 parent cd2e36a commit 6e57956
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
1 change: 1 addition & 0 deletions cmd/tools/modules/testing/common.v
Original file line number Diff line number Diff line change
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
40 changes: 40 additions & 0 deletions examples/pico/raw_callback.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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(data voidptr, fd int, remove_from_loop fn ()) {
// 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 first before closing the connection
remove_from_loop()
conn.close() or { eprintln('could not close connection') }
}
16 changes: 10 additions & 6 deletions vlib/picoev/picoev.v
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ pub mut:

pub struct Config {
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 }
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
// the last argument is a function that removes the current file descriptor
// from the event loop
raw_cb fn (voidptr, int, fn ()) = unsafe { nil }
user_data voidptr = unsafe { nil }
timeout_secs int = 8
max_headers int = 100
Expand All @@ -45,7 +47,7 @@ pub:
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 }
raw_cb fn (voidptr, int, fn ()) = unsafe { nil }
user_data voidptr = unsafe { nil }

timeout_secs int
Expand Down Expand Up @@ -213,7 +215,9 @@ 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(pv.user_data, fd, fn [mut pv, fd] () {
pv.del(fd)
})
return
}

Expand Down

0 comments on commit 6e57956

Please sign in to comment.