Executes the given callback when/if the connection is handshaked.
@param  callable $cb Callback
@callback $cb ( )
@return void
3964
public function onConnected($cb)
    {
        if ($this->state === self::STATE_AUTH_ERROR) {
            $cb($this, false);
        } elseif ($this->state === self::STATE_AUTH_OK) {
            $cb($this, true);
        } else {
            if (!$this->onConnected) {
                $this->onConnected = new StackCallbacks();
            }
            $this->onConnected->push($cb);
        }
    }
961623
public function onWriteOnce($cb)
    {
        if (!$this->writing) {
            $cb($this);
            return;
        }
        $this->onWriteOnce->push($cb);
    }
288227
public function on($name, callable $cb, $replace = false)
    {
        if ($replace || !isset($this->_events[$name])) {
            $this->_events[$name] = $cb;
        }
    }
761114
public function executeCommand(CommandInterface $command, callable $callback)
    {
        $this->connection->executeCommand($command, $this->wrapCallback($callback));
    }
166573
protected void acquireAccessToken(final OAuthCallback cb) {
        if (isAccessTokenCached()) {
            // Execute the callback immediately with cached OAuth credentials
            if (VERBOSE) Log.d(TAG, "Access token cached");
            if (cb != null) {
                // Ensure networking occurs off the main thread
                // TODO: Use an ExecutorService and expose an application shutdown method to shutdown?
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        cb.onSuccess(getRequestFactoryFromCachedCredentials());
                    }
                }).start();
            }
        } else if (mOauthInProgress && cb != null) {
            // Add the callback to the queue for execution when outstanding OAuth negotiation complete
            mCallbackQueue.add(cb);
            if (VERBOSE) Log.i(TAG, "Adding cb to queue");
        } else {
            mOauthInProgress = true;
            // Perform an OAuth Client Credentials Request
            // TODO: Replace with new Thread()
            new AsyncTask<Void, Void, Void>() {

                @Override
                protected Void doInBackground(Void... params) {
                    TokenResponse response = null;
                    try {
                        if (VERBOSE)
                            Log.i(TAG, "Fetching OAuth " + mConfig.getAccessTokenRequestUrl());
                        response = new ClientCredentialsTokenRequest(new NetHttpTransport(), new JacksonFactory(), new GenericUrl(mConfig.getAccessTokenRequestUrl()))
                                .setGrantType("client_credentials")
                                .setClientAuthentication(new BasicAuthentication(mConfig.getClientId(), mConfig.getClientSecret()))
                                .execute();
                    } catch (IOException e) {
                        // TODO: Alert user Kickflip down
                        //       or client credentials invalid
                        if (cb != null) {
                            postExceptionToCallback(cb, e);
                        }
                        e.printStackTrace();
                    }
                    if (response != null) {
                        if (VERBOSE)
                            Log.i(TAG, "Got Access Token " + response.getAccessToken().substring(0, 5) + "...");
                        storeAccessToken(response);
                        mOauthInProgress = false;
                        if (cb != null)
                            cb.onSuccess(getRequestFactoryFromAccessToken(mStorage.getString(ACCESS_TOKEN_KEY, null)));
                        executeQueuedCallbacks();
                    } else {
                        mOauthInProgress = false;
                        Log.w(TAG, "Failed to get Access Token");
                    }
                    return null;
                }
            }.execute();
        }
    }
291275
public function sendPing($host, $cb)
    {
        $this->connect('raw://' . $host, function ($conn) use ($cb) {
            $conn->sendEcho($cb);
        });
    }
691685
public function call(callable $callback, $connection = null)
    {
        $this->database->connection($connection)->beginTransaction();

        try
        {
            $result = $callback($this->database);

            $this->database->connection($connection)->commit();
            $this->state = static::STATE_COMMIT;

            return $result;
        }
        catch (\Exception $exception)
        {
            $this->database->connection($connection)->rollBack();
            $this->state = static::STATE_ROLLBACK;

            return null;
        }
    }
