Event EventListener::__construct
(PECL event >= 1.2.6-beta)
EventListener::__construct — 이벤트 기반과 연결된 새 연결 수신기를 만듭니다.
설명
public EventListener::__construct( EventBase $base , callable $cb , mixed $data , int $flags , int $backlog , mixed $target )
이벤트 기반과 연결된 새 연결 수신기를 만듭니다.
매개변수
base
- 관련 이벤트 베이스.
cb
- 새로운 연결이 수신될 때 호출될 callable입니다.
data
cb
에 첨부된 사용자 정의 사용자 데이터.flags
- 비트 마스크 또는
EventListener::OPT_*
상수입니다. EventListener constants를 참조하십시오. backlog
- 네트워크 스택이 언제든지 아직 허용되지 않은 상태에서 대기하도록 허용해야 하는 보류 중인 연결의 최대 수를 제어합니다. 자세한 내용은 시스템의
listen
함수에 대한 설명서를 참조하십시오.backlog
가 음수이면 Libevent는backlog
에 대해 적절한 값을 선택하려고 시도합니다. 0이면 이벤트는 소켓(target
)에서 이미listen
대기가 호출되었다고 가정합니다. target
- 문자열, 소켓 리소스 또는 소켓과 연결된 스트림일 수 있습니다.
target
이 문자열인 경우 문자열은 네트워크 주소로 구문 분석됩니다. 접두사가'unix:'
인 경우 UNIX 도메인 소켓 경로로 해석됩니다.'unix:/tmp/my.sock'
.
반환 값
이벤트 연결 수신기를 나타내는 EventListener 객체를 반환합니다.
변경 로그
버전 | 설명 |
---|---|
PECL event 1.5.0 | UNIX 도메인 소켓 지원이 추가되었습니다. |
Examples
예제 #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();
?>