Пример #1
0
 public function __construct()
 {
     $this->table = new swoole_table(1024);
     $this->table->column('serverfd', swoole_table::TYPE_INT, 8);
     $this->table->create();
     define('APPLICATION_PATH', dirname(dirname(__DIR__)) . "/application");
     define('MYPATH', dirname(APPLICATION_PATH));
     $this->application = new Yaf_Application(dirname(APPLICATION_PATH) . "/conf/application.ini");
     $this->application->bootstrap();
     $config_obj = Yaf_Registry::get("config");
     $distributed_config = $config_obj->distributed->toArray();
     $server = new swoole_server($distributed_config['ServerIp'], $distributed_config['port'], SWOOLE_PROCESS, SWOOLE_SOCK_TCP);
     if (isset($distributed_config['logfile'])) {
         $server->set(array('worker_num' => 4, 'task_worker_num' => 4, 'dispatch_mode' => 4, 'daemonize' => true, 'log_file' => $distributed_config['logfile']));
     } else {
         $server->set(array('worker_num' => 4, 'task_worker_num' => 4, 'dispatch_mode' => 4, 'daemonize' => true));
     }
     require_once __DIR__ . "/DistributedClient.php";
     $server->on('Start', array(&$this, 'onStart'));
     $server->on('WorkerStart', array(&$this, 'onWorkerStart'));
     $server->on('Connect', array(&$this, 'onConnect'));
     $server->on('Receive', array(&$this, 'onReceive'));
     $server->on('Task', array(&$this, 'onTask'));
     $server->on('Finish', array(&$this, 'onFinish'));
     $server->on('Close', array(&$this, 'onClose'));
     $server->on('ManagerStop', array(&$this, 'onManagerStop'));
     $server->on('WorkerError', array(&$this, 'onWorkerError'));
     $server->start();
 }
Пример #2
0
 function run($setting = array())
 {
     $this->swooleSetting = array_merge($this->swooleSetting, $setting);
     if (!empty($this->swooleSetting['pid_file'])) {
         $this->pid_file = $this->swooleSetting['pid_file'];
     }
     $this->sw->set($this->swooleSetting);
     $version = explode('.', SWOOLE_VERSION);
     //1.7.0
     if ($version[1] >= 7) {
         $this->sw->on('ManagerStart', function ($serv) {
             global $argv;
             Swoole\Console::setProcessName('php ' . $argv[0] . ': manager');
         });
     }
     $this->sw->on('Start', array($this, 'onMasterStart'));
     $this->sw->on('ManagerStop', array($this, 'onManagerStop'));
     $this->sw->on('WorkerStart', array($this->protocol, 'onStart'));
     $this->sw->on('Connect', array($this->protocol, 'onConnect'));
     $this->sw->on('Receive', array($this->protocol, 'onReceive'));
     $this->sw->on('Close', array($this->protocol, 'onClose'));
     $this->sw->on('WorkerStop', array($this->protocol, 'onShutdown'));
     if (is_callable(array($this->protocol, 'onTimer'))) {
         $this->sw->on('Timer', array($this->protocol, 'onTimer'));
     }
     if (is_callable(array($this->protocol, 'onTask'))) {
         $this->sw->on('Task', array($this->protocol, 'onTask'));
         $this->sw->on('Finish', array($this->protocol, 'onFinish'));
     }
     $this->sw->start();
 }
Пример #3
0
 /**
  * 设定配置
  * @param array $config 配置
  * @return void
  */
 public function set($config)
 {
     if ($this->isStart) {
         return;
     }
     $this->server->set($config);
 }
Пример #4
0
 function run($host, $port)
 {
     register_shutdown_function(array($this, 'errorHandler'));
     $this->serv = new swoole_server($host, $port);
     file_put_contents(PID_FILE_NAME, posix_getpid());
     $this->serv->set(array('max_request' => 0, 'open_length_check' => true, 'package_max_length' => 81920, 'package_length_type' => 'n', 'package_length_offset' => 0, 'package_body_offset' => 2, 'worker_num' => 2));
     $this->serv->on('receive', array($this, 'onReceive'));
     $this->serv->on('close', array($this, 'onClose'));
     $this->serv->start();
 }
