コード例 #1
0
ファイル: Test_TCP_Peer.php プロジェクト: im286er/Ding
 /**
  * @test
  */
 public function can_use_peer()
 {
     $peer = new \Ding\Helpers\Tcp\TcpPeer('address', 'port', null);
     $this->assertEquals('address', $peer->getAddress());
     $this->assertEquals('port', $peer->getPort());
     $this->assertEquals('address:port', $peer->getName());
     $this->assertNull($peer->getSocket());
     $this->assertFalse($peer->hasActivity());
 }
コード例 #2
0
ファイル: TcpServerHelper.php プロジェクト: im286er/Ding
 /**
  * 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();
 }