EventBufferEvent::connectHost
EventBufferEvent::connectHost
(PECL event >= 1.2.6-beta)
EventBufferEvent :: connectHost - 通过可选的异步 DNS 解析连接到主机名
描述
public bool EventBufferEvent::connectHost ( EventDnsBase $dns_base , string $hostname , int $port [, int $family = EventUtil::AF_UNSPEC ] )
解析 DNS 名称主机名,查找类型family
(EventUtil :: AF_ *
常量)的地址。如果名称解析失败,它会调用带有错误事件的事件回调。如果成功,它会像 EventBufferEvent :: connect()那样启动连接尝试。
dns_base
是可选的。可能是NULL
,或用 EventDnsBase :: __ construct()创建的对象。对于异步主机名解析传递有效的事件 dns 基础资源。否则,主机名解析会阻塞。
注意
:EventDnsBase 仅在事件配置为--with-event-extra
(event_extra库,libevent 协议特定的功能支持包括HTTP,DNS 和 RPC)时可用。
注意
:EventBufferEvent :: connectHost()
需要 libevent-2.0.3-alpha 或更高版本。
参数
dns_base
如果 DNS 要异步解析,则使用 EventDnsBase 对象。否则NULL
。
hostname
连接到的主机名。可识别的格式是:
www.example.com (hostname)
1.2.3.4 (ipv4address)
::1 (ipv6address)
[::1] ([ipv6address])
port
端口号
family
地址家庭。EventUtil::AF_UNSPEC
,EventUtil::AF_INET
或EventUtil::AF_INET6
。请参阅 EventUtil 常量。
返回值
TRUE
成功返回。否则FALSE
。
例子
示例#1 EventBufferEvent :: connectHost()示例
<?php
/* Read callback */
function readcb($bev, $base) {
//$input = $bev->input; //$bev->getInput(
//$pos = $input->search("TTP"
$pos = $bev->input->search("TTP"
while (($n = $bev->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(
$dns_base = new EventDnsBase($base, TRUE // We'll use async DNS resolving
if (!$dns_base) {
exit("Failed to init DNS Base\n"
}
$bev = new EventBufferEvent($base, /* use internal socket */ NULL,
EventBufferEvent::OPT_CLOSE_ON_FREE | EventBufferEvent::OPT_DEFER_CALLBACKS,
"readcb", /* writecb */ NULL, "eventcb", $base
if (!$bev) {
exit("Failed creating bufferevent socket\n"
}
//$bev->setCallbacks("readcb", /* writecb */ NULL, "eventcb", $base
$bev->enable(Event::READ | Event::WRITE
$output = $bev->output; //$bev->getOutput(
if (!$output->add(
"GET {$argv[2]} HTTP/1.0\r\n".
"Host: {$argv[1]}\r\n".
"Connection: Close\r\n\r\n"
)) {
exit("Failed adding request to output buffer\n"
}
if (!$bev->connectHost($dns_base, $argv[1], 80, EventUtil::AF_UNSPEC)) {
exit("Can't connect to host {$argv[1]}\n"
}
$base->dispatch(
?>
上面的例子会输出类似于:
Connected.
HTTP/1.0 301 Moved Permanently
Location: http://www.google.co.uk/
Content-Type: text/html; charset=UTF-8
Date: Sat, 09 Mar 2013 12:21:19 GMT
Expires: Mon, 08 Apr 2013 12:21:19 GMT
Cache-Control: public, max-age=2592000
Server: gws
Content-Length: 221
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.co.uk/">here</A>.
</BODY></HTML>
Closing
Done
也可以看看
- EventBufferEvent :: connect() - 将缓冲区事件的文件描述符连接到给定的地址或 UNIX 套接字
← EventBufferEvent::connect
EventBufferEvent::__construct →