Пример #5
0
 public function serve()
 {
     $support_callback = ['start' => [$this, 'onStart'], 'managerStart' => [$this, 'onManagerStart'], 'workerStart' => [$this, 'onWorkerStart'], 'receive' => [$this, 'onReceive'], 'task' => null, 'finish' => null, 'workerStop' => [$this, 'onWorkerStop']];
     foreach ($support_callback as $name => $callback) {
         // If has the dependency injection
         if (is_callable(Di::get($name))) {
             $callback = Di::get($name);
         }
         if ($callback !== null) {
             $this->serv->on($name, $callback);
         }
     }
     $this->serv->set($this->swoole_config);
     $this->serv->start();
 }
Пример #6
0
 public function run()
 {
     $config = Config::get('socket');
     $config = array('worker_num' => 4, 'task_worker_num' => 8, 'max_request' => 10000, 'dispatch_mode' => 2, 'debug_mode' => 0, 'daemonize' => false);
     if (isset($argv[1]) and $argv[1] == 'daemon') {
         $config['daemonize'] = true;
     } else {
         $config['daemonize'] = false;
     }
     $serv = new \swoole_server("0.0.0.0", 8808);
     $serv->set($config);
     $serv->config = $config;
     $handler = new Handler();
     $serv->on('Start', array($handler, "start"));
     $serv->on('Connect', array($handler, "connect"));
     $serv->on('Receive', array($handler, "receive"));
     $serv->on('Close', array($handler, "close"));
     $serv->on('Shutdown', array($handler, "shutdown"));
     $serv->on('Timer', array($handler, "timer"));
     $serv->on('WorkerStart', array($handler, "workStart"));
     $serv->on('WorkerStop', array($handler, "workStop"));
     $serv->on('Task', array($handler, "task"));
     $serv->on('Finish', array($handler, "finish"));
     $serv->on('WorkerError', array($handler, "workError"));
     $serv->start();
 }
Пример #7
0
 public function serverrun($queue)
 {
     try {
         $this->_setting($queue);
     } catch (Exception $e) {
         throw new \LogicException($e->getMessage());
     }
     $serv = new swoole_server($this->host, $this->port, $this->mode);
     $serv->set($this->config);
     $serv->on('Start', array($this->objCallback, 'onStart'));
     $serv->on('Receive', array($this->objCallback, 'onReceive'));
     $serv->on('Connect', array($this->objCallback, 'onConnect'));
     $serv->on('Timer', array($this->objCallback, 'onTimer'));
     $serv->on('Task', array($this->objCallback, 'onTask'));
     $serv->on('Finish', array($this->objCallback, 'onFinish'));
     $serv->on('WorkerStart', array($this->objCallback, 'onWorkerStart'));
     $serv->on('WorkerStop', array($this->objCallback, 'onWorkerStop'));
     $onArray = array('onShutdown', 'onClose', 'onPipeMessage', 'onManagerStart', 'onManagerStop', 'onWorkerError');
     foreach ($onArray as $on) {
         if (method_exists($this->objCallback, $on)) {
             $serv->on(str_replace('on', '', $on), array($this->objCallback, $on));
         }
     }
     $serv->start();
 }
Пример #8
0
 public function actionRun()
 {
     $serv = new swoole_server("127.0.0.1", 9550);
     self::$q_config = (require 'config.php');
     $task_num = 0;
     foreach (self::$q_config as $key => $val) {
         self::$event_base[$key] = $task_num;
         self::$cnt[$key] = 0;
         $task_num += $val;
     }
     $this->config['task_worker_num'] = $task_num;
     $serv->set($this->config);
     $serv->on('Start', array($this, 'my_onStart'));
     $serv->on('Connect', array($this, 'my_onConnect'));
     $serv->on('Receive', array($this, 'my_onReceive'));
     $serv->on('Close', array($this, 'my_onClose'));
     $serv->on('Shutdown', array($this, 'my_onShutdown'));
     $serv->on('Timer', array($this, 'my_onTimer'));
     $serv->on('WorkerStart', array($this, 'my_onWorkerStart'));
     $serv->on('WorkerStop', array($this, 'my_onWorkerStop'));
     $serv->on('Task', array($this, 'my_onTask'));
     $serv->on('Finish', array($this, 'my_onFinish'));
     $serv->on('WorkerError', array($this, 'my_onWorkerError'));
     $serv->start();
 }
