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 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); }
/** * 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(); }
/** * 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); }
$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); _lg("r: {$resp}" . PHP_EOL); if (++$i > 2) {