Ejemplo n.º 1
0
 public function onConnectedEvent($connSocket, $events, $arg)
 {
     $connId = $arg[0];
     Debug::netEvent(get_class($this) . '::' . __METHOD__ . '(' . $connId . ') invoked. ');
     //处理两种状态,一种是直接连接成功,一种是异步通知
     if (isset($this->checkConnEvPool[$connId])) {
         // 异步通知
         // 因为 注册 EV_WRITE 事件是非持久模式的,所以这里不用 delete, 只需要 unset pool 即可
         unset($this->checkConnSocketPool[$connId]);
         unset($this->checkConnEvPool[$connId]);
     }
     $evBuf = event_buffer_new($connSocket, array($this, 'onEvBufReadEvent'), array($this, 'onEvBufWriteEvent'), array($this, 'onEventEvent'), array($connId));
     event_buffer_base_set($evBuf, daemon::$eventBase);
     event_buffer_priority_set($evBuf, 10);
     event_buffer_watermark_set($evBuf, EV_READ, $this->evBufLowMark, $this->evBufHighMark);
     if (!event_buffer_enable($evBuf, EV_READ | EV_WRITE | EV_PERSIST)) {
         Debug::netErrorEvent(get_class($this) . '::' . __METHOD__ . ': can not set base of buffer. #' . $connId);
         //            socket_close($connSocket);
         fclose($connSocket);
         return;
     }
     // 调试这里时,浪费了很多时间,必须注意的是,以上 event 所用的变量如果在函数中,如果没有交给其他的变量引用,在函数结束时就会销毁,
     // 造成连接直接断或者bufferevent 不能触发。晕啊晕。
     $this->connSocketPool[$connId] = $connSocket;
     $this->connEvBufPool[$connId] = $evBuf;
     list($ip, $port) = explode(':', stream_socket_get_name($connSocket, true));
     $this->updateLastContact($connId);
     $this->onConnected($connId, $ip, $port);
 }
Ejemplo n.º 2
0
 public function onAcceptEvent($bindSocket, $events, $arg)
 {
     $bindSocketId = $arg[0];
     Debug::netEvent(get_class($this) . '::' . __METHOD__ . '(' . $bindSocketId . ') invoked.');
     // add to accept next event
     // why not use EV_PERSIST
     event_add($this->bindSocketEvPool[$bindSocketId]);
     $connSocket = stream_socket_accept($this->bindSocketPool[$bindSocketId]);
     if (!$connSocket) {
         Debug::netErrorEvent(get_class($this) . ': can not accept new TCP-socket');
         return;
     }
     stream_set_blocking($connSocket, 0);
     list($ip, $port) = explode(':', stream_socket_get_name($connSocket, true));
     $connId = daemon::getNextConnId();
     $evBuf = event_buffer_new($connSocket, array($this, 'onEvBufReadEvent'), array($this, 'onEvBufWriteEvent'), array($this, 'onEventEvent'), array($connId));
     event_buffer_base_set($evBuf, daemon::$eventBase);
     event_buffer_priority_set($evBuf, 10);
     event_buffer_watermark_set($evBuf, EV_READ, $this->evBufLowMark, $this->evBufHighMark);
     if (!event_buffer_enable($evBuf, EV_READ | EV_WRITE | EV_PERSIST)) {
         Debug::netErrorEvent(get_class($this) . '::' . __METHOD__ . ': can not set base of buffer. #' . $connId);
         //close socket
         stream_socket_shutdown($connSocket, STREAM_SHUT_RDWR);
         fclose($connSocket);
         return;
     }
     // 调试这里时,浪费了很多时间,必须注意的是,以上 event 所用的变量如果在函数中,如果没有交给其他的变量引用,在函数结束时就会销毁,
     // 造成连接直接断或者bufferevent 不能触发。晕啊晕。
     $this->connSocketPool[$connId] = $connSocket;
     $this->connEvBufPool[$connId] = $evBuf;
     $this->updateLastContact($connId);
     $this->onAccepted($connId, $ip, $port);
 }