Пример #9
0
 function serve()
 {
     $serv = new \swoole_server(SERVERHOST, SERVERPORT);
     $serv->on('workerStart', [$this, 'onStart']);
     $serv->on('receive', [$this, 'onReceive']);
     $serv->set(array('worker_num' => 1, 'dispatch_mode' => 1, 'open_length_check' => true, 'package_max_length' => 8192000, 'package_length_type' => 'N', 'package_length_offset' => 0, 'package_body_offset' => 4));
     $serv->start();
 }
Пример #10
0
 public function __construct($host, $ip, $config, $bufferDriverClass)
 {
     $server = new \swoole_server($host, $ip, SWOOLE_PROCESS, SWOOLE_SOCK_TCP);
     $server->set($config);
     $server->on('receive', array($this, 'onReceive'));
     $server->on('close', array($this, 'onClose'));
     $this->server =& $server;
     $this->bufferDriverClass = $bufferDriverClass;
 }
Пример #11
0
 function run($setting = array())
 {
     $this->runtimeSetting = array_merge($this->runtimeSetting, $setting);
     if (self::$pidFile) {
         $this->runtimeSetting['pid_file'] = self::$pidFile;
     }
     if (!empty(self::$options['daemon'])) {
         $this->runtimeSetting['daemonize'] = true;
     }
     if (!empty(self::$options['worker'])) {
         $this->runtimeSetting['worker_num'] = intval(self::$options['worker']);
     }
     if (!empty(self::$options['thread'])) {
         $this->runtimeSetting['reator_num'] = intval(self::$options['thread']);
     }
     if (!empty(self::$options['tasker'])) {
         $this->runtimeSetting['task_worker_num'] = intval(self::$options['tasker']);
     }
     $this->sw->set($this->runtimeSetting);
     $version = explode('.', SWOOLE_VERSION);
     //1.7.0
     if ($version[1] >= 7) {
         $this->sw->on('ManagerStart', function ($serv) {
             Swoole\Console::setProcessName($this->getProcessName() . ': manager');
         });
     }
     $this->sw->on('Start', array($this, 'onMasterStart'));
     $this->sw->on('Shutdown', array($this, 'onMasterStop'));
     $this->sw->on('ManagerStop', array($this, 'onManagerStop'));
     $this->sw->on('WorkerStart', array($this, 'onWorkerStart'));
     if (is_callable(array($this->protocol, 'onTimer'))) {
         $this->sw->on('Connect', array($this->protocol, 'onConnect'));
     }
     if (is_callable(array($this->protocol, 'onTimer'))) {
         $this->sw->on('Close', array($this->protocol, 'onClose'));
     }
     if (self::$useSwooleHttpServer) {
         $this->sw->on('Request', array($this->protocol, 'onRequest'));
     } else {
         $this->sw->on('Receive', array($this->protocol, 'onReceive'));
     }
     if (is_callable(array($this->protocol, 'WorkerStop'))) {
         $this->sw->on('WorkerStop', array($this->protocol, 'WorkerStop'));
     }
     //swoole-1.8已经移除了onTimer回调函数
     if ($version[1] < 8) {
         if (is_callable(array($this->protocol, 'onTimer'))) {
             $this->sw->on('Timer', array($this->protocol, 'onTimer'));
         }
     }
     if (is_callable(array($this->protocol, 'onTask'))) {
         $this->sw->on('Task', array($this->protocol, 'onTask'));
         $this->sw->on('Finish', array($this->protocol, 'onFinish'));
     }
     $this->sw->start();
 }
