Esempio n. 1
0
 /**
  * Main reading loop. Call this in your own infinite loop or declare(ticks)
  * in your software. This routine will call your server handler when there
  * is data available to read, new connections, or timeouts. Will always
  * detect when the other side closed the connection.
  *
  * @return void
  */
 public function process()
 {
     if ($this->_socket === false || !$this->_open) {
         return;
     }
     // Control server.
     $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)) {
             $newSocket = socket_accept($this->_socket);
             if ($newSocket !== false) {
                 $address = '';
                 $port = 0;
                 socket_getpeername($newSocket, $address, $port);
                 $peerObject = new \Ding\Helpers\Tcp\TcpPeer($address, $port, $newSocket);
                 $peername = $peerObject->getName();
                 $this->_peers[$peername] = $peerObject;
                 $this->_peersSockets[$peername] = $newSocket;
                 $this->_peersLastDataReceived[$peername] = $this->getMicrotime();
                 $this->_handler->handleConnection($peerObject);
             }
         }
     }
     $this->processPeers();
 }
Esempio n. 2
0
 /**
  * Sets the tcp client handler.
  *
  * @param ITcpClientHandler $handler Client handler to use for callbacks.
  *
  * @return void
  */
 public function setHandler(ITcpClientHandler $handler)
 {
     $this->_handler = $handler;
     $handler->setClient($this);
 }