Ejemplo n.º 3
0
 /**
  * Initialize a new stream listener
  */
 public function __construct($stream)
 {
     $this->stream = $stream;
     stream_set_blocking($this->stream, 0);
     $this->event = event_buffer_new($this->stream, array($this, '_read'), array($this, '_write'), array($this, '_error'));
     Loop::attachBuffer($this);
     event_buffer_timeout_set($this->event, 2, 5);
     event_buffer_watermark_set($this->event, EV_READ, 0, 0xffffff);
     event_buffer_priority_set($this->event, 10);
     event_buffer_enable($this->event, EV_READ | EV_PERSIST);
 }
 private function accept($socket, $flag, $base)
 {
     $connection = @stream_socket_accept($socket, 0);
     $connectionId = $this->getIdByConnection($connection);
     stream_set_blocking($connection, 0);
     $buffer = event_buffer_new($connection, array($this, 'onRead'), array($this, 'onWrite'), array($this, 'onError'), $connectionId);
     event_buffer_base_set($buffer, $this->base);
     event_buffer_watermark_set($buffer, EV_READ, 0, 0xffffff);
     event_buffer_priority_set($buffer, 10);
     event_buffer_enable($buffer, EV_READ | EV_WRITE | EV_PERSIST);
     $this->clients[$connectionId] = $connection;
     $this->buffers[$connectionId] = $buffer;
     $this->_onOpen($connectionId);
 }
Ejemplo n.º 5
0
 protected function addClient($socket, $flag, $base)
 {
     $connection = stream_socket_accept($socket);
     stream_set_blocking($connection, 0);
     $id = md5(time() . rand() . $flag);
     $client = new $this->client_type($this, $id, $connection);
     $buffer = event_buffer_new($connection, [$this, 'read'], NULL, [$this, 'error'], $client);
     event_buffer_base_set($buffer, $base);
     event_buffer_timeout_set($buffer, self::CONNECTION_TIMEOUT, self::CONNECTION_TIMEOUT);
     event_buffer_watermark_set($buffer, EV_READ, 0, 0xffffff);
     event_buffer_priority_set($buffer, self::EVENT_PRIORITY);
     event_buffer_enable($buffer, EV_READ | EV_PERSIST);
     $client->setBuffer($buffer);
 }
Ejemplo n.º 6
0
 public function accept($socket, $flag, $base)
 {
     Logger::getInstance()->outDebug("Try to accept new connection.");
     $this->id++;
     $connection = stream_socket_accept($socket);
     stream_set_blocking($connection, 0);
     $buffer = event_buffer_new($connection, array($this, 'onRead'), NULL, array($this, 'onError'), $this->id);
     event_buffer_base_set($buffer, $this->base);
     event_buffer_timeout_set($buffer, 180, 180);
     event_buffer_watermark_set($buffer, EV_READ, 0, 0xffffff);
     event_buffer_priority_set($buffer, 10);
     event_buffer_enable($buffer, EV_READ | EV_PERSIST);
     $this->connections[$this->id] = $connection;
     $this->buffers[$this->id] = $buffer;
 }
