1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision: 331331 $ -->
<refentry xml:id="eventhttp.accept" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>EventHttp::accept</refname>
<refpurpose>Makes an HTTP server accept connections on the specified socket stream or resource</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<modifier>public</modifier>
<type>bool</type>
<methodname>EventHttp::accept</methodname>
<methodparam>
<type>mixed</type>
<parameter>socket</parameter>
</methodparam>
</methodsynopsis>
<para>
Makes an HTTP server accept connections on the specified socket stream or
resource. The socket should be ready to accept connections.
</para>
<para>
Can be called multiple times to accept connections on different sockets.
</para>
<note>
<para>
To bind a socket,
<literal>listen</literal>
, and
<literal>
accept</literal>
connections on the socket in s single call use
<methodname>EventHttp::bind</methodname>
.
<methodname>EventHttp::accept</methodname>
is needed only if one already has a socket ready to accept connections.
</para>
</note>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<variablelist>
<varlistentry>
<term>
<parameter>socket</parameter>
</term>
<listitem>
<para>
Socket resource, stream or numeric file descriptor representing a socket
ready to accept connections.
</para>
</listitem>
</varlistentry>
</variablelist>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Returns &true; on success. Otherwise &false;.
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<example>
<title>
<function>EventHttp::accept</function> example</title>
<programlisting role="php">
<![CDATA[
<?php
$base = new EventBase();
$http = new EventHttp($base);
$addresses = array (
8091 => "127.0.0.1",
8092 => "127.0.0.2",
);
$i = 0;
$socket = array();
foreach ($addresses as $port => $ip) {
echo $ip, " ", $port, PHP_EOL;
$socket[$i] = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (!socket_bind($socket[$i], $ip, $port)) {
exit("socket_bind failed\n");
}
socket_listen($socket[$i], 0);
socket_set_nonblock($socket[$i]);
if (!$http->accept($socket[$i])) {
echo "Accept failed\n";
exit(1);
}
++$i;
}
$http->setCallback("/some-page", function() {
echo "(some-page)\n";
echo "URI: ", $req->getUri(), PHP_EOL;
$req->sendReply(200, "OK");
echo "OK\n";
});
$http->setDefaultCallback(function($req) {
echo "URI: ", $req->getUri(), PHP_EOL;
$req->sendReply(200, "OK");
echo "OK\n";
});
$signal = Event::signal($base, SIGINT, function () use ($base) {
echo "Caught SIGINT. Stopping...\n";
$base->stop();
});
$signal->add();
$base->dispatch();
echo "END\n";
// We didn't close sockets, since Libevent already sets
// CLOSE_ON_FREE and CLOSE_ON_EXEC flags on the file
// descriptor associated with the sockets.
?>
]]>
</programlisting>
&example.outputs.similar;
<screen>
<![CDATA[
Client:
$ nc 127.0.0.1 8091
GET /about HTTP/1.0
Connection: close
HTTP/1.0 200 OK
Content-Type: text/html; charset=ISO-8859-1
Connection: close
Server:
127.0.0.1 8091
127.0.0.2 8092
URI: /about
OK
]]>
</screen>
</example>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<simplelist>
<member>
<methodname>EventHttp::bind</methodname>
</member>
</simplelist>
</refsect1>
</refentry>
<!-- 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
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
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
-->
|