Event EventBufferEvent::connect
(PECL event >= 1.2.6-beta)
EventBufferEvent::connect — 버퍼 이벤트의 파일 디스크립터를 주어진 주소 또는 UNIX 소켓에 연결
설명
public EventBufferEvent::connect(string $addr
): bool
버퍼 이벤트의 파일 디스크립터를 주어진 주소(선택적으로 포트와 함께) 또는 UNIX 도메인 소켓에 연결합니다.
소켓이 버퍼 이벤트에 할당되지 않은 경우 이 함수는 새 소켓을 할당하고 내부적으로 비블로킹으로 만듭니다.
DNS 이름을 (비동기적으로) 확인하려면 EventBufferEvent::connectHost() 메서드를 사용하십시오.
매개변수
addr
- 선택적 포트 번호가 있는 IP 주소 또는 UNIX 도메인 소켓에 대한 경로를 포함해야 합니다. 인식되는 형식은 다음과 같습니다.
[IPv6Address]:port [IPv6Address] IPv6Address IPv4Address:port IPv4Address unix:path
참고로
'unix:'
접두사는 현재 대소문자를 구분하지 않습니다.
반환 값
성공하면 true
를, 실패하면 false
를 반환합니다.
Examples
예제 #1 EventBufferEvent::connect() 예제
<?php
/*
* 1. Connect to 127.0.0.1 at port 80
* by means of EventBufferEvent::connect().
*
* 2. Request /index.cphp via HTTP/1.0
* using the output buffer.
*
* 3. Asyncronously read the response and print it to stdout.
*/
/* Read callback */
function readcb($bev, $base) {
$input = $bev->getInput();
while (($n = $input->remove($buf, 1024)) > 0) {
echo $buf;
}
}
/* Event callback */
function eventcb($bev, $events, $base) {
if ($events & EventBufferEvent::CONNECTED) {
echo "Connected.\n";
} elseif ($events & (EventBufferEvent::ERROR | EventBufferEvent::EOF)) {
if ($events & EventBufferEvent::ERROR) {
echo "DNS error: ", $bev->getDnsErrorString(), PHP_EOL;
}
echo "Closing\n";
$base->exit();
exit("Done\n");
}
}
$base = new EventBase();
echo "step 1\n";
$bev = new EventBufferEvent($base, /* use internal socket */ NULL,
EventBufferEvent::OPT_CLOSE_ON_FREE | EventBufferEvent::OPT_DEFER_CALLBACKS);
if (!$bev) {
exit("Failed creating bufferevent socket\n");
}
echo "step 2\n";
$bev->setCallbacks("readcb", /* writecb */ NULL, "eventcb", $base);
$bev->enable(Event::READ | Event::WRITE);
echo "step 3\n";
/* Send request */
$output = $bev->getOutput();
if (!$output->add(
"GET /index.cphp HTTP/1.0\r\n".
"Connection: Close\r\n\r\n"
)) {
exit("Failed adding request to output buffer\n");
}
/* Connect to the host syncronously.
* We know the IP, and don't need to resolve DNS. */
if (!$bev->connect("127.0.0.1:80")) {
exit("Can't connect to host\n");
}
/* Dispatch pending events */
$base->dispatch();
위의 예는 다음과 유사한 결과를 출력합니다.
step 1 step 2 step 3 Connected. HTTP/1.1 200 OK Server: nginx/1.2.6 Date: Sat, 09 Mar 2013 10:06:58 GMT Content-Type: text/html; charset=utf-8 Connection: close X-Powered-By: PHP/5.4.11--pl2-gentoo sdfsdfsf Closing Done
예제 #2 서버에서 제공하는 것으로 추정되는 UNIX 도메인 소켓에 연결하고 서버에서 응답을 읽고 콘솔에 출력합니다.
<?php
class MyUnixSocketClient {
private $base, $bev;
function __construct($base, $sock_path) {
$this->base = $base;
$this->bev = new EventBufferEvent($base, NULL, EventBufferEvent::OPT_CLOSE_ON_FREE,
array ($this, "read_cb"), NULL, array ($this, "event_cb"));
if (!$this->bev->connect("unix:$sock_path")) {
trigger_error("Failed to connect to socket `$sock_path'", E_USER_ERROR);
}
$this->bev->enable(Event::READ);
}
function __destruct() {
if ($this->bev) {
$this->bev->free();
$this->bev = NULL;
}
}
function dispatch() {
$this->base->dispatch();
}
function read_cb($bev, $unused) {
$in = $bev->input;
printf("Received %ld bytes\n", $in->length);
printf("----- data ----\n");
printf("%ld:\t%s\n", (int) $in->length, $in->pullup(-1));
$this->bev->free();
$this->bev = NULL;
$this->base->exit(NULL);
}
function event_cb($bev, $events, $unused) {
if ($events & EventBufferEvent::ERROR) {
echo "Error from bufferevent\n";
}
if ($events & (EventBufferEvent::EOF | EventBufferEvent::ERROR)) {
$bev->free();
$bev = NULL;
} elseif ($events & EventBufferEvent::CONNECTED) {
$bev->output->add("test\n");
}
}
}
if ($argc <= 1) {
exit("Socket path is not provided\n");
}
$sock_path = $argv[1];
$base = new EventBase();
$cl = new MyUnixSocketClient($base, $sock_path);
$cl->dispatch();
?>
위의 예는 다음과 유사한 결과를 출력합니다.
Received 5 bytes ----- data ---- 5: test
기타
- EventBufferEvent::connectHost() - 선택적으로 비동기 DNS 확인을 사용하여 호스트 이름에 연결