Ejemplo n.º 7
0
function ev_accept($socket, $flag, $base)
{
    static $id = 0;
    $connection = stream_socket_accept($socket);
    stream_set_blocking($connection, 0);
    $id += 1;
    $buffer = event_buffer_new($connection, 'ev_read', NULL, 'ev_error', $id);
    event_buffer_base_set($buffer, $base);
    event_buffer_timeout_set($buffer, 30, 30);
    event_buffer_watermark_set($buffer, EV_READ, 0, 0xffffff);
    event_buffer_priority_set($buffer, 10);
    event_buffer_enable($buffer, EV_READ | EV_PERSIST);
    // we need to save both buffer and connection outside
    $GLOBALS['connections'][$id] = $connection;
    $GLOBALS['buffers'][$id] = $buffer;
}
Ejemplo n.º 8
0
 function ev_accept($socket, $flag, $base)
 {
     static $id = 0;
     $connection = stream_socket_accept($socket);
     stream_set_blocking($connection, 0);
     $id++;
     // increase on each accept
     $buffer = event_buffer_new($connection, array(__CLASS__, 'ev_read'), array(__CLASS__, 'ev_write'), array(__CLASS__, 'ev_error'), $id);
     event_buffer_base_set($buffer, $base);
     event_buffer_timeout_set($buffer, 30, 30);
     event_buffer_watermark_set($buffer, EV_READ, 0, 0xffffff);
     event_buffer_priority_set($buffer, 10);
     event_buffer_enable($buffer, EV_READ | EV_PERSIST);
     // we need to save both buffer and connection outside
     self::$connections[$id] = $connection;
     self::$buffers[$id] = $buffer;
 }
 public function ev_accept($socket, $flag, $base)
 {
     static $id = 0;
     // stream_socket_accept — 接受由 stream_socket_server() 创建的套接字连接
     $connection = stream_socket_accept($socket);
     stream_set_blocking($connection, 0);
     $id++;
     // event_buffer_new — Create new buffered event
     $buffer = event_buffer_new($connection, [__CLASS__, 'ev_read'], [__CLASS__, 'ev_write'], [__CLASS__, 'ev_error'], $id);
     // event_buffer_base_set — Associate buffered event with an event base
     event_buffer_base_set($buffer, $base);
     // event_buffer_timeout_set — Set read and write timeouts for a buffered event
     event_buffer_timeout_set($buffer, 30, 30);
     // event_buffer_watermark_set — Set the watermarks for read and write events
     event_buffer_watermark_set($buffer, EV_READ, 0, 0xffffff);
     // event_buffer_priority_set — Assign a priority to a buffered event
     event_buffer_priority_set($buffer, 10);
     // event_buffer_enable — Enable a buffered event
     event_buffer_enable($buffer, EV_READ | EV_PERSIST);
     self::$connections[$id] = $connection;
     self::$buffers[$id] = $buffer;
 }
Ejemplo n.º 10
0
 public function setPriority($p)
 {
     $this->priority = $p;
     if ($this->buffer !== null) {
         event_buffer_priority_set($this->buffer, $p);
     }
     if ($this->event !== null) {
         event_priority_set($this->event, $p);
     }
 }
Ejemplo n.º 11
0
 public function onAcceptEvent($stream, $events, $arg)
 {
     $sockId = $arg[0];
     if (Daemon::$settings['logevents']) {
         Daemon::log('[WORKER ' . Daemon::$worker->pid . '] ' . get_class($this) . '::' . __METHOD__ . '(' . $sockId . ') invoked.');
     }
     if ($this->checkAccept()) {
         Daemon::$worker->addEvent($this->socketEvents[$sockId]);
     }
     if (Daemon::$useSockets) {
         $conn = @socket_accept($stream);
         if (!$conn) {
             return;
         }
         socket_set_nonblock($conn);
         if (Daemon::$sockets[$sockId][1] === 2) {
             $addr = '';
         } else {
             socket_getpeername($conn, $host, $port);
             $addr = $host === '' ? '' : $host . ':' . $port;
         }
     } else {
         $conn = @stream_socket_accept($stream, 0, $addr);
         if (!$conn) {
             return;
         }
         stream_set_blocking($conn, 0);
     }
     if (!$this->onAccept(Daemon::$worker->connCounter + 1, $addr)) {
         Daemon::log('Connection is not allowed (' . $addr . ')');
         if (Daemon::$useSockets) {
             socket_close($conn);
         } else {
             fclose($conn);
         }
         return;
     }
     $connId = ++Daemon::$worker->connCounter;
     Daemon::$worker->pool[$connId] = $conn;
     Daemon::$worker->poolApp[$connId] = $this;
     $this->poolQueue[$connId] = array();
     $this->poolState[$connId] = array();
     if ($this->directReads) {
         $ev = event_new();
         if (!event_set($ev, Daemon::$worker->pool[$connId], EV_READ | EV_PERSIST, array($this, 'onReadEvent'), $connId)) {
             Daemon::log(get_class($this) . '::' . __METHOD__ . ': Couldn\'t set event on accepted socket #' . $connId);
             return;
         }
         event_base_set($ev, Daemon::$worker->eventBase);
         event_add($ev);
         $this->readEvents[$connId] = $ev;
     }
     $buf = event_buffer_new(Daemon::$worker->pool[$connId], $this->directReads ? NULL : array($this, 'onReadEvent'), array($this, 'onWriteEvent'), array($this, 'onFailureEvent'), array($connId));
     if (!event_buffer_base_set($buf, Daemon::$worker->eventBase)) {
         throw new Exception('Couldn\'t set base of buffer.');
     }
     event_buffer_priority_set($buf, 10);
     event_buffer_watermark_set($buf, EV_READ, $this->initialLowMark, $this->initialHighMark);
     event_buffer_enable($buf, $this->directReads ? EV_WRITE | EV_PERSIST : EV_READ | EV_WRITE | EV_PERSIST);
     $this->buf[$connId] = $buf;
     $this->onAccepted($connId, $addr);
 }
