Example #1
0
 /**
  * Create connection, with built-in pool.
  * @param string $conn connection string, like `protocal://host:port`.
  * @param mixed $arg external argument, fetched by `[[getExArg()]]`
  * @return static the connection object, null if it reaches the upper limit of concurrent, or false on failure.
  */
 public static function connect($conn, $arg = null)
 {
     $obj = null;
     if (!isset(self::$_objs[$conn])) {
         self::$_objs[$conn] = [];
     }
     foreach (self::$_objs[$conn] as $tmp) {
         if (!($tmp->flag & self::FLAG_BUSY)) {
             Client::debug('reuse conn \'', $tmp->conn, '\': ', $tmp->sock);
             $obj = $tmp;
             break;
         }
     }
     if ($obj === null && count(self::$_objs[$conn]) < self::MAX_BURST) {
         $obj = new self($conn);
         self::$_objs[$conn][] = $obj;
         Client::debug('create conn \'', $conn, '\'');
     }
     if ($obj !== null) {
         if ($obj->flag & self::FLAG_OPENED) {
             $obj->flag |= self::FLAG_REUSED;
         } else {
             if (!$obj->openSock()) {
                 return false;
             }
         }
         $obj->flag |= self::FLAG_BUSY;
         $obj->outBuf = null;
         $obj->outLen = 0;
         $obj->arg = $arg;
     }
     return $obj;
 }