public function __construct() { $this->process = new swoole_process(array($this, 'run'), false, true); if (!$this->process->useQueue(123)) { var_dump(swoole_strerror(swoole_errno())); exit; } $this->process->start(); while (true) { $data = $this->process->pop(); echo "RECV: " . $data . PHP_EOL; } }
function long_tcp(Benchmark $bc) { global $send_data, $package_eof; static $client = null; static $i; static $index; $start = microtime(true); if (empty($client)) { $client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_SYNC); $client->set(array('open_eof_check' => true, "package_eof" => $package_eof)); $end = microtime(true); $conn_use = $end - $start; $bc->max_conn_time = $conn_use; $i = 0; $index = 0; if (!$client->connect($bc->server_config['host'], $bc->server_config['port'], 2)) { error: echo "Error: " . swoole_strerror($client->errCode) . "[{$client->errCode}]\n"; $client = null; return false; } $start = $end; } $data = $send_data[$index]["data"] . $package_eof; if (!$client->send($data)) { goto error; } $end = microtime(true); $write_use = $end - $start; if ($write_use > $bc->max_write_time) { $bc->max_write_time = $write_use; } $start = $end; $i++; if ($i >= $send_data[$index]["num"]) { $index++; } $ret = $client->recv(); if (empty($ret)) { echo $bc->pid, "#{$i}", " is lost\n"; return false; } $end = microtime(true); $read_use = $end - $start; if ($read_use > $bc->max_read_time) { $bc->max_read_time = $read_use; } return true; }
function long_tcp(Swoole_Benchmark $bc) { static $fp = null; static $i; $start = microtime(true); if (empty($fp)) { $fp = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_SYNC); $end = microtime(true); $conn_use = $end - $start; $bc->max_conn_time = $conn_use; $i = 0; //echo "connect {$bc->server_url} \n"; if (!$fp->connect($bc->server_config['host'], $bc->server_config['port'], 1)) { error: echo "Error: " . swoole_strerror($fp->errCode) . "[{$fp->errCode}]\n"; $fp = null; return false; } $start = $end; } /*--------写入Sokcet-------*/ if (!$fp->send($bc->send_data)) { goto error; } $end = microtime(true); $write_use = $end - $start; if ($write_use > $bc->max_write_time) { $bc->max_write_time = $write_use; } $start = $end; /*--------读取Sokcet-------*/ $ret = $fp->recv(); //var_dump($ret); $i++; if (empty($ret)) { echo $bc->pid, "#{$i}@", " is lost\n"; return false; } $end = microtime(true); $read_use = $end - $start; if ($read_use > $bc->max_read_time) { $bc->max_read_time = $read_use; } return true; }
#!/usr/local/bin/php <?php /** * usage: php upload_client.php -h 127.0.0.1 -p 9507 -f test.jpg */ require_once dirname(__DIR__) . '/src/_init.php'; $encrypt_key = file_get_contents(dirname(__DIR__) . '/src/encrypt.key'); $client = new NodeAgent\Client($encrypt_key); /** * 连接到服务器 */ if (!$client->connect('127.0.0.1', 9507, 30)) { echo "Error: connect to server failed. " . swoole_strerror($client->errCode); die("\n"); } //$res = $client->request(['cmd' => 'getNodeList']); //var_dump($res); //exit; // //$remote_file = '/data/testnode/' . basename($file); //$client->UploadCallback = function ($send_n, $total) //{ // echo "$send_n/$total\n"; //}; //if (!$client->upload($file, $remote_file)) //{ // die("upload success.\n"); //} var_dump($client->execute('reload_nginx.sh'), $client->errCode); //var_dump($client->delete(['/tmp/test1.txt', '/tmp/test2.txt']));
private function spawnCustomWorker($worker_name, $config) { $process = new \swoole_process(function (\swoole_process $worker) { $recv = $worker->pop(); $worker_config = unserialize($recv); extract($worker_config); $name = isset($this->serverConfig['server']['worker_process_name']) ? $this->serverConfig['server']['worker_process_name'] : 'ypf:swoole-worker-%d'; $processName = sprintf("{$name}:%s", 0, $worker_name); \swoole_set_process_name($processName); self::getInstance()->disPatch($config['action'], array('worker_name' => $worker_name)); }, false, false); $process->useQueue(); $pid = $process->start(); $this->worker_pid[] = $pid; $process->push(serialize(array('pid' => $pid, 'worker_name' => $worker_name, 'config' => $config))); if ($pid > 0) { echo "starting worker : {$worker_name} [ OK ]\n"; } else { echo "starting worker : {$worker_name} [ FAIL ] '" . \swoole_strerror(swoole_errno()) . "' \n"; } }
public function start() { // 只能单例运行 if ($this->config['is_sington'] == true) { $this->checkPidfile(); } $this->createPidfile(); if (!isset($this->on_func['request'])) { log::prn_log(ERROR, "must set on_request callback function!"); exit; } $this->serv->start(); if (!$this->shutdown) { log::prn_log(ERROR, "swoole start error: " . swoole_errno() . ',' . swoole_strerror(swoole_errno())); } }
} $request = ['request' => $_REQUEST, 'cookies' => $_COOKIE, 'server' => $_SERVER, 'files' => $_FILES]; $request = serialize($request); $request = pack('N', $length = strlen($request)) . $request; if ($length > 2097152) { foreach (str_split($request, 1048576) as $chunk) { $client->send($chunk); } } else { $client->send($request); } $response = $client->recv(); $client->close(); if ($response === false) { header('Bad Gateway', true, 502); echo '<html><head><title>502 Bad Gateway</title></head><body bgcolor="white"><center><h1>502 Bad Gateway</h1></center><hr><center>nginx</center><div style="display:none">err ' . $client->errCode . ': ' . swoole_strerror($client->errCode) . '</div></body></html>'; exit; } $length = unpack('N', $response)[1]; $response = unserialize(substr($response, -$length)); if (isset($response['headers']) && ($headers = $response['headers'])) { isset($headers['status']) and header($headers['status'], true); if (isset($headers['set_cookies']) && is_array($headers['set_cookies'])) { foreach ($headers['set_cookies'] as $cookie) { call_user_func_array('setcookie', $cookie); } } if (isset($headers['headers']) && is_array($headers['headers'])) { foreach ($headers['headers'] as $v) { header($v, false); }
function long_socks5($bc) { static $fp = null; static $i; $start = microtime(true); if (empty($fp)) { $fp = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_SYNC, 5); $end = microtime(true); $conn_use = $end - $start; $bc->max_conn_time = $conn_use; $i = 0; //echo "connect {$bc->server_url} \n"; if (!$fp->connect($bc->server_config['host'], $bc->server_config['port'], 2)) { error: echo "Error: " . swoole_strerror($fp->errCode) . "[{$fp->errCode}]\n"; $fp = null; return false; } $fp->send(pack("C3", 0x5, 0x1, 0x0)); //greet $data = $fp->recv(); $response = unpack("Cversion/Cmethod", $data); if ($response['version'] != 0x5) { exit('SOCKS version is not supported.'); } $headers = getHeader($bc->send_data); if (empty($headers['port'])) { $headers['port'] = 80; } $g = pack("C5", 0x5, 0x1, 0x0, 0x3, strlen($headers['host'])) . $headers['host'] . pack("n", $headers['port']); $fp->send($g); $data = $fp->recv(); $response = unpack("Cversion/Cresult/Creg/Ctype/Lip/Sport", $data); if ($response['result'] != 0x0) { echo 'SOCKS connection request failed: ' . getSocksRefusalMsg($response['result']), $response['result']; exit; } $start = $end; } /*--------写入Sokcet-------*/ if (!$fp->send($bc->send_data)) { goto error; } $end = microtime(true); $write_use = $end - $start; if ($write_use > $bc->max_write_time) { $bc->max_write_time = $write_use; } $start = $end; /*--------读取Sokcet-------*/ while (true) { $ret = $fp->recv(65530); if (empty($ret) or substr($ret, -1, 1) == "\n") { break; } } //var_dump($ret); $i++; if (empty($ret)) { echo $bc->pid, "#{$i}@", " is lost\n"; return false; } $end = microtime(true); $read_use = $end - $start; if ($read_use > $bc->max_read_time) { $bc->max_read_time = $read_use; } return true; }
public function start() { // 只能单例运行 if ($this->config['is_sington'] == true) { $this->checkPidfile(); } $this->createPidfile(); $this->serv->start(); if (!$this->shutdown) { log::prn_log(ERROR, "swoole start error: " . swoole_errno() . ',' . swoole_strerror(swoole_errno())); } }