Ejemplo n.º 12
0
 private function initiate_handshake($conn)
 {
     $buffer = event_buffer_new($conn, [$this, 'attempt_handshake'], null, [$this, 'errorcb'], $conn);
     event_buffer_base_set($buffer, $this->ev_base);
     event_buffer_timeout_set($buffer, 10, null);
     event_buffer_watermark_set($buffer, EV_READ, 100, 1024);
     event_buffer_priority_set($buffer, 1);
     event_buffer_enable($buffer, EVLOOP_ONCE | EV_READ);
     $this->connections[(int) $conn] = $conn;
     $this->unauthorized[(int) $conn] = $buffer;
 }
Ejemplo n.º 13
0
 private function master($connection, $flag, $base)
 {
     $connectionId = $this->getIdByConnection($connection);
     $buffer = event_buffer_new($connection, array($this, 'onRead'), array($this, 'onWrite'), array($this, 'onError'), $connectionId);
     event_buffer_base_set($buffer, $this->base);
     event_buffer_watermark_set($buffer, EV_READ, 0, 0xffffff);
     event_buffer_priority_set($buffer, 10);
     event_buffer_enable($buffer, EV_READ | EV_WRITE | EV_PERSIST);
     $this->buffers[$connectionId] = $buffer;
     event_del($this->master_event);
     event_free($this->master_event);
     unset($this->master_event);
 }
Ejemplo n.º 14
0
 public function ev_accept($socket, $flag, $argument)
 {
     $this->log("ev_accept(..., {$flag}, ...)");
     $to = ini_get("default_socket_timeout");
     $connection = stream_socket_accept($socket, $to, $peer);
     stream_set_blocking($connection, 0);
     $readCb = array($this, 'ev_read');
     $writeCb = array($this, 'ev_write');
     $errCb = array($this, 'ev_error');
     $id = uniqid();
     $buffer = event_buffer_new($connection, $readCb, $writeCb, $errCb, $id);
     event_buffer_base_set($buffer, $this->base);
     event_buffer_timeout_set($buffer, self::TIMEOUT_SECONDS, self::TIMEOUT_SECONDS);
     event_buffer_watermark_set($buffer, EV_READ, 0, 0xffffff);
     event_buffer_priority_set($buffer, 10);
     event_buffer_enable($buffer, EV_READ | EV_PERSIST);
     $this->fire('connection', $connection);
     $request = new ServerRequest($this->base);
     $request->client_id = $id;
     $request->server = $this;
     $request->connection = $connection;
     $request->ev_buffer = $buffer;
     $request->peer = $peer;
     $response = new ServerResponse($request);
     $response->client_id = $id;
     $response->server = $this;
     $response->connection = $connection;
     $response->ev_buffer = $buffer;
     $this->clients[$id] = array('socket' => $connection, 'buffer' => $buffer, 'request' => $request, 'response' => $response, 'disconnect' => false);
 }
