File: setclosecallback.xml

package info (click to toggle)
php-doc 20140201-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 74,084 kB
  • ctags: 4,040
  • sloc: xml: 998,137; php: 20,812; cpp: 500; sh: 177; makefile: 63; awk: 28
file content (185 lines) | stat: -rw-r--r-- 4,925 bytes parent folder | download
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
176
177
178
179
180
181
182
183
184
185
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision: 331716 $ -->
<refentry xml:id="eventhttpconnection.setclosecallback" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
 <refnamediv>
  <refname>EventHttpConnection::setCloseCallback</refname>
  <refpurpose>Set callback for connection close</refpurpose>
 </refnamediv>
 <refsect1 role="description">
  &reftitle.description;
  <methodsynopsis>
   <modifier>public</modifier>
   <type>void</type>
   <methodname>EventHttpConnection::setCloseCallback</methodname>
   <methodparam>
    <type>callable</type>
    <parameter>callback</parameter>
   </methodparam>
   <methodparam choice="opt">
    <type>mixed</type>
    <parameter>data</parameter>
   </methodparam>
  </methodsynopsis>
  <para>
   Sets callback for connection close.
  </para>
 </refsect1>
 <refsect1 role="parameters">
  &reftitle.parameters;
  <variablelist>
   <varlistentry>
    <term>
     <parameter>callback</parameter>
    </term>
    <listitem>
     <para>
      Callback which is called when connection is closed. Should match the
      following prototype:
     </para>
     <methodsynopsis>
      <type>void</type>
      <methodname>callback</methodname>
      <methodparam
        choice="opt">
       <type>EventHttpConnection</type>
       <parameter>conn</parameter>
       <initializer>&null;</initializer>
      </methodparam>
      <methodparam
        choice="opt">
       <type>mixed</type>
       <parameter>arg</parameter>
       <initializer>&null;</initializer>
      </methodparam>
     </methodsynopsis>
    </listitem>
   </varlistentry>
  </variablelist>
 </refsect1>
 <refsect1 role="returnvalues">
  &reftitle.returnvalues;
  <para>
   &return.void;
  </para>
 </refsect1>
 <refsect1 role="examples">
  &reftitle.examples;
  <example>
   <title>
    <methodname>EventHttpConnection::setCloseCallback</methodname> example</title>
   <programlisting role="php">
<![CDATA[
<?php
/*
 * Setting up close-connection callback
 *
 * The script handles closed connections using HTTP API.
 *
 * Usage:
 * 1) Launch the server:
 * $ php examples/http_closecb.php 4242
 *
 * 2) Launch a client in another terminal. Telnet-like
 * session should look like the following:
 *
 * $ nc -t 127.0.0.1 4242
 * GET / HTTP/1.0
 * Connection: close
 *
 * The server will output something similar to the following:
 *
 * HTTP/1.0 200 OK
 * Content-Type: multipart/x-mixed-replace;boundary=boundarydonotcross
 * Connection: close
 *
 * <html>
 *
 * 3) Terminate the client connection abruptly,
 * i.e. kill the process, or just press Ctrl-C.
 *
 * 4) Check if the server called _close_callback.
 * The script should output "_close_callback" string to standard output.
 *
 * 5) Check if the server's process has no orphaned connections,
 * e.g. with `lsof` utility.
 */

function _close_callback($conn) {
	echo __FUNCTION__, PHP_EOL;
}

function _http_default($req, $dummy) {
	$conn = $req->getConnection();
	$conn->setCloseCallback('_close_callback', NULL);

	/*
	By enabling Event::READ we protect the server against unclosed conections.
	This is a peculiarity of Libevent. The library disables Event::READ events
 	on this connection, and the server is not notified about terminated
	connections.

	So each time client terminates connection abruptly, we get an orphaned
	connection. For instance, the following is a part of `lsof -p $PID | grep TCP`
	command after client has terminated connection:

	57-php     15057 ruslan  6u  unix 0xffff8802fb59c780   0t0  125187 socket
	58:php     15057 ruslan  7u  IPv4             125189   0t0     TCP *:4242 (LISTEN)
	59:php     15057 ruslan  8u  IPv4             124342   0t0     TCP localhost:4242->localhost:37375 (CLOSE_WAIT)

	where $PID is our process ID. 

	The following block of code fixes such kind of orphaned connections.
	 */
	$bev = $req->getBufferEvent();
	$bev->enable(Event::READ);

	$req->addHeader('Content-Type',
		'multipart/x-mixed-replace;boundary=boundarydonotcross',
		EventHttpRequest::OUTPUT_HEADER);

	$buf = new EventBuffer();
	$buf->add('<html>');

	$req->sendReply(200, "OK");
	$req->sendReplyChunk($buf);
}

$port = 4242;
if ($argc > 1) {
	$port = (int) $argv[1];
}
if ($port <= 0 || $port > 65535) {
	exit("Invalid port");
}

$base = new EventBase();
$http = new EventHttp($base);

$http->setDefaultCallback("_http_default", NULL);
$http->bind("0.0.0.0", $port);
$base->loop();
]]>
   </programlisting>
  </example>
 </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
-->