本文整理汇总了PHP中stream_set_blocking函数的典型用法代码示例。如果您正苦于以下问题:PHP stream_set_blocking函数的具体用法?PHP stream_set_blocking怎么用?PHP stream_set_blocking使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了stream_set_blocking函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: sshiconn
function sshiconn($cmd, $pass, $ip, $sshp = 22)
{
$ip = $_REQUEST['ip'];
$pass = $_REQUEST['pass'];
$sshp = $_REQUEST['sshp'];
if (!isset($_REQUEST['sshp'])) {
$sshp = '22';
}
$connection = ssh2_connect($ip, $sshp);
if (!$connection) {
throw new Exception("fail: unable to establish connection\nPlease IP or if server is on and connected");
}
$pass_success = ssh2_auth_password($connection, 'root', $pass);
if (!$pass_success) {
throw new Exception("fail: unable to establish connection\nPlease Check your password");
}
$stream = ssh2_exec($connection, $cmd);
$errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
stream_set_blocking($errorStream, true);
stream_set_blocking($stream, true);
print_r($cmd);
$output = stream_get_contents($stream);
fclose($stream);
fclose($errorStream);
ssh2_exec($connection, 'exit');
unset($connection);
return $output;
}
示例2: connect
/**
* Connects the TCP socket to the host with the given IP address and port
* number
*
* Depending on whether PHP's sockets extension is loaded, this uses either
* <var>socket_create</var>/<var>socket_connect</var> or
* <var>fsockopen</var>.
*
* @param string $ipAddress The IP address to connect to
* @param int $portNumber The TCP port to connect to
* @param int $timeout The timeout in milliseconds
* @throws SocketException if an error occurs during connecting the socket
*/
public function connect($ipAddress, $portNumber, $timeout)
{
$this->ipAddress = $ipAddress;
$this->portNumber = $portNumber;
if ($this->socketsEnabled) {
if (!($this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP))) {
throw new SocketException(socket_last_error($this->socket));
}
socket_set_nonblock($this->socket);
@socket_connect($this->socket, $ipAddress, $portNumber);
$write = array($this->socket);
$read = $except = array();
$sec = floor($timeout / 1000);
$usec = $timeout % 1000;
if (!socket_select($read, $write, $except, $sec, $usec)) {
$errorCode = socket_last_error($this->socket);
} else {
$errorCode = socket_get_option($this->socket, SOL_SOCKET, SO_ERROR);
}
if ($errorCode) {
throw new SocketException($errorCode);
}
socket_set_block($this->socket);
} else {
if (!($this->socket = @fsockopen("tcp://{$ipAddress}", $portNumber, $socketErrno, $socketErrstr, $timeout / 1000))) {
throw new SocketException($socketErrstr);
}
stream_set_blocking($this->socket, true);
}
}
示例3: load_data
/**
* Send request to VIES site and retrieve results
*
* @access public
* @param string
* @return mixed
*/
function load_data($url)
{
$url = parse_url($url);
if (!in_array($url['scheme'], array('', 'http'))) {
return false;
}
$fp = fsockopen($url['host'], $url['port'] > 0 ? $url['port'] : 80, $errno, $errstr, 2);
if (!$fp) {
return false;
} else {
fputs($fp, "GET " . $url['path'] . (isset($url['query']) ? '?' . $url['query'] : '') . " HTTP/1.0\r\n");
fputs($fp, "Host: " . $url['host'] . "\r\n");
fputs($fp, "Connection: close\r\n\r\n");
$data = '';
stream_set_blocking($fp, false);
stream_set_timeout($fp, 4);
$status = socket_get_status($fp);
while (!feof($fp) && !$status['timed_out']) {
$data .= fgets($fp, 1000);
$status = socket_get_status($fp);
}
if ($status['timed_out']) {
return false;
}
fclose($fp);
return $data;
}
}
示例4: read
/**
* Read everything from standard input
*
* @param callable $lineCallback An operation to perform on each line read
* @param callable $doneCallback An operation to perform when the end has been reached
* @throws \RuntimeException if a timeout occurs
* @link http://www.gregfreeman.org/2013/processing-data-with-php-using-stdin-and-piping/
* Based on "Processing data with PHP using STDIN and Piping" by Greg Freeman
*/
public function read(callable $lineCallback, callable $doneCallback = null)
{
stream_set_blocking(STDIN, 0);
$timeoutStarted = false;
$timeout = null;
while (1) {
// I'm getting something...
while (false !== ($line = fgets(STDIN))) {
$lineCallback($line);
if ($timeoutStarted) {
$timeoutStarted = false;
$timeout = null;
}
}
// End of input
if (feof(STDIN)) {
if ($doneCallback) {
$doneCallback();
}
break;
}
// Wait a spell
if (null === $timeout) {
$timeout = time();
$timeoutStarted = true;
continue;
}
// Timeout
if (time() > $timeout + $this->wait) {
throw new \RuntimeException('Timeout reached while reading STDIN');
return;
}
}
}
示例5: setBlocking
/**
* @param bool $blocking
*/
public function setBlocking($blocking)
{
$this->blocking = (bool) $blocking;
foreach ($this->pipes as $pipe) {
stream_set_blocking($pipe, $this->blocking);
}
}
示例6: SendMsg2Daemon
function SendMsg2Daemon($ip, $port, $msg, $timeout = 15)
{
if (!$ip || !$port || !$msg) {
return array(false);
}
$errno;
$errstr;
$fp = @fsockopen($ip, $port, $errno, $errstr, $timeout);
if (!$fp) {
return array(false);
}
stream_set_blocking($fp, true);
stream_set_timeout($fp, $timeout);
@fwrite($fp, $msg . "\n");
$status = stream_get_meta_data($fp);
$ret;
if (!$status['timed_out']) {
$datas = 'data:';
while (!feof($fp)) {
$data = fread($fp, 4096);
if ($data) {
$datas = $datas . $data;
}
}
return array(true, $datas);
} else {
$ret = array(false);
}
@fclose($fp);
return ret;
}
示例7: asteriskClient
/**
* this function defines the asterisk client
*/
function asteriskClient()
{
global $app_strings, $current_user;
global $adb, $log;
$data = getAsteriskInfo($adb);
$server = $data['server'];
$port = $data['port'];
$username = $data['username'];
$password = $data['password'];
$version = $data['version'];
$errno = $errstr = NULL;
$sock = @fsockopen($server, $port, $errno, $errstr, 1);
stream_set_blocking($sock, false);
if ($sock === false) {
echo "Socket cannot be created due to error: {$errno}: {$errstr}\n";
$log->debug("Socket cannot be created due to error: {$errno}: {$errstr}\n");
exit(0);
} else {
echo "Date: " . date("d-m-Y") . "\n";
echo "Connecting to asterisk server.....\n";
$log->debug("Connecting to asterisk server.....\n");
}
echo "Connected successfully\n\n\n";
$asterisk = new Asterisk($sock, $server, $port);
authorizeUser($username, $password, $asterisk);
//keep looping continuosly to check if there are any calls
while (true) {
//check for incoming calls and insert in the database
sleep(2);
$incoming = handleIncomingCalls($asterisk, $adb, $version);
}
fclose($sock);
unset($sock);
}
示例8: execute
public function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('Starting server on port 5000');
$descriptorspec = array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w'));
$port = $input->getOption('port');
// Start server process
$process = proc_open("php -S localhost:{$port} -t public/ " . getcwd() . "/scripts/normal-server/router.php", $descriptorspec, $pipes, getcwd(), $_ENV);
if (!is_resource($process)) {
throw new Exception("popen error");
}
stream_set_blocking($pipes[0], 0);
stream_set_blocking($pipes[1], 0);
stream_set_blocking($pipes[2], 0);
// Redirect all output
while (!feof($pipes[1])) {
foreach ($pipes as $pipe) {
$line = fread($pipe, 128);
if ($line) {
$output->writeln($line);
}
}
sleep(0.5);
}
foreach ($pipes as $pipe) {
fclose($pipe);
}
}
示例9: acceptSocket
protected function acceptSocket()
{
$connection = stream_socket_accept($this->socket, 0);
stream_set_blocking($this->socket, 0);
self::log('Socket', 'accepted');
return $connection;
}
示例10: exec
/**
* @param $command
* @param bool $display
* @return array
* @throws \Exception
*/
public function exec($command, $display = true)
{
$outStream = ssh2_exec($this->stream, $command);
$errStream = ssh2_fetch_stream($outStream, SSH2_STREAM_STDERR);
stream_set_blocking($outStream, true);
stream_set_blocking($errStream, true);
$err = $this->removeEmptyLines(explode("\n", stream_get_contents($errStream)));
if (count($err)) {
if (strpos($err[0], 'Cloning into') === false) {
// throw new \Exception(implode("\n", $err));
}
}
$out = $this->removeEmptyLines(explode("\n", stream_get_contents($outStream)));
fclose($outStream);
fclose($errStream);
if ($display) {
if (!$this->output instanceof OutputInterface) {
throw new \LogicException('You should set output first');
}
foreach ($out as $line) {
$this->output->writeln(sprintf('<info>%s</info>', $line));
}
}
return $out;
}
示例11: _sock
function _sock($url)
{
$host = parse_url($url, PHP_URL_HOST);
$port = parse_url($url, PHP_URL_PORT);
$port = $port ? $port : 80;
$scheme = parse_url($url, PHP_URL_SCHEME);
$path = parse_url($url, PHP_URL_PATH);
$query = parse_url($url, PHP_URL_QUERY);
if ($query) {
$path .= '?' . $query;
}
if ($scheme == 'https') {
$host = 'ssl://' . $host;
}
if ($fp = @fsockopen($host, $port, $error_code, $error_msg, 5)) {
stream_set_blocking($fp, 0);
//开启了手册上说的非阻塞模式
$header = "GET {$path} HTTP/1.1\r\n";
$header .= "Host: {$host}\r\n";
$header .= "Connection: Close\r\n\r\n";
//长连接关闭
fwrite($fp, $header);
fclose($fp);
}
return array($error_code, $error_msg);
}
示例12: connect
/**
*
* @param string $host Host to connect to. Default is localhost (127.0.0.1).
* @param int $port Port to connect to. Default is 8080.
* @return boolean True on success
* @throws \RuntimeException
*/
protected function connect($host, $port)
{
$key1 = $this->generateRandomString(32);
$key2 = $this->generateRandomString(32);
$key3 = $this->generateRandomString(8, false, true);
$header = "GET /echo HTTP/1.1\r\n";
$header .= "Upgrade: WebSocket\r\n";
$header .= "Connection: Upgrade\r\n";
$header .= "Host: " . $host . ":" . $port . "\r\n";
$header .= "Sec-WebSocket-Key1: " . $key1 . "\r\n";
$header .= "Sec-WebSocket-Key2: " . $key2 . "\r\n";
$header .= "\r\n";
$header .= $key3;
$this->socket = @stream_socket_client('tcp://' . $host . ':' . $port, $errno, $errstr, self::SOCKET_TIMEOUT);
if (!$this->socket) {
throw new \RuntimeException(sprintf('WebSocket connection error (%u): %s', $errno, $errstr));
}
stream_set_blocking($this->socket, false);
// do a handshake
if (!fwrite($this->socket, $header)) {
throw new \RuntimeException('WebSocket write error');
}
/**
* @todo: check response here. Currently not implemented cause "2 key handshake" is already deprecated.
* See: http://en.wikipedia.org/wiki/WebSocket#WebSocket_Protocol_Handshake
*/
// $response = fread($this->socket, 2000);
return true;
}
示例13: __construct
function __construct($config)
{
if (extension_loaded("pcntl")) {
//Add signal handlers to shut down the bot correctly if its getting killed
pcntl_signal(SIGTERM, array($this, "signalHandler"));
pcntl_signal(SIGINT, array($this, "signalHandler"));
} else {
//die("Please make sure the pcntl PHP extension is enabled.\n");
}
$this->config = $config;
$this->startTime = time();
$this->lastServerMessage = $this->startTime;
ini_set("memory_limit", $this->config['memoryLimit'] . "M");
if ($config['verifySSL']) {
$this->socket = stream_socket_client("" . $config['server'] . ":" . $config['port']) or die("Connection error!");
} else {
$socketContext = stream_context_create(array("ssl" => array("verify_peer" => false, "verify_peer_name" => false)));
$this->socket = stream_socket_client("" . $config['server'] . ":" . $config['port'], $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $socketContext) or die("Connection error!");
}
stream_set_blocking($this->socket, 0);
stream_set_timeout($this->socket, 600);
$this->login();
$this->loadPlugins();
$this->main($config);
}
示例14: connect
public function connect()
{
$nick = $this->getConfig('nick', 'phabot');
$server = $this->getConfig('server');
$port = $this->getConfig('port', 6667);
$pass = $this->getConfig('pass');
$ssl = $this->getConfig('ssl', false);
$user = $this->getConfig('user', $nick);
if (!preg_match('/^[A-Za-z0-9_`[{}^|\\]\\-]+$/', $nick)) {
throw new Exception(pht("Nickname '%s' is invalid!", $nick));
}
$errno = null;
$error = null;
if (!$ssl) {
$socket = fsockopen($server, $port, $errno, $error);
} else {
$socket = fsockopen('ssl://' . $server, $port, $errno, $error);
}
if (!$socket) {
throw new Exception(pht('Failed to connect, #%d: %s', $errno, $error));
}
$ok = stream_set_blocking($socket, false);
if (!$ok) {
throw new Exception(pht('Failed to set stream nonblocking.'));
}
$this->socket = $socket;
if ($pass) {
$this->write("PASS {$pass}");
}
$this->write("NICK {$nick}");
$this->write("USER {$user} 0 * :{$user}");
}
示例15: __construct
/**
* Initialize a new stream listener
*/
public function __construct($stream)
{
$this->stream = $stream;
stream_set_blocking($this->stream, 0);
$meta = stream_get_meta_data($this->stream);
if (substr($meta['mode'], -1) === '+') {
$this->writable = true;
$this->readable = true;
} else {
if (strpos($meta['mode'], 'r') !== false) {
$this->readable = true;
}
if (strpos($meta['mode'], 'w') !== false) {
$this->writable = true;
}
}
if ($this->readable) {
$this->ev_read = event_new();
event_set($this->ev_read, $this->stream, EV_READ | EV_PERSIST, array($this, '_read'));
Loop::attachEvent($this->ev_read);
}
if ($this->writable) {
$this->ev_write = event_new();
event_set($this->ev_write, $this->stream, EV_WRITE | EV_PERSIST, array($this, '_write'));
Loop::attachEvent($this->ev_write);
}
}