Ejemplo n.º 15
0
 /**
  * Sets the priority of this event
  *
  * This should somehow correlate with the priority range set in
  * \Fruux\Event\Base. If you don't set a priority, (priorities/2) is used.
  *
  * @param int $priority
  * @return void
  */
 public function setPriority($priority)
 {
     event_buffer_priority_set($this->resource, $priority);
 }
Ejemplo n.º 16
0
/**
 * Handle new connection events. Add new clients to the list. The server will write a welcome message to each client
 * Sets the following functions to handle I/O events
 * 'ev_read()', 'ev_write()', 'ev_error()'
 *
 * @param $socket resource
 * @param $flag   int A flag indicating the event. Consists of the following flags: EV_TIMEOUT, EV_SIGNAL, EV_READ, EV_WRITE and EV_PERSIST.
 * @param $base   resource created by event_base_new()
 */
function ev_accept($socket, $flag, $base)
{
    global $clients;
    static $next_id = 0;
    $connection = stream_socket_accept($socket);
    stream_set_blocking($connection, 0);
    $next_id++;
    $buffer = event_buffer_new($connection, 'ev_read', 'ev_write', 'ev_error', $next_id);
    event_buffer_base_set($buffer, $base);
    event_buffer_timeout_set($buffer, GSMTP_TIMEOUT, GSMTP_TIMEOUT);
    event_buffer_watermark_set($buffer, EV_READ, 0, 0xffffff);
    event_buffer_priority_set($buffer, 10);
    event_buffer_enable($buffer, EV_READ | EV_PERSIST);
    $clients[$next_id]['socket'] = $connection;
    // new socket
    $clients[$next_id]['ev_buffer'] = $buffer;
    // new socket
    $clients[$next_id]['state'] = 0;
    $clients[$next_id]['mail_from'] = '';
    $clients[$next_id]['helo'] = '';
    $clients[$next_id]['rcpt_to'] = '';
    $clients[$next_id]['error_c'] = 0;
    $clients[$next_id]['read_buffer'] = '';
    $clients[$next_id]['read_buffer_ready'] = false;
    // true if the buffer is ready to be fetched
    $clients[$next_id]['response'] = '';
    // response messages are placed here, before they go on the write buffer
    $clients[$next_id]['time'] = time();
    $address = stream_socket_get_name($clients[$next_id]['socket'], true);
    $clients[$next_id]['address'] = $address;
    process_smtp($next_id);
    if (strlen($clients[$next_id]['response']) > 0) {
        event_buffer_write($buffer, $clients[$next_id]['response']);
        add_response($next_id, null);
    }
}
Ejemplo n.º 17
0
 /**
  * Assign a priority to a buffered event.
  *
  * @see event_buffer_priority_set
  *
  * @param int $value
  * Priority level. Cannot be less than zero and cannot exceed
  * maximum priority level of the event base (see {@link event_base_priority_init}()).
  * 
  *
  * @throws EventException
  *
  * @return EventBuffer
  */
 public function setPriority($value = self::DEFAULT_PRIORITY)
 {
     if (!event_buffer_priority_set($this->resource, $value)) {
         throw $this->exception('Could not set buffered event priority (event_buffer_priority_set).');
     }
     return $this;
 }
Ejemplo n.º 18
0
 public function setPriority($read, $write)
 {
     $this->readPriority = $read;
     $this->writePriority = $write;
     if ($this->readBuf !== NULL) {
         if (event_buffer_priority_set($this->readBuf, $this->readPriority) === FALSE) {
             throw new Exception('setting priority for read buffer failed');
         }
     }
     if ($this->writeBuf !== NULL) {
         if (event_buffer_priority_set($this->writeBuf, $this->writePriority) === FALSE) {
             throw new Exception('setting priority for read buffer failed');
         }
     }
     return $this;
 }