ci: fix `v -cc gcc -cstrict examples/pico/pico.v`

pull/12037/head
Delyan Angelov 2021-10-01 13:49:38 +03:00
parent 6f7c3a7cdf
commit 41de0c3c6a
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
5 changed files with 28 additions and 22 deletions

View File

@ -6,7 +6,7 @@ typedef struct phr_header phr_header_t;
#define ADVANCE_TOKEN2(buf, tok, toklen, max_len) \ #define ADVANCE_TOKEN2(buf, tok, toklen, max_len) \
do {\ do {\
for (int i = 0; i < max_len; i++) {\ for (u32 i = 0; i < max_len; i++) {\
if (buf[i] == ' ') {\ if (buf[i] == ' ') {\
tok = buf;\ tok = buf;\
toklen = i++;\ toklen = i++;\

View File

@ -16,7 +16,7 @@ import picohttpparser
struct C.in_addr { struct C.in_addr {
mut: mut:
s_addr int s_addr u32
} }
struct C.sockaddr_in { struct C.sockaddr_in {
@ -32,6 +32,7 @@ fn C.atoi() int
fn C.strncasecmp(s1 &char, s2 &char, n usize) int fn C.strncasecmp(s1 &char, s2 &char, n usize) int
[typedef]
struct C.picoev_loop {} struct C.picoev_loop {}
fn C.picoev_del(&C.picoev_loop, int) int fn C.picoev_del(&C.picoev_loop, int) int
@ -107,7 +108,7 @@ fn setup_sock(fd int) ? {
[inline] [inline]
fn close_conn(loop &C.picoev_loop, fd int) { fn close_conn(loop &C.picoev_loop, fd int) {
C.picoev_del(loop, fd) C.picoev_del(voidptr(loop), fd)
C.close(fd) C.close(fd)
} }
@ -127,7 +128,7 @@ fn rw_callback(loop &C.picoev_loop, fd int, events int, context voidptr) {
close_conn(loop, fd) close_conn(loop, fd)
return return
} else if (events & int(Event.read)) != 0 { } else if (events & int(Event.read)) != 0 {
C.picoev_set_timeout(loop, fd, p.timeout_secs) C.picoev_set_timeout(voidptr(loop), fd, p.timeout_secs)
// Request init // Request init
mut buf := p.buf mut buf := p.buf
@ -157,15 +158,18 @@ fn rw_callback(loop &C.picoev_loop, fd int, events int, context voidptr) {
return return
} else if r == -1 { } else if r == -1 {
// error // error
if C.errno == C.EAGAIN || C.errno == C.EWOULDBLOCK { if C.errno == C.EAGAIN {
// try again later // try again later
return return
} else { }
if C.errno == C.EWOULDBLOCK {
// try again later
return
}
// fatal error // fatal error
close_conn(loop, fd) close_conn(loop, fd)
return return
} }
}
p.idx[fd] += r p.idx[fd] += r
mut s := unsafe { tos(buf, p.idx[fd]) } mut s := unsafe { tos(buf, p.idx[fd]) }
@ -198,7 +202,8 @@ fn accept_callback(loop &C.picoev_loop, fd int, events int, cb_arg voidptr) {
p.err_cb(mut p.user_data, picohttpparser.Request{}, mut &picohttpparser.Response{}, p.err_cb(mut p.user_data, picohttpparser.Request{}, mut &picohttpparser.Response{},
err) err)
} }
C.picoev_add(loop, newfd, int(Event.read), p.timeout_secs, rw_callback, cb_arg) C.picoev_add(voidptr(loop), newfd, int(Event.read), p.timeout_secs, rw_callback,
cb_arg)
} }
} }
@ -225,7 +230,7 @@ pub fn new(config Config) &Picoev {
addr.sin_port = C.htons(config.port) addr.sin_port = C.htons(config.port)
addr.sin_addr.s_addr = C.htonl(C.INADDR_ANY) addr.sin_addr.s_addr = C.htonl(C.INADDR_ANY)
size := 16 // sizeof(C.sockaddr_in) size := 16 // sizeof(C.sockaddr_in)
bind_res := C.bind(fd, unsafe { &net.Addr(&addr) }, size) bind_res := C.bind(fd, voidptr(unsafe { &net.Addr(&addr) }), size)
assert bind_res == 0 assert bind_res == 0
listen_res := C.listen(fd, C.SOMAXCONN) listen_res := C.listen(fd, C.SOMAXCONN)
assert listen_res == 0 assert listen_res == 0
@ -242,11 +247,11 @@ pub fn new(config Config) &Picoev {
user_data: config.user_data user_data: config.user_data
timeout_secs: config.timeout_secs timeout_secs: config.timeout_secs
max_headers: config.max_headers max_headers: config.max_headers
date: C.get_date() date: &byte(C.get_date())
buf: unsafe { malloc_noscan(picoev.max_fds * picoev.max_read + 1) } buf: unsafe { malloc_noscan(picoev.max_fds * picoev.max_read + 1) }
out: unsafe { malloc_noscan(picoev.max_fds * picoev.max_write + 1) } out: unsafe { malloc_noscan(picoev.max_fds * picoev.max_write + 1) }
} }
C.picoev_add(loop, fd, int(Event.read), 0, accept_callback, pv) C.picoev_add(voidptr(loop), fd, int(Event.read), 0, accept_callback, pv)
go update_date(mut pv) go update_date(mut pv)
return pv return pv
} }
@ -259,7 +264,7 @@ pub fn (p Picoev) serve() {
fn update_date(mut p Picoev) { fn update_date(mut p Picoev) {
for { for {
p.date = C.get_date() p.date = &byte(C.get_date())
C.usleep(1000000) C.usleep(1000000)
} }
} }

View File

@ -29,7 +29,7 @@ fn C.phr_parse_headers(buf &char, len usize, headers &C.phr_header, num_headers
fn C.phr_parse_request_path(buf_start &char, len usize, method PPchar, method_len &usize, path PPchar, path_len &usize) int fn C.phr_parse_request_path(buf_start &char, len usize, method PPchar, method_len &usize, path PPchar, path_len &usize) int
fn C.phr_parse_request_path_pipeline(buf_start &char, len usize, method PPchar, method_len &usize, path PPchar, path_len &usize) int fn C.phr_parse_request_path_pipeline(buf_start &char, len usize, method PPchar, method_len &usize, path PPchar, path_len &usize) int
fn C.get_date() &byte fn C.get_date() &char
// static inline int u64toa(char* buf, uint64_t value) { // static inline int u64toa(char* buf, uint64_t value) {
fn C.u64toa(buffer &char, value u64) int fn C.u64toa(buffer &char, value u64) int

View File

@ -18,8 +18,9 @@ pub fn (mut r Request) parse_request(s string, max_headers int) int {
minor_version := 0 minor_version := 0
num_headers := usize(max_headers) num_headers := usize(max_headers)
pret := C.phr_parse_request(s.str, s.len, PPchar(&r.method.str), &method_len, PPchar(&r.path.str), pret := C.phr_parse_request(&char(s.str), s.len, voidptr(&r.method.str), &method_len,
&path_len, &minor_version, &r.headers[0], &num_headers, r.prev_len) voidptr(&r.path.str), &path_len, &minor_version, &r.headers[0], &num_headers,
r.prev_len)
if pret > 0 { if pret > 0 {
unsafe { unsafe {
r.method = tos(r.method.str, int(method_len)) r.method = tos(r.method.str, int(method_len))
@ -37,8 +38,8 @@ pub fn (mut r Request) parse_request_path(s string) int {
method_len := usize(0) method_len := usize(0)
path_len := usize(0) path_len := usize(0)
pret := C.phr_parse_request_path(s.str, s.len, PPchar(&r.method.str), &method_len, pret := C.phr_parse_request_path(&char(s.str), s.len, voidptr(&r.method.str), &method_len,
PPchar(&r.path.str), &path_len) voidptr(&r.path.str), &path_len)
if pret > 0 { if pret > 0 {
unsafe { unsafe {
r.method = tos(r.method.str, int(method_len)) r.method = tos(r.method.str, int(method_len))
@ -53,8 +54,8 @@ pub fn (mut r Request) parse_request_path_pipeline(s string) int {
method_len := usize(0) method_len := usize(0)
path_len := usize(0) path_len := usize(0)
pret := C.phr_parse_request_path_pipeline(s.str, s.len, PPchar(&r.method.str), &method_len, pret := C.phr_parse_request_path_pipeline(&char(s.str), s.len, voidptr(&r.method.str),
PPchar(&r.path.str), &path_len) &method_len, voidptr(&r.path.str), &path_len)
if pret > 0 { if pret > 0 {
unsafe { unsafe {
r.method = tos(r.method.str, int(method_len)) r.method = tos(r.method.str, int(method_len))

View File

@ -78,7 +78,7 @@ pub fn (mut r Response) json() &Response {
pub fn (mut r Response) body(body string) { pub fn (mut r Response) body(body string) {
r.write_string('Content-Length: ') r.write_string('Content-Length: ')
unsafe { unsafe {
r.buf += C.u64toa(r.buf, body.len) r.buf += C.u64toa(&char(r.buf), body.len)
} }
r.write_string('\r\n\r\n') r.write_string('\r\n\r\n')
r.write_string(body) r.write_string(body)
@ -106,7 +106,7 @@ pub fn (mut r Response) raw(response string) {
[inline] [inline]
pub fn (mut r Response) end() int { pub fn (mut r Response) end() int {
n := int(r.buf) - int(r.buf_start) n := int(i64(r.buf) - i64(r.buf_start))
if C.write(r.fd, r.buf_start, n) != n { if C.write(r.fd, r.buf_start, n) != n {
return -1 return -1
} }