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

maximum number of file descriptors that can be managed

const max_queue = 4096

maximum size of the event queue

const picoev_read = 1

event for incoming data ready to be read on a socket

const picoev_write = 2

event for socket ready for writing

const picoev_timeout = 4

event indicating a timeout has occurred

const picoev_add = 0x40000000

flag for adding a file descriptor to the event loop

const picoev_del = 0x20000000

flag for removing a file descriptor from the event loop

const picoev_readwrite = 3

event read/write

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 #

@[typedef]
union C.epoll_data_t {
mut:
	ptr voidptr
	fd  int
	u32 u32
	u64 u64
}

struct C.epoll_event #

@[packed]
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_error_callback
	raw_cb       fn (mut Picoev, int, int) = unsafe { nil }
	user_data    voidptr        = unsafe { nil }
	timeout_secs int            = 8
	max_headers  int            = 100
	max_read     int            = 4096
	max_write    int            = 8192
	family       net.AddrFamily = .ip6
	host         string
}

Config configures the Picoev instance with server settings and callbacks

struct EpollLoop #

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

struct Picoev #

@[heap]
struct Picoev {
	cb             fn (voidptr, picohttpparser.Request, mut picohttpparser.Response) = unsafe { nil }
	error_callback fn (voidptr, picohttpparser.Request, mut picohttpparser.Response, IError) = default_error_callback
	raw_callback   fn (mut Picoev, int, int) = unsafe { nil }

	timeout_secs int
	max_headers  int = 100
	max_read     int = 4096
	max_write    int = 8192

	err_cb fn (voidptr, picohttpparser.Request, mut picohttpparser.Response, IError) = default_error_callback @[deprecated: 'use `error_callback` instead']
	raw_cb fn (mut Picoev, int, int) = unsafe { nil }                                                 @[deprecated: 'use `raw_callback` instead']
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 }
}

Core structure for managing the event loop and connections. Contains event loop, file descriptor table, timeouts, buffers, and configuration.

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, callback voidptr) int

add a file descriptor to the event loop

fn (Picoev) del #

deprecated: use delete() instead
fn (mut pv Picoev) del(fd int) int

del remove a file descriptor from the event loop

fn (Picoev) delete #

fn (mut pv Picoev) delete(fd int) int

remove a file descriptor from the event 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 event loop for accepting new connections See also picoev.new().

struct Target #

struct Target {
pub mut:
	fd      int // file descriptor
	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)