NAME
Hypersonic::Event::Role - Base class for event backends
SYNOPSIS
package Hypersonic::Event::MyBackend;
use parent 'Hypersonic::Event::Role';
sub name { 'mybackend' }
sub available {
# Return true if this backend works on current platform
return $^O eq 'myos';
}
sub includes {
return '#include <mybackend.h>';
}
sub defines {
return '#define USE_MYBACKEND 1';
}
sub event_struct { 'mybackend_event' }
sub gen_create {
my ($class, $builder, $listen_fd_var) = @_;
$builder->line("int ev_fd = mybackend_create();");
# ... more code generation
}
# ... implement other gen_* methods
1;
DESCRIPTION
Hypersonic::Event::Role is the base class for all event loop backends in Hypersonic. It defines the interface contract and provides default implementations for optional methods.
Backends should inherit from this class using use parent.
REQUIRED METHODS
These methods MUST be implemented by every backend:
name
sub name { 'epoll' }
Return the backend name as a string.
available
sub available { $^O eq 'linux' }
Return true if this backend is available on the current platform.
includes
sub includes { '#include <sys/epoll.h>' }
Return C preprocessor #include directives needed by this backend.
defines
sub defines { '#define USE_EPOLL 1' }
Return C preprocessor #define directives.
event_struct
sub event_struct { 'epoll_event' }
Return the C struct name used for the events array.
gen_create($builder, $listen_fd_var)
Generate C code to create the event loop and register the listen socket.
sub gen_create {
my ($class, $builder, $listen_fd_var) = @_;
$builder->line("int ev_fd = epoll_create1(0);")
->line("/* ... */");
}
gen_add($builder, $loop_var, $fd_var)
Generate C code to add a file descriptor to the event loop.
gen_del($builder, $loop_var, $fd_var)
Generate C code to remove a file descriptor from the event loop.
gen_wait($builder, $loop_var, $events_var, $count_var, $timeout_var)
Generate C code to wait for events with a timeout.
gen_get_fd($builder, $events_var, $index_var, $fd_var)
Generate C code to extract the file descriptor from an event.
OPTIONAL METHODS
These methods have default implementations but MAY be overridden:
extra_cflags
sub extra_cflags { '-I/opt/mylib/include' }
Return additional compiler flags. Default: empty string.
extra_ldflags
sub extra_ldflags { '-luring' }
Return additional linker flags. Default: empty string.
gen_init($builder)
Generate one-time initialization code. Default: no-op.
gen_cleanup($builder)
Generate cleanup code for shutdown. Default: no-op.
UTILITY METHODS
_has_header(@paths)
if ($class->_has_header('/usr/include/foo.h')) { ... }
Check if any of the given header files exist.
_has_library($name)
if ($class->_has_library('uring')) { ... }
Check if a library exists in common library paths.
VARIABLE NAMING CONVENTION
When generating C code, use these variable names for consistency:
ev_fd - Event loop file descriptor
listen_fd - Server listen socket
client_fd - Accepted client socket
events - Array of event structures
n - Number of ready events
i - Loop index
fd - Current file descriptor being processed
SEE ALSO
Hypersonic::Event, Hypersonic::Event::Epoll, Hypersonic::Event::Kqueue
AUTHOR
LNATION <email@lnation.org>
LICENSE
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.