警告:當您傳輸「目標」字串時,EventListener::OPT_CLOSE_ON_FREE 會被強制設定。
唯一不設定 EventListener::OPT_CLOSE_ON_FREE 的方法是在建立 EventListener 之前綁定通訊端,並使用此通訊端作為「目標」。
(PECL event >= 1.2.6-beta)
EventListener::__construct — 建立與事件基底相關聯的新連線監聽器
$base
,$cb
,$data
,$flags
,$backlog
,$target
建立與事件基底相關聯的新連線監聽器。
base
相關聯的事件基底。
cb
收到新連線時將被呼叫的 callable。
data
附加到 cb
的自訂使用者資料。
旗標
EventListener::OPT_*
常數的位元遮罩。詳見 EventListener 常數。
待辦事項數量 (backlog)
控制網路堆疊允許在任何時間處於尚未接受狀態的待處理連線的最大數量;有關更多詳細資訊,請參閱系統 listen
函式的說明文件。如果 backlog
為負數,Libevent 會嘗試為 backlog
選擇一個適當的值;如果為零,Event 會假設已在通訊端 (target
) 上呼叫 listen
。
目標 (target)
可以是字串、通訊端資源或與通訊端關聯的串流。如果 target
是字串,則該字串將被解析為網路位址。如果以 'unix:'
為字首,例如 'unix:/tmp/my.sock'
,它將被解釋為 UNIX 網域通訊端路徑。
版本 | 說明 |
---|---|
PECL event 1.5.0 | 新增了對 UNIX 網域通訊端的支援。 |
範例 #1 EventListener::__construct() 範例
<?php
/*
* Simple echo server based on libevent's connection listener.
*
* Usage:
* 1) In one terminal window run:
*
* $ php listener.php 9881
*
* 2) In another terminal window open up connection, e.g.:
*
* $ nc 127.0.0.1 9881
*
* 3) start typing. The server should repeat the input.
*/
class MyListenerConnection {
private $bev, $base;
public function __destruct() {
$this->bev->free();
}
public function __construct($base, $fd) {
$this->base = $base;
$this->bev = new EventBufferEvent($base, $fd, EventBufferEvent::OPT_CLOSE_ON_FREE);
$this->bev->setCallbacks(array($this, "echoReadCallback"), NULL,
array($this, "echoEventCallback"), NULL);
if (!$this->bev->enable(Event::READ)) {
echo "Failed to enable READ\n";
return;
}
}
public function echoReadCallback($bev, $ctx) {
// Copy all the data from the input buffer to the output buffer
// Variant #1
$bev->output->addBuffer($bev->input);
/* Variant #2 */
/*
$input = $bev->getInput();
$output = $bev->getOutput();
$output->addBuffer($input);
*/
}
public function echoEventCallback($bev, $events, $ctx) {
if ($events & EventBufferEvent::ERROR) {
echo "Error from bufferevent\n";
}
if ($events & (EventBufferEvent::EOF | EventBufferEvent::ERROR)) {
//$bev->free();
$this->__destruct();
}
}
}
class MyListener {
public $base,
$listener,
$socket;
private $conn = array();
public function __destruct() {
foreach ($this->conn as &$c) $c = NULL;
}
public function __construct($port) {
$this->base = new EventBase();
if (!$this->base) {
echo "Couldn't open event base";
exit(1);
}
// Variant #1
/*
$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (!socket_bind($this->socket, '0.0.0.0', $port)) {
echo "Unable to bind socket\n";
exit(1);
}
$this->listener = new EventListener($this->base,
array($this, "acceptConnCallback"), $this->base,
EventListener::OPT_CLOSE_ON_FREE | EventListener::OPT_REUSEABLE,
-1, $this->socket);
*/
// Variant #2
$this->listener = new EventListener($this->base,
array($this, "acceptConnCallback"), $this->base,
EventListener::OPT_CLOSE_ON_FREE | EventListener::OPT_REUSEABLE, -1,
"0.0.0.0:$port");
if (!$this->listener) {
echo "Couldn't create listener";
exit(1);
}
$this->listener->setErrorCallback(array($this, "accept_error_cb"));
}
public function dispatch() {
$this->base->dispatch();
}
// This callback is invoked when there is data to read on $bev
public function acceptConnCallback($listener, $fd, $address, $ctx) {
// We got a new connection! Set up a bufferevent for it. */
$base = $this->base;
$this->conn[] = new MyListenerConnection($base, $fd);
}
public function accept_error_cb($listener, $ctx) {
$base = $this->base;
fprintf(STDERR, "Got an error %d (%s) on the listener. "
."Shutting down.\n",
EventUtil::getLastSocketErrno(),
EventUtil::getLastSocketError());
$base->exit(NULL);
}
}
$port = 9808;
if ($argc > 1) {
$port = (int) $argv[1];
}
if ($port <= 0 || $port > 65535) {
exit("Invalid port");
}
$l = new MyListener($port);
$l->dispatch();
?>
警告:當您傳輸「目標」字串時,EventListener::OPT_CLOSE_ON_FREE 會被強制設定。
唯一不設定 EventListener::OPT_CLOSE_ON_FREE 的方法是在建立 EventListener 之前綁定通訊端,並使用此通訊端作為「目標」。