90728
protected function postFinishHandler($cb = null)
    {
        if (!$this->headers_sent) {
            $this->out('');
        }
        $this->sendfp = null;
        if (isset($this->attrs->files)) {
            foreach ($this->attrs->files as $f) {
                if (isset($f['tmp_name'])) {
                    FileSystem::unlink($f['tmp_name']);
                }
            }
        }
        if (isset($this->attrs->session)) {
            $this->sessionCommit($cb);
        } else {
            $cb === null || $cb();
        }
    }
319517
protected function setCallbackQueryHandler(CallbackQueryHandler $handler): void
	{
		$handler->setBot($this);
		$this->cqHandler = $handler;
	}
741419
private void fireConnectionEstablished() {
        final boolean hadLock = lock.isHeldByCurrentThread();
        if (hadLock) {
            lock.unlock();
        }
        try {
            BOSHClientConnEvent event = null;
            for (BOSHClientConnListener listener : connListeners) {
                if (event == null) {
                    event = BOSHClientConnEvent
                            .createConnectionEstablishedEvent(this);
                }
                try {
                    listener.connectionEvent(event);
                } catch (Exception ex) {
                    LOG.log(Level.WARNING, UNHANDLED, ex);
                }
            }
        } finally {
            if (hadLock) {
                lock.lock();
            }
        }
    }
990707
public function listen($callback) {
		if ($this->listening) {
			$this->connection->read(function ($line) use ($callback) {
				$this->checkForError($line);
				$change = $this->parseChangeLine($line);
				if ($change) {
					return $callback($change);
				}
			});
		}
	}
426454
function run(callable $callback = null) {
        if (isset($this->res))
            return null;
        $this->res = proc_open($this->cmd, $this->descriptor_spec, $this->pipes, $this->cwd, $this->env);
        if ($this->res) {
            $this->stdout = new TcpConnection($this->pipes[1]);
            if (is_callable($this->onMessage))
                $this->stdout->onMessage = function ($conn, $data) {
                    ($this->onMessage)($this, $data);
                };
            if (is_callable($this->onClose))
                $this->stdout->onClose = function () {
                    ($this->onClose)($this);
                };
            stream_set_blocking($this->pipes[0], false);
            if (is_callable($callback))
                $callback($this);
            return true;
        }
        unset ($this->res);
        return false;
    }
1166793
public static function on($name, callable $cb, $replace = false)
    {
        if ($replace || !isset(self::$_events[$name])) {
            self::$_events[$name] = $cb;
        }
    }
241391
public function ping($addr, $cb)
    {
        $e = explode(':', $addr);
        $this->getConnection('valve://[udp:' . $e[0] . ']' . (isset($e[1]) ? ':' . $e[1] : '') . '/ping',
            function ($conn) use ($cb) {
                if (!$conn->connected) {
                    $cb($conn, false);
                    return;
                }
                $mt = microtime(true);
                $conn->request('ping', null, function ($conn, $success) use ($mt, $cb) {
                    $cb($conn, $success ? (microtime(true) - $mt) : false);
                });
            });
    }
107294
public static function forceWrap($cb, $timeout = null)
    {
        if ($cb instanceof CallbackWrapper) {
            return $cb;
        }
        if ($cb === null) {
            return null;
        }
        return new static($cb, $timeout, Daemon::$context);
    }
906902
protected function challenge($cb)
    {
        $this->onChallenge = $cb;
        $this->state = self::CONN_STATE_CHALLENGE_PACKET_SENT;
        $this->write(
            "Action: Challenge\r\n"
            . "AuthType: MD5\r\n"
            . "\r\n"
        );
    }
136448
protected function executeMutedCallback()
    {
        try {
            $this->result->setValue(call_user_func_array($this->callback, $this->params));
        } catch (Throwable $exception) {
            $this->result->setException($exception);
            $this->result->setValue(null);
        }
    }