function evtcb($fd, $what, $args) { $base = $args[0]; $port = $args[1]; $bev = new EventBufferEvent($base, $fd, EventBufferEvent::OPT_CLOSE_ON_FREE, 'readcb', 'writecb', 'eventcb', $port); $bev->enable(Event::READ); }
function callback_accept($listener, $fd, $address, $base) { echo "accept running...\n"; $buffer = new EventBufferEvent($base, $fd, EventBufferEvent::OPT_CLOSE_ON_FREE); $buffer->setCallbacks('callback_read', null, 'callback_event', $base); $buffer->setTimeouts(3, 3); $buffer->enable(Event::READ); }
public function service($listener, $connectionId, $address, $id) { $buffer = new EventBufferEvent($this->base, $connectionId, EventBufferEvent::OPT_CLOSE_ON_FREE); $buffer->setCallbacks(array($this, "onRead"), array($this, "onWrite"), array($this, "onError"), $connectionId); $buffer->enable(Event::READ | Event::WRITE | Event::PERSIST); $this->services[$connectionId] = $connectionId; $this->buffers[$connectionId] = $buffer; $this->onServiceOpen($connectionId); }
function connect_dest() { global $read_timeout, $write_timeout; echo "connect dest...\n"; global $base; $buffer = new EventBufferEvent($base, NULL, EventBufferEvent::OPT_CLOSE_ON_FREE | EventBufferEvent::OPT_DEFER_CALLBACKS, 'readcb', null, 'eventcb', $base); $buffer->enable(Event::READ); $buffer->setTimeouts($read_timeout, $write_timeout); $buffer->connect('127.0.0.1:8000'); $buffer->write('client data, ' . time()); }
function callback_accept($listener, $fd, $address, $base) { echo "accept running...\n"; $buffer = new EventBufferEvent($base, $fd, EventBufferEvent::OPT_CLOSE_ON_FREE); $buffer->setCallbacks('callback_read', 'callback_write', 'callback_event', $base); $buffer->setTimeouts(3, 3); $buffer->enable(Event::READ); $data = 'server.hello'; $length = strlen($data); $buffer->write($data); echo "write output buffer, data: server.hello({$length})\n"; }
function send_data() { global $count; global $url, $port; global $base; $pid = posix_getpid(); $rand = random(); $data = time() . "-{$rand}-{$pid}-{$count}"; $bev = new EventBufferEvent($base, null, EventBufferEvent::OPT_CLOSE_ON_FREE | EventBufferEvent::OPT_DEFER_CALLBACKS); $bev->setTimeouts(3, 3); $bev->setCallbacks('readcb', null, 'eventcb', $pid); $bev->enable(Event::READ | Event::WRITE); $bev->connect("{$url}:{$port}"); $bev->write($data . "\r\n"); $base->dispatch(); lg("[{$pid}] {$data}"); //lg("pid:{$pid},count:{$count}"); exit(0); }
/** * Sets fd * @param resource $fd File descriptor * @param \EventBufferEvent $bev * @return void */ public function setFd($fd, $bev = null) { $this->fd = $fd; if ($fd === false) { $this->finish(); return; } $this->fdWrite = $this->pipes[0]; $flags = !is_resource($this->fd) ? \EventBufferEvent::OPT_CLOSE_ON_FREE : 0; $flags |= \EventBufferEvent::OPT_DEFER_CALLBACKS; // buggy option if ($this->eventLoop === null) { $this->eventLoop = EventLoop::$instance; } $this->bev = $this->eventLoop->bufferEvent($this->fd, 0, [$this, 'onReadEv'], null, [$this, 'onStateEv']); $this->bevWrite = $this->eventLoop->bufferEvent($this->fdWrite, 0, null, [$this, 'onWriteEv'], null); if (!$this->bev || !$this->bevWrite) { $this->finish(); return; } if ($this->priority !== null) { $this->bev->priority = $this->priority; } if ($this->timeout !== null) { $this->setTimeout($this->timeout); } if (!$this->bev->enable(\Event::READ | \Event::TIMEOUT | \Event::PERSIST)) { $this->finish(); return; } if (!$this->bevWrite->enable(\Event::WRITE | \Event::TIMEOUT | \Event::PERSIST)) { $this->finish(); return; } $this->bev->setWatermark(\Event::READ, $this->lowMark, $this->highMark); init: if (!$this->inited) { $this->inited = true; $this->init(); } }
/** * Service * * @return void */ public function service() { if (!$this->socket->isConnected()) { return false; } $base = new \EventBase(); $listener = new \EventListener($base, function ($listener, $fd, $address, $base) { $event = new \EventBufferEvent($base, $fd, \EventBufferEvent::OPT_CLOSE_ON_FREE); $event->setCallbacks(function ($event) { $this->protocol->handleData(new EventSocket($event)); }, function ($event) { if (0 === $event->output->length) { $event->free(); } }, function ($event, $events) { if ($events & (\EventBufferEvent::EOF | \EventBufferEvent::ERROR)) { $event->free(); } }, null); $event->enable(\Event::READ); }, $base, \EventListener::OPT_CLOSE_ON_FREE | \EventListener::OPT_REUSEABLE, -1, $this->socket->getHandle()); $base->dispatch(); }
} // 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"); } } if ($argc != 3) { echo <<<EOS Trivial HTTP 0.x client Syntax: php {$argv[0]} [hostname] [resource] Example: php {$argv[0]} www.google.com / EOS; exit; } $base = new EventBase(); $dns_base = new EventDnsBase($base, TRUE); $bev = new EventBufferEvent($base, null, EventBufferEvent::OPT_CLOSE_ON_FREE | EventBufferEvent::OPT_DEFER_CALLBACKS, "readcb", null, "eventcb", $base); $bev->enable(Event::READ | Event::WRITE); $bev->getOutput()->add("GET {$argv[2]} HTTP/1.0\r\n" . "Host: {$argv[1]}\r\n" . "Connection: Close\r\n\r\n"); $bev->connectHost($dns_base, $argv[1], 80, EventUtil::AF_UNSPEC); $base->dispatch();
<?php error_reporting(E_ALL); $base = new EventBase(); $buffer = new EventBufferEvent($base, NULL, EventBufferEvent::OPT_CLOSE_ON_FREE | EventBufferEvent::OPT_DEFER_CALLBACKS, 'readcb', 'writecb', 'eventcb', $base); $buffer->enable(Event::READ); $buffer->setTimeouts(3, 3); $buffer->connect('127.0.0.1:8000'); $base->dispatch(); function readcb($buffer, $base) { echo "read running...\n"; $data = ''; while ($read = $buffer->read(1024)) { $data .= $read; } echo 'read input buffer, data: ', $data, PHP_EOL; $body = "client.hello"; $length = strlen($body); $buffer->write($body); } function writecb($buffer, $base) { echo "write running...\n"; free($buffer, $base); echo "close client.\n"; } function eventcb($buffer, $events, $base) { echo "event running, status: {$events}\n"; if ($events & (EventBufferEvent::EOF | EventBufferEvent::ERROR)) {
/** * callback_accept * * @param mixed $listener * @param mixed $fd * @param mixed $address * @access public * @return void */ public function callback_accept($listener, $fd, $address) { list($client_ip, $port) = $address; $max_conns = $this->__max_connections; $current_conns = count($this->__buffer_event); if ($current_conns >= $max_conns) { $this->log("reach max_connections {$max_conns}, client ip: {$client_ip}, disconnect client", LOG_INFO); // 断开连接 $bev = new \EventBufferEvent($this->__event_base, $fd, \EventBufferEvent::OPT_CLOSE_ON_FREE); $bev->free(); unset($bev); return; } $current_conns++; $this->__bev_key = $this->__bev_key + 1; $bev = new \EventBufferEvent($this->__event_base, $fd, \EventBufferEvent::OPT_CLOSE_ON_FREE); $bev->setCallbacks(array($this, 'callback_buffer_read'), NULL, array($this, 'callback_buffer_event'), $this->__bev_key); $bev->setTimeouts($this->__read_timeout, $this->__write_timeout); $is_enable = $bev->enable(\Event::READ); if (!$is_enable) { $log = "can not enable EventBufferEvent, client: {$client_ip}:{$port}, current connections: {$current_conns}"; $this->log($log, LOG_INFO); return; } $this->__buffer_event[$this->__bev_key] = array($bev, $client_ip, $port); }