Socket-Class
view release on metacpan or search on metacpan
my( $client ) = @_;
my( $buf, $got );
print 'Connection from ' . $client->remote_addr .
' port ' . $client->remote_port . "\n";
# do something with the client
$client->set_blocking( 0 );
while( $RUNNING ) {
$got = $client->read( $buf, 4096 );
if( ! defined $got ) {
# error
warn $client->error;
last;
}
elsif( ! $got ) {
# no data available, sleep for a while
$client->wait( 10 );
next;
}
print "Got $got bytes from client\n";
$client->write( 'thank you!' );
}
# close the client and free allocated resources
$client->wait( 50 );
$client->free();
# detach thread
threads->self->detach() if $RUNNING;
return 1;
}
=head1 XS / C API
The module provides a C interface for extension writers.
B<Example XS>
=for formatter cpp
#include <mod_sc.h>
/* global pointer to the socket class interface */
mod_sc_t *g_mod_sc;
MODULE = MyModule PACKAGE = MyModule
BOOT:
{
SV **psv;
psv = hv_fetch(PL_modglobal, "Socket::Class", 13, 0);
if (psv == NULL)
croak("Socket::Class 2.0 or higher required");
g_mod_sc = INT2PTR(mod_sc_t *, SvIV(*psv));
}
void
test()
PREINIT:
sc_t *socket;
char *args[4];
int r;
SV *sv;
PPCODE:
args[0] = "local_port";
args[1] = "8080";
args[2] = "listen";
args[3] = "10";
r = g_mod_sc->sc_create(args, 4, &socket);
if (r != SC_OK)
croak(g_mod_sc->sc_get_error(NULL));
g_mod_sc->sc_create_class(socket, NULL, &sv);
ST(0) = sv_2mortal(sv);
XSRETURN(1);
=for formatter perl
See I<mod_sc.h> for the definition and the source code of I<Class.xs>
for an implementation.
Use I<Socket::Class::include_path()> to get the path to I<mod_sc.h>.
=head1 AUTHORS
Christian Mueller, L<http://www.alien-heads.org/>
=head1 COPYRIGHT AND LICENSE
The Socket::Class module is free software. You may distribute under the
terms of either the GNU General Public License or the Artistic
License, as specified in the Perl README file.
=cut
( run in 0.317 second using v1.01-cache-2.11-cpan-5511b514fd6 )