예제 #1
0
 /**
  * This will process activity in all peers. Called from process().
  *
  * @return void
  */
 public function processPeers()
 {
     // Control peers.
     if (count($this->_peers) < 1) {
         return;
     }
     $now = $this->getMicrotime();
     foreach ($this->_peers as $peerName => $peer) {
         if (!$peer->hasActivity()) {
             continue;
         }
         $buffer = '';
         $len = 1;
         $len = $peer->read($buffer, $len, true);
         if ($len > 0) {
             if ($len >= $this->_readLen) {
                 $this->_handler->handleData($peer);
                 $this->_peersLastDataReceived[$peerName] = $now;
             }
         } else {
             $peer->disconnect();
             $this->_freePeer($peer);
             $this->_handler->disconnect($peer);
         }
     }
     foreach ($this->_peers as $peerName => $peer) {
         $peerTime = $this->_peersLastDataReceived[$peerName];
         if ($now - $peerTime > $this->_rTo) {
             if ($this->_rTo > 0) {
                 $peer->disconnect();
                 $this->_freePeer($peer);
                 $this->_handler->readTimeout($peer);
             }
             $this->_peersLastDataReceived[$peerName] = $now;
         }
     }
 }
예제 #2
0
 /**
  * Main reading loop. Call this in your own infinite loop or declare(ticks)
  * in your software. This routine will call your client handler when there
  * is data available to read. Will always detect when the other side closed
  * the connection.
  *
  * @return void
  */
 public function process()
 {
     if ($this->_socket === false || !$this->_connected) {
         return;
     }
     $read = array($this->_socket);
     $write = null;
     $ex = null;
     $result = @socket_select($read, $write, $ex, 0, 1);
     if ($result === false) {
         throw new TcpException('Error selecting from socket: ' . socket_strerror(socket_last_error($this->_socket)));
     }
     if ($result > 0) {
         if (in_array($this->_socket, $read)) {
             $buffer = '';
             $len = 1;
             $len = $this->read($buffer, $len, true);
             if ($len > 0) {
                 if ($len >= $this->_readLen) {
                     $this->_lastDataReadTime = $this->getMicrotime();
                     $this->_handler->data();
                     return;
                 }
             } else {
                 $this->close();
                 return;
             }
         }
     }
     $now = $this->getMicrotime();
     if ($now - $this->_lastDataReadTime > $this->_rTo) {
         if ($this->_rTo > 0) {
             $this->_handler->readTimeout();
         }
         $this->_lastDataReadTime = $now;
     }
 }