Пример #12
0
 function run()
 {
     $serv = new swoole_server("127.0.0.1", 8002);
     $serv->set(array('timeout' => 1, 'poll_thread_num' => 1, 'worker_num' => 1, 'backlog' => 128, 'max_conn' => 10000, 'dispatch_mode' => 2));
     $serv->on('Receive', array($this, 'onReceive'));
     $serv->on('Close', array($this, 'onClose'));
     //swoole_server_addtimer($serv, 2);
     #swoole_server_addtimer($serv, 10);
     $serv->start();
 }
Пример #13
0
 public function __construct()
 {
     define('APPLICATION_PATH', dirname(dirname(__DIR__)) . "/application");
     define('MYPATH', dirname(APPLICATION_PATH));
     $this->application = new Yaf_Application(dirname(APPLICATION_PATH) . "/conf/application.ini");
     $this->application->bootstrap();
     $config_obj = Yaf_Registry::get("config");
     $task_config = $config_obj->task->toArray();
     $server = new swoole_server($task_config['ServerIp'], $task_config['port']);
     if (isset($task_config['logfile'])) {
         $server->set(array('worker_num' => 8, 'daemonize' => true, 'task_worker_num' => 8, 'log_file' => $task_config['logfile']));
     } else {
         $server->set(array('worker_num' => 8, 'daemonize' => true, 'task_worker_num' => 8));
     }
     $server->on('Receive', array($this, 'onReceive'));
     $server->on('Task', array($this, 'onTask'));
     $server->on('Finish', array($this, 'onFinish'));
     $server->start();
 }
Пример #14
0
 public function run()
 {
     $serv = new swoole_server('0.0.0.0', 9501);
     $serv->on('connect', array($this, 'onConnect'));
     $serv->on('receive', array($this, 'onReceive'));
     $serv->on('close', array($this, 'onClose'));
     $serv->on('workerstart', array($this, 'onWorkerStart'));
     $serv->set($this->setting);
     $serv->start();
 }
Пример #15
0
 function run()
 {
     $serv = new swoole_server("", 9509);
     $serv->set(array('worker_num' => 1, 'max_request' => 0));
     $serv->on('WorkerStart', array($this, 'onStart'));
     //$serv->on('Connect', array($this, 'onConnect'));
     $serv->on('Receive', array($this, 'onReceive'));
     //$serv->on('Close', array($this, 'onClose'));
     $serv->start();
 }
Пример #16
0
 function run()
 {
     $serv = new swoole_server("127.0.0.1", 55151);
     $serv->set(array('worker_num' => 1));
     $serv->on('WorkerStart', array($this, 'onStart'));
     //$serv->on('Connect', array($this, 'onConnect'));
     $serv->on('Receive', array($this, 'onReceive'));
     //$serv->on('Close', array($this, 'onClose'));
     $serv->start();
 }
Пример #17
0
 /**
  * 创建server 对象
  *
  * @return \swoole_server
  */
 private function createServer()
 {
     $server = new \swoole_server($this->host, $this->port, SWOOLE_PROCESS, SWOOLE_SOCK_UDP);
     $server->set($this->normalizedConfig());
     $server->on('workerStart', [$this, 'onWorkerStart']);
     $server->on('connect', [$this, 'onConnect']);
     $server->on("receive", [$this, "onReceive"]);
     $server->on('close', [$this, 'onClose']);
     return $server;
 }
Пример #18
0
 protected function setConfig()
 {
     //加载惯例配置//一次性配置,不用保存在内存中
     $defaultConfig = (include Application::GetFrameworkPath() . '/Config/Swoole.php');
     //加载自定义配置//一次性配置,不用保存在内存中
     $serverConfig = (include Application::GetServerPath() . '/Config/Swoole.php');
     //更新配置
     foreach ($defaultConfig as $key => $value) {
         if (isset($serverConfig[$key])) {
             //如果存在Server的重定义,则重定义。
             $defaultConfig[$key] = $serverConfig[$key];
         }
         if (empty($defaultConfig[$key])) {
             //如果不存在,unset之
             unset($defaultConfig[$key]);
         }
     }
     $this->settingCheck($defaultConfig);
     $this->swoole->set($defaultConfig);
 }
