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); }
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); }
/** * 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); }
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); }
/** * Creates a new buffered event resource. * * @see event_buffer_new * @link http://www.php.net/manual/en/function.event-buffer-new.php * * @param resource $stream Valid PHP stream resource. Must be castable to file descriptor * @param callable|null $readcb Callback to invoke where there is data to read, or NULL if no callback is desired * @param callable|null $writecb Callback to invoke where the descriptor is ready for writing, or NULL if no callback is desired * @param callable $errorcb Callback to invoke where there is an error on the descriptor, cannot be NULL * @param array $arguments An arguments that will be passed to each of the callbacks * * @throws EventException|\InvalidArgumentException * * @return EventBuffer */ public function prepare($stream, $readcb = null, $writecb = null, $errorcb = null, array $arguments = array()) { if (!is_resource($stream)) { throw new \InvalidArgumentException('Could not prepare buffered event. Invalid fd.'); } if (false === ($this->resource = event_buffer_new($stream, $readcb, $writecb, $errorcb, $this))) { throw $this->exception('Could not create new buffered event resourse (event_buffer_new).'); } if (!event_buffer_base_set($this->resource, $this->base->getResource())) { throw $this->exception('Could not set buffered event base (event_buffer_base_set).'); } $this->arguments = $arguments; $this->base->registerEvent($this); return $this; }
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; }
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; }
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; }
public function setFD($readFD, $writeFD = NULL) { $this->readFD = $readFD; $this->writeFD = $writeFD; if (!is_resource($this->readFD)) { throw new BadStreamDescriptorException('wrong readFD', 1); } if ($this->readBuf === NULL) { if (!stream_set_blocking($this->readFD, 0)) { throw new Exception('setting blocking for read stream failed'); } $this->readBuf = event_buffer_new($this->readFD, array($this, 'onReadEvent'), array($this, 'onWriteEvent'), array($this, 'onReadFailureEvent'), array()); if (!$this->readBuf) { throw new Exception('creating read buffer failed'); } if (!event_buffer_base_set($this->readBuf, Daemon::$worker->eventBase)) { throw new Exception('wrong base'); } if (event_buffer_priority_set($this->readBuf, $this->readPriority) === FALSE && FALSE) { throw new Exception('setting priority for read buffer failed'); } } else { if (!stream_set_blocking($this->readFD, 0)) { throw new Exception('setting blocking for read stream failed'); } if (!event_buffer_fd_set($this->readBuf, $this->readFD)) { throw new Exception('setting descriptor for write buffer failed'); } } if ($this->writeFD === NULL) { return $this; } if (!is_resource($this->writeFD)) { throw new BadStreamDescriptorException('wrong writeFD', 1); } if ($this->writeBuf === NULL) { if (!stream_set_blocking($this->writeFD, 0)) { throw new Exception('setting blocking for write stream failed'); } $this->writeBuf = event_buffer_new($this->writeFD, NULL, array($this, 'onWriteEvent'), array($this, 'onWriteFailureEvent'), array()); if (!$this->writeBuf) { throw new Exception('creating write buffer failed'); } if (!event_buffer_base_set($this->writeBuf, Daemon::$worker->eventBase)) { throw new Exception('wrong base'); } if (event_buffer_priority_set($this->writeBuf, $this->writePriority) === FALSE && FALSE) { throw new Exception('setting priority for write buffer failed'); } } else { stream_set_blocking($this->writeFD, 0); event_buffer_fd_set($this->buf, $this->writeFD); } return $this; }
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); }
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; }
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); }
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); }
$i++; if ($i == 10) { event_base_loopexit($arg[1]); } var_dump(fread($fd, 1000)); } $base = event_base_new(); $event = event_new(); $fd = STDIN; var_dump(event_set($event, $fd, EV_READ | EV_PERSIST, "foo", array($event, $base))); var_dump(event_set($event, $fd, EV_READ | EV_PERSIST, "foo", array($event, $base))); event_base_set($event, $base); var_dump(event_add($event)); var_dump(event_base_loop($base)); exit; /* poll STDIN using event_buffer API */ function foo2($buf, $arg) { static $i; $i++; if ($i == 10) { event_base_loopexit($arg); } var_dump($buf); var_dump(event_buffer_read($buf, 10)); } $base = event_base_new(); $b = event_buffer_new(STDIN, "foo2", NULL, "foo2", $base); event_buffer_base_set($b, $base); event_buffer_enable($b, EV_READ); event_base_loop($base);
/** * Creates the buffered event * * @param resource $stream You must pass an open stream * @param int $operations Specify self::READ / self::WRITE to handle these * events. */ public function __construct($stream, $operations) { $self = $this; $this->resource = event_buffer_new($stream, function () use($self) { if ($self->onRead) { call_user_func($self->onRead, $self); } }, function () use($self) { if ($self->onWrite) { call_user_func($self->onWrite, $this); } }, function ($discard, $errorCode) use($self) { if ($errorCode & EVBUFFER_EOF) { if ($self->onEOF) { call_user_func($self->onEOF, $self); } } elseif ($errorCode & EVBUFFER_TIMEOUT) { if ($self->onTimeout) { call_user_func($self->onTimeout, $self, $errorCode & (Buffer::READ || Buffer::WRITE)); } } elseif ($self->onError) { call_user_func($self->onError, $self, $errorCode); } }); $this->operations = $operations; }
public function setFd($fd) { $this->fd = $fd; if ($this->directInput || $this->directOutput) { $ev = event_new(); $flags = 0; if ($this->directInput) { $flags |= EV_READ; } if ($this->directOutput) { $flags |= EV_WRITE; } if ($this->timeout !== null) { $flags |= EV_TIMEOUT; } event_set($ev, $this->fd, $flags | EV_PERSIST, array($this, 'onDirectEvent')); event_base_set($ev, Daemon::$process->eventBase); if ($this->priority !== null) { event_priority_set($ev, $this->priority); } if ($this->timeout !== null) { event_add($ev, 1000000.0 * $this->timeout); } else { event_add($ev); } $this->event = $ev; } if (!$this->directOutput || !$this->directOutput) { $this->buffer = event_buffer_new($this->fd, $this->directInput ? null : array($this, 'onReadEvent'), $this->directOutput ? null : array($this, 'onWriteEvent'), array($this, 'onFailureEvent')); event_buffer_base_set($this->buffer, Daemon::$process->eventBase); if ($this->priority !== null) { event_buffer_priority_set($this->buffer, $this->priority); } if ($this->timeout) { event_buffer_timeout_set($this->buffer, $this->timeout, $this->timeout); } if (!$this->directInput) { event_buffer_watermark_set($this->buffer, EV_READ, $this->lowMark, $this->highMark); } event_buffer_enable($this->buffer, $this->directInput ? EV_WRITE | EV_TIMEOUT | EV_PERSIST : EV_READ | EV_WRITE | EV_TIMEOUT | EV_PERSIST); } if (!$this->inited) { $this->inited = true; $this->init(); } }
/** * Obsluga nowych polaczen * @param resource (stream) $pSock socket wykorzystywany do komunikacji * @param int $pFlag flaga okreslajaca zdarzenie * {EV_TIMEOUT, EV_SIGNAL, EV_READ, EV_WRITE, EV_PERSIST} * @param resource (event base) $pBase zdarzenie bazowe * @return boolean czy obsluzono nowe polaczenie */ private function onConnection($pSock, $pFlag, $pBase) { // jesli za duzo polaczen, czekamy az beda jakies dostepne if (count($this->connections) == $this->maxConnections) { return false; } // unikalny identyfikator polaczenia static $connection_id = 0; $connection_id++; $sock = stream_socket_accept($pSock); stream_set_blocking($sock, 0); $buffer = event_buffer_new($sock, [$this, 'onRead'], null, [$this, 'onError'], $connection_id); event_buffer_base_set($buffer, $pBase); if ($this->timeout > 0) { event_buffer_timeout_set($buffer, $this->timeout, $this->timeout); } event_buffer_watermark_set($buffer, EV_READ, 0, 0xffffff); event_buffer_enable($buffer, EV_READ | EV_PERSIST); $connection = new DaemonConnection($connection_id, $sock); $connection->setEventBuffer($buffer); $this->setConnection($connection); $this->log("New connection (id = " . $connection_id . ")..."); return true; }
private function make_offer() { $this->offer_socket = socket_create(AF_INET, SOCK_STREAM, 0); //socket_set_nonblock( $this->offer_socket ); // Maybe connect async (change callbacks, add write_offer) socket_connect($this->offer_socket, $this->offer_address, $this->offer_port); $this->offer_event = event_buffer_new($this->offer_socket, null, array($this, 'write_offer_success'), array($this, 'write_offer_error'), null); event_buffer_watermark_set($this->offer_event, EV_WRITE, 0, 0); event_buffer_base_set($this->offer_event, $this->event_base); event_buffer_enable($this->offer_event, EV_WRITE); $offer = pack('N', $this->worker_pid); event_buffer_write($this->offer_event, $offer); }
/** * 添加事件 * @see BaseEvent::add() */ public function add($fd, $flag, $func, $args = null, $read_timeout = 1000, $write_timeout = 1000) { $event_key = (int) $fd; $real_flag = EV_READ | EV_PERSIST; switch ($flag) { // 链接事件 case self::EV_ACCEPT: break; // 数据可读事件 // 数据可读事件 case self::EV_READ: $this->eventBuffer[$event_key] = event_buffer_new($fd, array($this, 'bufferCallBack'), NULL, array($this, 'bufferCallBackErr'), array('args' => $args, 'func' => $func, 'fd' => $fd)); event_buffer_base_set($this->eventBuffer[$event_key], $this->eventBase); event_buffer_timeout_set($this->eventBuffer[$event_key], ceil($read_timeout / 1000), ceil($write_timeout / 1000)); event_buffer_watermark_set($this->eventBuffer[$event_key], EV_READ, 0, 0xffffff); // event_buffer_priority_set($this->eventBuffer[$event_key], 10); event_buffer_enable($this->eventBuffer[$event_key], EV_READ | EV_PERSIST); return true; // 数据可写事件,这个事件目前没有用到 // 数据可写事件,这个事件目前没有用到 case self::EV_WRITE: $real_flag = EV_WRITE | EV_PERSIST; break; // 信号监听事件 // 信号监听事件 case self::EV_SIGNAL: $real_flag = EV_SIGNAL | EV_PERSIST; // 创建一个用于监听的event $this->eventSignal[$event_key] = event_new(); // 设置监听处理函数 if (!event_set($this->eventSignal[$event_key], $fd, $real_flag, $func, $args)) { return false; } // 设置event base if (!event_base_set($this->eventSignal[$event_key], $this->eventBase)) { return false; } // 添加事件 if (!event_add($this->eventSignal[$event_key])) { return false; } return true; break; // 监控文件描述符可读事件 // 监控文件描述符可读事件 case self::EV_NOINOTIFY: $real_flag = EV_READ | EV_PERSIST; break; } // 创建一个用于监听的event $this->allEvents[$event_key][$flag] = event_new(); // 设置监听处理函数 if (!event_set($this->allEvents[$event_key][$flag], $fd, $real_flag, $func, $args)) { return false; } // 设置event base if (!event_base_set($this->allEvents[$event_key][$flag], $this->eventBase)) { return false; } // 添加事件 if (!event_add($this->allEvents[$event_key][$flag])) { return false; } return true; }
protected function addConnectionBuffer($conn_num) { $buffer = event_buffer_new($this->connections[$conn_num], array($this, 'onEventRead'), array($this, 'onEventWrite'), array($this, 'onEventError'), array($conn_num)); event_buffer_base_set($buffer, $this->event_base); event_buffer_timeout_set($buffer, $this->timeout, $this->timeout); event_buffer_enable($buffer, EV_READ | EV_WRITE | EV_PERSIST); self::log('Connection', $conn_num, 'buffer added'); $this->connection_buffers[$conn_num] = $buffer; $this->connection_statuses[$conn_num] = self::STATE_READ; }
/** * 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); } }
protected function InitSocket() { stream_set_blocking($this->Socket, 0); stream_set_read_buffer($this->Socket, 4096); stream_set_write_buffer($this->Socket, 4096); $this->BufferEvent = event_buffer_new($this->Socket, array($this, 'evcb_doReceive'), array($this, 'evcb_WriteFin'), array($this, 'evcb_ClientBufferError'), $this->Socket); if (!$this->BufferEvent) { // echo "Buffer Event Error $this\n"; @stream_socket_shutdown($this->Socket, STREAM_SHUT_RDWR); @fclose($this->Socket); } event_buffer_watermark_set($this->BufferEvent, EV_WRITE, 1, 1); event_buffer_base_set($this->BufferEvent, $this->BaseEvent); event_buffer_enable($this->BufferEvent, EV_READ | EV_WRITE); $this->Server->IncCurrentConnected(); }