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"; }
protected function cmd($buffer, $id, $line) { switch ($line) { case strncmp('EHLO ', $line, 4): $this->ev_write($id, "250-STARTTLS\r\n"); $this->ev_write($id, "250 OK ehlo\r\n"); break; case strncmp('HELO ', $line, 4): $this->ev_write($id, "250-STARTTLS\r\n"); $this->ev_write($id, "250 OK helo\r\n"); break; case strncmp('QUIT', $line, 3): $this->ev_write($id, "250 OK quit\r\n"); $this->ev_close($id); break; case strncmp('STARTTLS', $line, 3): $this->ev_write($id, "220 Ready to start TLS\r\n"); $this->connections[$id]['cnx'] = EventBufferEvent::sslFilter($this->base, $this->connections[$id]['cnx'], $this->ctx, EventBufferEvent::SSL_ACCEPTING, EventBufferEvent::OPT_CLOSE_ON_FREE); $this->connections[$id]['cnx']->setCallbacks([$this, "ev_read"], NULL, [$this, 'ev_error'], $id); $this->connections[$id]['cnx']->enable(Event::READ | Event::WRITE); break; default: echo 'unknown command: ' . $line . "\n"; break; } }
/** * 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(); }
/** * Send data and appending \n to connection. Note that it just writes to buffer flushed at every baseloop * @param string Data to send * @return boolean Success */ public function writeln($data) { if (!$this->alive) { Daemon::log('Attempt to write to dead IOStream (' . get_class($this) . ')'); return false; } if (!isset($this->bevWrite)) { return false; } if (!strlen($data) && !strlen($this->EOL)) { return true; } $this->writing = true; $this->bevWrite->write($data); $this->bevWrite->write($this->EOL); return true; }
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 object $bev EventBufferEvent * @return void */ public function setFd($fd, $bev = null) { $this->fd = $fd; if ($this->fd === false) { $this->finish(); return; } if ($bev !== null) { $this->bev = $bev; $this->bev->setCallbacks([$this, 'onReadEv'], [$this, 'onWriteEv'], [$this, 'onStateEv']); if (!$this->bev) { return; } $this->ready = true; $this->alive = true; } else { $flags = !is_resource($this->fd) ? \EventBufferEvent::OPT_CLOSE_ON_FREE : 0; $flags |= \EventBufferEvent::OPT_DEFER_CALLBACKS; /* buggy option */ if ($this->ctx) { if ($this->ctx instanceof \EventSslContext) { $this->bev = \EventBufferEvent::sslSocket(Daemon::$process->eventBase, $this->fd, $this->ctx, $this->ctxMode, $flags); if ($this->bev) { $this->bev->setCallbacks([$this, 'onReadEv'], [$this, 'onWriteEv'], [$this, 'onStateEv']); } $this->ssl = true; } else { $this->log('unsupported type of context: ' . ($this->ctx ? get_class($this->ctx) : 'undefined')); return; } } else { $this->bev = new \EventBufferEvent(Daemon::$process->eventBase, $this->fd, $flags, [$this, 'onReadEv'], [$this, 'onWriteEv'], [$this, 'onStateEv']); } if (!$this->bev) { return; } } if ($this->priority !== null) { $this->bev->priority = $this->priority; } $this->setTimeouts($this->timeoutRead !== null ? $this->timeoutRead : $this->timeout, $this->timeoutWrite !== null ? $this->timeoutWrite : $this->timeout); if ($this->bevConnect && $this->fd === null) { //$this->bev->connectHost(Daemon::$process->dnsBase, $this->hostReal, $this->port); $this->bev->connect($this->addr); } if (!$this->bev) { $this->finish(); return; } if (!$this->bev->enable(\Event::READ | \Event::WRITE | \Event::TIMEOUT | \Event::PERSIST)) { $this->finish(); return; } $this->bev->setWatermark(\Event::READ, $this->lowMark, $this->highMark); init: if ($this->keepalive) { $this->setKeepalive(true); } if (!$this->inited) { $this->inited = true; $this->init(); } }
} 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); // We'll use async DNS resolving if (!$dns_base) { exit("Failed to init DNS Base\n"); } $bev = new EventBufferEvent($base, NULL, EventBufferEvent::OPT_CLOSE_ON_FREE | EventBufferEvent::OPT_DEFER_CALLBACKS, "readcb", NULL, "eventcb"); 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();
error_reporting(E_ALL); $curr_stat = -1; $sub_stat = []; $stat = ['STAT_READY' => 1, 'STAT_AUTH' => 2, 'STAT_USER' => 3, 'STAT_PASSWD' => 4, 'STAT_FROM' => 5, 'STAT_TO' => 6, 'STAT_DATA' => 7, 'STAT_MAIL' => 8, 'STAT_QUIT' => 9, 'STAT_END' => 10, 'STAT_INIT' => -1]; $CRLF = "\r\n"; $timeout = 3; $resp = ''; $args = ['host' => "mail.eyou.net:465", 'passwd' => "eYouGaoJing!nb", 'email' => "*****@*****.**"]; $fd = stream_socket_client($args['host'], $errno, $errstr, 3, STREAM_CLIENT_CONNECT | STREAM_CLIENT_ASYNC_CONNECT); $opt = EventBufferEvent::OPT_CLOSE_ON_FREE | EventBufferEvent::OPT_DEFER_CALLBACKS; $base = new EventBase(); if (false !== strpos($args['host'], '465')) { $ctx = new EventSslContext(EventSslContext::SSLv3_CLIENT_METHOD, []); $bev = EventBufferEvent::sslSocket($base, $fd, $ctx, EventBufferEvent::SSL_CONNECTING, $opt); } else { $bev = new EventBufferEvent($base, $fd, $opt); } $bev->setCallbacks('readcb', null, null, $base); $bev->enable(Event::READ | Event::WRITE); $bev->setTimeouts($timeout, $timeout); $base->dispatch(); $i = 0; function readcb($bev, $base) { global $curr_stat, $stat, $sub_stat, $resp, $args; global $i; $resp = ''; while (null !== ($msg = $bev->read(512))) { $resp .= $msg; } $resp = trim($resp);
<?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); }
/** * @param array ...$args * @return mixed */ public function bufferEventSsl(...$args) { return \EventBufferEvent::sslSocket($this->base, ...$args); }
} 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, NULL, EventBufferEvent::OPT_CLOSE_ON_FREE | EventBufferEvent::OPT_DEFER_CALLBACKS, "readcb", 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 HTTP/1.0\r\n")) { exit("Failed adding request to output buffer\n"); } // mole.eyousop.com:8538 if (!$bev->connectHost($dns_base, 'mole.eyousop.com', 8538, EventUtil::AF_UNSPEC)) { exit("Can't connect to host {$argv[1]}\n"); } $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();
} // Event callback function eventcb($bev, $stat, $base) { var_dump('status: ' . $stat); if ($stat & EventBufferEvent::CONNECTED) { echo "Connected.\n"; } elseif ($stat & (EventBufferEvent::ERROR | EventBufferEvent::EOF)) { if ($stat & EventBufferEvent::ERROR) { echo "DNS error: ", $bev->getDnsErrorString(), PHP_EOL; } echo "Closing\n"; $base->exit(); exit("Done\n"); } elseif ($stat & EventBufferEvent::TIMEOUT) { echo "Timeout\n"; } } function writecb($bev, $base) { echo "writecb\r\n"; $bev->output->add(microtime(true) . "\r\n"); } $base = new EventBase(); $bev = new EventBufferEvent($base, null, EventBufferEvent::OPT_CLOSE_ON_FREE | EventBufferEvent::TIMEOUT, "readcb", null, "eventcb", $base); //"readcb", "writecb", "eventcb", $base); $bev->enable(Event::READ | Event::WRITE); $bev->getOutput()->add(microtime(true) . "\r\n"); $bev->setTimeouts(2, 2); $bev->connect('127.0.0.1:8000'); $base->dispatch();