Пример #19
0
 function run($_setting = array())
 {
     $default_setting = array('dispatch_mode' => 3, 'max_request' => 0, 'open_eof_split' => true, 'package_eof' => self::EOF);
     define('SWOOLE_SERVER', true);
     $setting = array_merge($default_setting, $_setting);
     $serv = new \swoole_server('0.0.0.0', self::PORT, SWOOLE_PROCESS);
     $serv->set($setting);
     $serv->on('receive', array($this, 'onReceive'));
     $this->serv = $serv;
     $this->serv->start();
 }
Пример #20
0
 function run($_setting = array())
 {
     $default_setting = array('worker_num' => 4, 'open_eof_check' => true, 'open_eof_split' => true, 'package_eof' => self::EOF);
     $this->pid_file = $_setting['pid_file'];
     $setting = array_merge($default_setting, $_setting);
     $serv = new \swoole_server('0.0.0.0', self::SVR_PORT_AOP, SWOOLE_PROCESS, SWOOLE_TCP);
     $serv->set($setting);
     $serv->on('receive', array($this, 'onReceive'));
     $this->serv = $serv;
     $this->serv->start();
 }
Пример #21
0
 /**
  * server run
  */
 function run()
 {
     $serv = new swoole_server('0.0.0.0', 9501, SWOOLE_BASE);
     $serv->set(array('worker_num' => 3, 'daemonize' => true));
     $this->serv = $serv;
     $serv->on('receive', array($this, 'onReceive'));
     $serv->on('close', array($this, 'onClose'));
     $serv->on('start', array($this, 'onStart'));
     $serv->on('WorkerStart', array($this, 'onWorkerStart'));
     $serv->start();
 }
Пример #22
0
 public function __construct()
 {
     define('APPLICATION_PATH', dirname(dirname(__DIR__)) . "/application");
     $this->application = new Yaf_Application(dirname(APPLICATION_PATH) . "/conf/application.ini");
     $this->application->bootstrap();
     $server = new swoole_server("0.0.0.0", 9503);
     $server->set(array('worker_num' => 8, 'daemonize' => true, 'task_worker_num' => 8));
     $server->on('Receive', array($this, 'onReceive'));
     $server->on('Task', array($this, 'onTask'));
     $server->on('Finish', array($this, 'onFinish'));
     $server->start();
 }
Пример #23
0
 /**
  * @param $handel
  */
 public function run()
 {
     $this->server = new \swoole_server($this->host, $this->port, SWOOLE_BASE, SWOOLE_SOCK_TCP);
     $this->server->set($this->config);
     $this->server->on('Start', [$this, 'onMasterStart']);
     $this->server->on('Shutdown', [$this, 'onMasterStop']);
     $this->server->on('ManagerStop', [$this, 'onManagerStop']);
     $this->server->on('WorkerStart', [$this, 'onWorkerStart']);
     $this->server->on('Connect', [$this->handel, 'onConnect']);
     $this->server->on('Receive', [$this->handel, 'onReceive']);
     $this->server->on('Close', [$this->handel, 'onClose']);
     $this->server->on('WorkerStop', [$this->handel, 'onStop']);
     if (is_callable([$this->handel, "onTimer"])) {
         $this->server->on("Timer", [$this->handel, "onTimer"]);
     }
     if (is_callable([$this->handel, 'onTask'])) {
         $this->server->on("Timer", [$this->handel, "onTimer"]);
         $this->server->on("Finish", [$this->handel, "onFinish"]);
     }
     $this->server->start();
 }
Пример #24
0
 function run()
 {
     $serv = new swoole_server("0.0.0.0", 10000);
     $serv->set(array('timeout' => 1, 'poll_thread_num' => 1, 'worker_num' => 2, 'backlog' => 128, 'max_conn' => 10000, 'dispatch_mode' => 2));
     $serv->on('WorkerStart', array($this, 'onStart'));
     $serv->on('Connect', array($this, 'onConnect'));
     $serv->on('Receive', array($this, 'onReceive'));
     $serv->on('Close', array($this, 'onClose'));
     $serv->on('WorkerStop', array($this, 'onShutdown'));
     //swoole_server_addtimer($serv, 2);
     #swoole_server_addtimer($serv, 10);
     $serv->start();
 }
