2010-03-28 22:10:10 +00:00
|
|
|
<?xml version="1.0" encoding="utf-8"?>
|
2009-08-25 21:31:06 +00:00
|
|
|
<!-- $Revision$ -->
|
2009-08-14 13:44:58 +00:00
|
|
|
|
|
|
|
<chapter xml:id="libevent.examples" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
|
|
|
|
&reftitle.examples;
|
|
|
|
<para>
|
|
|
|
<example>
|
2009-08-25 12:46:34 +00:00
|
|
|
<title>polling <constant>STDIN</constant> using basic API</title>
|
2009-08-14 13:44:58 +00:00
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
|
|
|
|
2010-01-18 21:58:54 +00:00
|
|
|
function print_line($fd, $events, $arg)
|
2009-08-25 12:46:34 +00:00
|
|
|
{
|
2010-01-18 21:58:54 +00:00
|
|
|
static $max_requests = 0;
|
2009-08-25 12:46:34 +00:00
|
|
|
|
|
|
|
$max_requests++;
|
|
|
|
|
2010-01-18 21:58:54 +00:00
|
|
|
if ($max_requests == 10) {
|
|
|
|
// exit loop after 10 writes
|
2009-08-25 12:46:34 +00:00
|
|
|
event_base_loopexit($arg[1]);
|
|
|
|
}
|
|
|
|
|
2010-01-18 21:58:54 +00:00
|
|
|
// print the line
|
|
|
|
echo fgets($fd);
|
2009-08-25 12:46:34 +00:00
|
|
|
}
|
|
|
|
|
2010-01-18 21:58:54 +00:00
|
|
|
// create base and event
|
2009-08-25 12:46:34 +00:00
|
|
|
$base = event_base_new();
|
|
|
|
$event = event_new();
|
|
|
|
|
|
|
|
$fd = STDIN;
|
|
|
|
|
2010-01-18 21:58:54 +00:00
|
|
|
// set event flags
|
|
|
|
event_set($event, $fd, EV_READ | EV_PERSIST, "print_line", array($event, $base));
|
|
|
|
// set event base
|
2009-08-25 12:46:34 +00:00
|
|
|
event_base_set($event, $base);
|
|
|
|
|
2010-01-18 21:58:54 +00:00
|
|
|
// enable event
|
2009-08-25 12:46:34 +00:00
|
|
|
event_add($event);
|
2010-01-18 21:58:54 +00:00
|
|
|
// start event loop
|
2009-08-25 12:46:34 +00:00
|
|
|
event_base_loop($base);
|
2009-08-14 13:44:58 +00:00
|
|
|
|
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
</example>
|
|
|
|
</para>
|
|
|
|
<para>
|
2009-08-25 12:46:34 +00:00
|
|
|
<example>
|
|
|
|
<title>polling <constant>STDIN</constant> using buffered event API</title>
|
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
|
|
|
|
2010-01-18 21:58:54 +00:00
|
|
|
function print_line($buf, $arg)
|
2009-08-25 12:46:34 +00:00
|
|
|
{
|
|
|
|
static $max_requests;
|
|
|
|
|
|
|
|
$max_requests++;
|
|
|
|
|
|
|
|
if ($max_requests == 10) {
|
|
|
|
event_base_loopexit($arg);
|
|
|
|
}
|
|
|
|
|
2010-01-18 21:58:54 +00:00
|
|
|
// print the line
|
|
|
|
echo event_buffer_read($buf, 4096);
|
2009-08-25 12:46:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function error_func($buf, $what, $arg)
|
|
|
|
{
|
2010-01-18 21:58:54 +00:00
|
|
|
// handle errors
|
2009-08-25 12:46:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$base = event_base_new();
|
2010-01-18 21:58:54 +00:00
|
|
|
$eb = event_buffer_new(STDIN, "print_line", NULL, "error_func", $base);
|
2009-08-25 12:46:34 +00:00
|
|
|
|
2010-01-18 21:58:54 +00:00
|
|
|
event_buffer_base_set($eb, $base);
|
|
|
|
event_buffer_enable($eb, EV_READ);
|
2009-08-25 12:46:34 +00:00
|
|
|
|
|
|
|
event_base_loop($base);
|
|
|
|
|
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
</example>
|
2009-08-14 13:44:58 +00:00
|
|
|
</para>
|
|
|
|
</chapter>
|
|
|
|
|
|
|
|
<!-- Keep this comment at the end of the file
|
|
|
|
Local variables:
|
|
|
|
mode: sgml
|
|
|
|
sgml-omittag:t
|
|
|
|
sgml-shorttag:t
|
|
|
|
sgml-minimize-attributes:nil
|
|
|
|
sgml-always-quote-attributes:t
|
|
|
|
sgml-indent-step:1
|
|
|
|
sgml-indent-data:t
|
|
|
|
indent-tabs-mode:nil
|
|
|
|
sgml-parent-document:nil
|
2009-09-25 07:04:39 +00:00
|
|
|
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
2009-08-14 13:44:58 +00:00
|
|
|
sgml-exposed-tags:nil
|
|
|
|
sgml-local-catalogs:nil
|
|
|
|
sgml-local-ecat-files:nil
|
|
|
|
End:
|
|
|
|
vim600: syn=xml fen fdm=syntax fdl=2 si
|
|
|
|
vim: et tw=78 syn=sgml
|
|
|
|
vi: ts=1 sw=1
|
|
|
|
-->
|
|
|
|
|