mirror of
https://github.com/vlang/v.git
synced 2025-09-15 23:42:28 +03:00
vlib: run vfmt over vlib files, so that v doc -m vlib/
can run without warnings
This commit is contained in:
parent
5b1ab3b0bb
commit
dab66593fc
27 changed files with 343 additions and 334 deletions
|
@ -11,19 +11,16 @@ import picohttpparser
|
|||
#include <netinet/tcp.h>
|
||||
#include <fcntl.h>
|
||||
#include <signal.h>
|
||||
|
||||
#flag -I @VROOT/thirdparty/picoev
|
||||
#flag -L @VROOT/thirdparty/picoev
|
||||
#flag @VROOT/thirdparty/picoev/picoev.o
|
||||
|
||||
#include "src/picoev.h"
|
||||
|
||||
const (
|
||||
max_fds = 1024
|
||||
max_fds = 1024
|
||||
timeout_secs = 8
|
||||
max_timeout = 10
|
||||
max_read = 4096
|
||||
max_write = 8192
|
||||
max_timeout = 10
|
||||
max_read = 4096
|
||||
max_write = 8192
|
||||
)
|
||||
|
||||
struct C.in_addr {
|
||||
|
@ -38,31 +35,48 @@ mut:
|
|||
sin_addr C.in_addr
|
||||
}
|
||||
|
||||
struct C.sockaddr_storage {}
|
||||
struct C.sockaddr_storage {
|
||||
}
|
||||
|
||||
fn C.atoi() int
|
||||
|
||||
fn C.strncasecmp() int
|
||||
|
||||
fn C.socket() int
|
||||
|
||||
fn C.setsockopt() int
|
||||
|
||||
fn C.htonl() int
|
||||
|
||||
fn C.htons() int
|
||||
|
||||
fn C.bind() int
|
||||
|
||||
fn C.listen() int
|
||||
|
||||
fn C.accept() int
|
||||
|
||||
fn C.getaddrinfo() int
|
||||
|
||||
fn C.connect() int
|
||||
|
||||
fn C.send() int
|
||||
|
||||
fn C.recv() int
|
||||
//fn C.read() int
|
||||
|
||||
// fn C.read() int
|
||||
fn C.shutdown() int
|
||||
//fn C.close() int
|
||||
|
||||
// fn C.close() int
|
||||
fn C.ntohs() int
|
||||
|
||||
fn C.getsockname() int
|
||||
|
||||
fn C.fcntl() int
|
||||
//fn C.write() int
|
||||
|
||||
struct C.picoev_loop {}
|
||||
// fn C.write() int
|
||||
struct C.picoev_loop {
|
||||
}
|
||||
|
||||
struct Picoev {
|
||||
loop &C.picoev_loop
|
||||
|
@ -76,15 +90,25 @@ mut:
|
|||
}
|
||||
|
||||
fn C.picoev_del(&C.picoev_loop, int) int
|
||||
|
||||
fn C.picoev_set_timeout(&C.picoev_loop, int, int)
|
||||
|
||||
fn C.picoev_add(&C.picoev_loop, int, int, int, &C.picoev_handler, voidptr) int
|
||||
|
||||
fn C.picoev_init(int) int
|
||||
|
||||
fn C.picoev_create_loop(int) &C.picoev_loop
|
||||
|
||||
fn C.picoev_loop_once(&C.picoev_loop, int) int
|
||||
|
||||
fn C.picoev_destroy_loop(&C.picoev_loop) int
|
||||
|
||||
fn C.picoev_deinit() int
|
||||
|
||||
fn C.phr_parse_request() int
|
||||
|
||||
fn C.phr_parse_request_path_pipeline() int
|
||||
|
||||
fn C.phr_parse_request_path() int
|
||||
|
||||
[inline]
|
||||
|
@ -105,27 +129,26 @@ fn close_conn(loop &C.picoev_loop, fd int) {
|
|||
}
|
||||
|
||||
[inline]
|
||||
fn myread(fd int, b byteptr, max_len, idx int) int {
|
||||
fn myread(fd int, b byteptr, max_len int, idx int) int {
|
||||
unsafe {
|
||||
return C.read(fd, b + idx, max_len - idx)
|
||||
}
|
||||
}
|
||||
|
||||
[inline]
|
||||
fn mysubstr(s byteptr, from, len int) string {
|
||||
fn mysubstr(s byteptr, from int, len int) string {
|
||||
unsafe {
|
||||
return tos(s + from, len)
|
||||
}
|
||||
}
|
||||
|
||||
fn rw_callback(loop &C.picoev_loop, fd, events int, cb_arg voidptr) {
|
||||
fn rw_callback(loop &C.picoev_loop, fd int, events int, cb_arg voidptr) {
|
||||
mut p := &Picoev(cb_arg)
|
||||
if (events & C.PICOEV_TIMEOUT) != 0 {
|
||||
close_conn(loop, fd)
|
||||
p.idx[fd] = 0
|
||||
return
|
||||
}
|
||||
else if (events & C.PICOEV_READ) != 0 {
|
||||
} else if (events & C.PICOEV_READ) != 0 {
|
||||
C.picoev_set_timeout(loop, fd, timeout_secs)
|
||||
mut buf := p.buf
|
||||
unsafe {
|
||||
|
@ -138,7 +161,7 @@ fn rw_callback(loop &C.picoev_loop, fd, events int, cb_arg voidptr) {
|
|||
p.idx[fd] = 0
|
||||
return
|
||||
} else if r == -1 {
|
||||
if false { //errno == C.EAGAIN || errno == C.EWOULDBLOCK {
|
||||
if false { // errno == C.EAGAIN || errno == C.EWOULDBLOCK {
|
||||
// TODO
|
||||
} else {
|
||||
close_conn(loop, fd)
|
||||
|
@ -165,24 +188,25 @@ fn rw_callback(loop &C.picoev_loop, fd, events int, cb_arg voidptr) {
|
|||
for {
|
||||
pret := req.parse_request(s, 100)
|
||||
if pret <= 0 && s.len > 0 {
|
||||
unsafe { C.memmove(buf, s.str, s.len) }
|
||||
unsafe {C.memmove(buf, s.str, s.len)}
|
||||
p.idx[fd] = s.len
|
||||
p.oidx[fd] = int(res.buf) - int(res.buf_start)
|
||||
break
|
||||
}
|
||||
c0 := unsafe { req.method.str[0] }
|
||||
if c0 ==`p` || c0 == `P` || c0 == `d` || c0 == `D` {
|
||||
c0 := unsafe {req.method.str[0]}
|
||||
if c0 == `p` || c0 == `P` || c0 == `d` || c0 == `D` {
|
||||
mut j := 0
|
||||
for {
|
||||
if j == req.num_headers {
|
||||
break
|
||||
}
|
||||
if req.headers[j].name_len == 14 && C.strncasecmp(req.headers[j].name, "content-length", 14) == 0 {
|
||||
//cont_length := C.atoi(tos(req.headers[j].value, req.headers[j].value_len).str)
|
||||
//println('$cont_length')
|
||||
//TODO need to maintain state of incomplete request to collect body later
|
||||
if req.headers[j].name_len == 14 &&
|
||||
C.strncasecmp(req.headers[j].name, 'content-length', 14) == 0 {
|
||||
// cont_length := C.atoi(tos(req.headers[j].value, req.headers[j].value_len).str)
|
||||
// println('$cont_length')
|
||||
// TODO need to maintain state of incomplete request to collect body later
|
||||
}
|
||||
j = j+1
|
||||
j = j + 1
|
||||
}
|
||||
}
|
||||
p.cb(req, mut &res)
|
||||
|
@ -201,7 +225,7 @@ fn rw_callback(loop &C.picoev_loop, fd, events int, cb_arg voidptr) {
|
|||
}
|
||||
}
|
||||
|
||||
fn accept_callback(loop &C.picoev_loop, fd, events int, cb_arg voidptr) {
|
||||
fn accept_callback(loop &C.picoev_loop, fd int, events int, cb_arg voidptr) {
|
||||
newfd := C.accept(fd, 0, 0)
|
||||
if newfd != -1 {
|
||||
setup_sock(newfd)
|
||||
|
@ -212,7 +236,6 @@ fn accept_callback(loop &C.picoev_loop, fd, events int, cb_arg voidptr) {
|
|||
pub fn new(port int, cb voidptr) &Picoev {
|
||||
fd := C.socket(C.AF_INET, C.SOCK_STREAM, 0)
|
||||
assert fd != -1
|
||||
|
||||
flag := 1
|
||||
assert C.setsockopt(fd, C.SOL_SOCKET, C.SO_REUSEADDR, &flag, sizeof(int)) == 0
|
||||
assert C.setsockopt(fd, C.SOL_SOCKET, C.SO_REUSEPORT, &flag, sizeof(int)) == 0
|
||||
|
@ -223,7 +246,6 @@ pub fn new(port int, cb voidptr) &Picoev {
|
|||
queue_len := 4096
|
||||
assert C.setsockopt(fd, C.IPPROTO_TCP, C.TCP_FASTOPEN, &queue_len, sizeof(int)) == 0
|
||||
}
|
||||
|
||||
mut addr := C.sockaddr_in{}
|
||||
addr.sin_family = C.AF_INET
|
||||
addr.sin_port = C.htons(port)
|
||||
|
@ -231,12 +253,9 @@ pub fn new(port int, cb voidptr) &Picoev {
|
|||
size := 16 // sizeof(C.sockaddr_in)
|
||||
bind_res := C.bind(fd, &addr, size)
|
||||
assert bind_res == 0
|
||||
|
||||
listen_res := C.listen(fd, C.SOMAXCONN)
|
||||
assert listen_res == 0
|
||||
|
||||
setup_sock(fd)
|
||||
|
||||
C.picoev_init(max_fds)
|
||||
loop := C.picoev_create_loop(max_timeout)
|
||||
mut pv := &Picoev{
|
||||
|
@ -247,9 +266,7 @@ pub fn new(port int, cb voidptr) &Picoev {
|
|||
out: malloc(max_fds * max_write + 1)
|
||||
}
|
||||
C.picoev_add(loop, fd, C.PICOEV_READ, 0, accept_callback, pv)
|
||||
|
||||
go update_date(mut pv)
|
||||
|
||||
return pv
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue