Skip to content

Commit a176021

Browse files
authored
picoev: improve raw mode, change fn signature for the raw_cb field to fn (mut Picoev, int) (#19817)
1 parent e7cad4f commit a176021

File tree

3 files changed

+47
-6
lines changed

3 files changed

+47
-6
lines changed

cmd/tools/modules/testing/common.v

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ pub fn new_test_session(_vargs string, will_compile bool) TestSession {
209209
$if solaris {
210210
skip_files << 'examples/gg/gg2.v'
211211
skip_files << 'examples/pico/pico.v'
212+
skip_files << 'examples/pico/raw_callback.v'
212213
skip_files << 'examples/sokol/fonts.v'
213214
skip_files << 'examples/sokol/drawing.v'
214215
}

examples/pico/raw_callback.v

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
module main
2+
3+
import net
4+
import picoev
5+
6+
const (
7+
port = 8080
8+
http_response = 'HTTP/1.1 200 OK\r\nContent-type: text/html\r\nContent-length: 18\r\n\r\nHello from Picoev!'
9+
)
10+
11+
fn main() {
12+
println('Starting webserver on http://localhost:${port}/ ...')
13+
mut pico := picoev.new(
14+
port: port
15+
raw_cb: handle_conn
16+
)
17+
pico.serve()
18+
}
19+
20+
fn handle_conn(mut pv picoev.Picoev, fd int) {
21+
// setup a nonblocking tcp connection
22+
mut conn := &net.TcpConn{
23+
sock: net.tcp_socket_from_handle_raw(fd)
24+
handle: fd
25+
is_blocking: false
26+
}
27+
28+
mut buf := []u8{len: 4096}
29+
// read data from the tcp connection
30+
conn.read(mut buf) or { eprintln('could not read data from socket') }
31+
32+
println('received data:')
33+
println(buf.bytestr())
34+
35+
conn.write(http_response.bytes()) or { eprintln('could not write response') }
36+
37+
// remove the socket from picoev's event loop and close the connection
38+
pv.close_conn(fd)
39+
}

vlib/picoev/picoev.v

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub:
3333
port int = 8080
3434
cb fn (voidptr, picohttpparser.Request, mut picohttpparser.Response) = unsafe { nil }
3535
err_cb fn (voidptr, picohttpparser.Request, mut picohttpparser.Response, IError) = default_err_cb
36-
raw_cb fn (voidptr, int) = unsafe { nil }
36+
raw_cb fn (mut Picoev, int) = unsafe { nil }
3737
user_data voidptr = unsafe { nil }
3838
timeout_secs int = 8
3939
max_headers int = 100
@@ -43,10 +43,9 @@ pub:
4343

4444
[heap]
4545
pub struct Picoev {
46-
cb fn (voidptr, picohttpparser.Request, mut picohttpparser.Response) = unsafe { nil }
47-
err_cb fn (voidptr, picohttpparser.Request, mut picohttpparser.Response, IError) = default_err_cb
48-
raw_cb fn (voidptr, int) = unsafe { nil }
49-
user_data voidptr = unsafe { nil }
46+
cb fn (voidptr, picohttpparser.Request, mut picohttpparser.Response) = unsafe { nil }
47+
err_cb fn (voidptr, picohttpparser.Request, mut picohttpparser.Response, IError) = default_err_cb
48+
raw_cb fn (mut Picoev, int) = unsafe { nil }
5049

5150
timeout_secs int
5251
max_headers int = 100
@@ -63,6 +62,8 @@ mut:
6362
out &u8 = unsafe { nil }
6463

6564
date string
65+
pub:
66+
user_data voidptr = unsafe { nil }
6667
}
6768

6869
// init fills the `file_descriptors` array
@@ -213,7 +214,7 @@ fn raw_callback(fd int, events int, context voidptr) {
213214
} else if events & picoev.picoev_read != 0 {
214215
pv.set_timeout(fd, pv.timeout_secs)
215216
if !isnil(pv.raw_cb) {
216-
pv.raw_cb(pv.user_data, fd)
217+
pv.raw_cb(mut pv, fd)
217218
return
218219
}
219220

0 commit comments

Comments
 (0)