Ejemplo n.º 1
0
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);
}
Ejemplo n.º 2
0
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());
}
Ejemplo n.º 3
0
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";
}
Ejemplo n.º 4
0
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);
}
Ejemplo n.º 5
0
<?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)) {
Ejemplo n.º 6
0
 /**
  * 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);
 }
Ejemplo n.º 7
0
$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) {
        //exit;
    }
Ejemplo n.º 8
0
}
// 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();