Пример #25
0
 public function start()
 {
     $this->setTitle('proxy');
     $type = $this->option['type'] == 'UDP' ? SWOOLE_SOCK_UDP : SWOOLE_SOCK_TCP;
     $server = new \swoole_server($this->option['host'], $this->option['port'], SWOOLE_PROCESS, $type);
     $server->set($this->option['setting']);
     $event = ['Start', 'Receive', 'Close', 'Shutdown', 'Timer', 'WorkerStart', 'WorkerStop', 'Task', 'Finish', 'WorkerError', 'MasterConnect', 'MasterClose', 'Connect'];
     foreach ($event as $event) {
         $server->on($event, [$this, sprintf('on%s', $event)]);
     }
     $this->server = $server;
     $this->server->start();
 }
Пример #26
0
 public function __construct()
 {
     //初始化应用
     $this->initializationOfApp();
     // 创建swoole_http_server对象
     $server = new swoole_server("0.0.0.0", 9504, SWOOLE_PROCESS, SWOOLE_SOCK_UDP);
     // 设置参数
     $server->set(array('worker_num' => 4, 'dispatch_mode' => 2, 'task_worker_num' => 4));
     $server->on('receive', array($this, 'onReceive'));
     $server->on('task', array($this, 'onTask'));
     $server->on('finish', array($this, 'onFinish'));
     $server->start();
 }
 public function start($setting = array())
 {
     Trace::info(str_repeat('-', 100));
     Trace::info(str_pad('[ ShadowSocks Swoole PHP ]', 100, ' ', STR_PAD_BOTH));
     Trace::info(str_pad("IP:" . self::$conf['ip'] . " PORT:" . self::$conf['port'], 100, ' ', STR_PAD_BOTH));
     Trace::info(str_pad("PassWord : "******"Encode :" . self::$conf['method'], 100, ' ', STR_PAD_BOTH));
     Trace::info(str_repeat('-', 100));
     $default = ['timeout' => 1, 'poll_thread_num' => 1, 'worker_num' => 4, 'backlog' => 128, 'dispatch_mode' => 2];
     $setting = array_merge($default, $setting);
     $this->serv->set($setting);
     $this->serv->start();
 }
Пример #28
0
 public function index()
 {
     $server = new \swoole_server(C('SERVICE_IP'), C('SERVICE_PORT'));
     $server->set(array('task_worker_num' => 4, 'daemonize' => false, 'heartbeat_check_interval' => 30, 'heartbeat_idle_time' => 60));
     $server->on('WorkerStart', array($this, 'onWorkerStart'));
     $server->on('Receive', array($this, 'onReceive'));
     $server->on('Task', array($this, 'onTask'));
     $server->on('Finish', array($this, 'onFinish'));
     $server->on('pipeMessage', array($this, 'onPipeMessage'));
     $server->on('WorkerError', array($this, 'onWorkerError'));
     $this->conn = array();
     $server->start();
 }
Пример #29
0
 function listen($host = '0.0.0.0', $port = 9510)
 {
     $swoole_setting = (require dirname(__DIR__) . '/configs/swoole.php');
     $server = new swoole_server($host, $port, SWOOLE_BASE);
     $swoole_setting['open_eof_check'] = true;
     $swoole_setting['open_eof_split'] = true;
     $swoole_setting['package_eof'] = self::EOF;
     $server->set($swoole_setting);
     $server->on('WorkerStart', [$this, 'onStart']);
     $server->on('WorkerStop', [$this, 'onStop']);
     $server->on('receive', [$this, 'onReceive']);
     $this->server = $server;
     $this->server->start();
 }
Пример #30
0
 function run()
 {
     $serv = new swoole_server("0.0.0.0", 9507);
     $serv->set(array('worker_num' => 1));
     $serv->on('Start', function ($serv) {
         echo "Swoole Upload Server running\n";
     });
     $this->root_path = rtrim($this->root_path, ' /');
     $serv->on('connect', array($this, 'onConnect'));
     $serv->on('receive', array($this, 'onreceive'));
     $serv->on('close', array($this, 'onclose'));
     $this->serv = $serv;
     $serv->start();
 }