Skip to content

picoev #

Description:

picoev is a V implementation of picoev, which in turn is "A tiny, lightning fast event loop for network applications".

Constants #

const max_fds = 1024
const max_queue = 4096
const picoev_read = 1

events

const picoev_write = 2
const picoev_timeout = 4
const picoev_add = 0x40000000
const picoev_del = 0x20000000
const picoev_readwrite = 3

fn create_epoll_loop #

fn create_epoll_loop(id int) !&EpollLoop

create_epoll_loop creates a new epoll instance for and returns an EpollLoop struct with id

fn new #

fn new(config Config) &Picoev

new creates a Picoev struct and initializes the main loop

struct C.epoll_data_t #

union C.epoll_data_t {
mut:
	ptr voidptr
	fd  int
	u32 u32
	u64 u64
}

struct C.epoll_event #

struct C.epoll_event {
mut:
	events u32
	data   C.epoll_data_t
}

struct Config #

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 (mut Picoev, int) = unsafe { nil }
	user_data    voidptr = unsafe { nil }
	timeout_secs int     = 8
	max_headers  int     = 100
	max_read     int     = 4096
	max_write    int     = 8192
}

struct EpollLoop #

struct EpollLoop {
mut:
	id       int
	epoll_fd int
	events   [1024]C.epoll_event
	now      i64
}

struct Picoev #

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 (mut Picoev, int) = unsafe { nil }

	timeout_secs int
	max_headers  int = 100
	max_read     int = 4096
	max_write    int = 8192
mut:
	loop             &LoopType = unsafe { nil }
	file_descriptors [max_fds]&Target
	timeouts         map[int]i64
	num_loops        int

	buf &u8 = unsafe { nil }
	idx [1024]int
	out &u8 = unsafe { nil }

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

fn (Picoev) init #

fn (mut pv Picoev) init()

init fills the file_descriptors array

fn (Picoev) add #

fn (mut pv Picoev) add(fd int, events int, timeout int, cb voidptr) int

add adds a file descriptor to the loop

fn (Picoev) close_conn #

fn (mut pv Picoev) close_conn(fd int)

close_conn closes the socket fd and removes it from the loop

fn (Picoev) serve #

fn (mut pv Picoev) serve()

serve starts the Picoev server

struct Target #

struct Target {
pub mut:
	fd      int
	loop_id int = -1
	events  u32
	cb      fn (int, int, voidptr) = unsafe { nil }
	// used internally by the kqueue implementation
	backend int
}

Target is a data representation of everything that needs to be associated with a single file descriptor (connection)