主进程启动

@param  \Swoole\Server   $server
346603
public function onMasterStart(SwServer $server)
    {
        // {{
        $this->setRunUser();
        $this->setProcessName('master');
        // }}

        // {{ log
        $this->get('logger')->info('启动 Master 进程', [
            'pid' => $server->master_pid
        ]);
        // }}
    }
691775
public function onStart(Server $server): void
    {
        $this->fire(ServerEvent::STARTED, $server);

        $this->masterPid = $masterPid = $server->master_pid;
        $rootPath        = $this->config('rootPath');
        $rootPath        = $rootPath ? " (at $rootPath)" : '';

        // save master process id to file.
        ProcessUtil::setTitle(sprintf('%s: master%s', $this->name, $rootPath));

        $this->log("The <comment>master</comment> process success started. (PID:<info>{$masterPid}</info>, pidFile: $this->pidFile)");
    }
861150
public function init($server = null)
    {
        $server = $server ?: $_SERVER;

        $this->set('args', array());
        if (isset($server['argv'])) {
            // first argument is script name.
            $args = $server['argv'];
            $script = array_shift($args);
            $this->script_name = $script;

            // parse options.
            foreach ($args as $arg) {
                // is long option
                // --foo
                //   => foo=true
                // --foo=bar
                //   => foo=bar
                if (preg_match('/^--(..+)/', $arg, $matches)) {
                    $option = explode('=', $matches[1]);
                    $key = array_shift($option);
                    $value = $option ? join('=', $option) : true;
                    $this->add($key, $value);
                }

                // is short option
                // -abc
                //   => option.a=true, option.b=true, option.c=true
                // -abc=foo
                //   => option.a=true, option.b=true, option.c=foo
                elseif (preg_match('/^-(.+)/', $arg, $matches)) {
                    $option = $matches[1];
                    for ($i = 0; $i < strlen($option); $i++) {
                        $j = $i + 1;
                        $key = $option[$i];
                        if (isset($option[$j]) && $option[$j] === '=') {
                            $value = substr($option, $j + 1);
                            $this->set('option.' . $key, $value);
                            break;
                        } else {
                            $value = true;
                            $this->set('option.' . $key, $value);
                        }
                    }
                }
                // is else to args.
                else {
                    $this->add('args', $arg);
                }
            }
        }
    }
386522
public function server(array $server)
    {
        foreach ((array) $server as $key => $value)
        {
            $string = strtolower(substr($key, 5));

            $key = str_replace('_', '-', $string);

            if (strpos($key, 'HTTP_') === 0)
            {
                $this->headers[$key] = $value;
            }
        }
    }
65521
public function execute(Server $server)
    {
        $options = [];

        if (isset($this->options['bypassDocumentValidation']) && \MongoDB\server_supports_feature($server, self::$wireVersionForDocumentLevelValidation)) {
            $options['bypassDocumentValidation'] = $this->options['bypassDocumentValidation'];
        }

        $bulk = new Bulk($options);
        $insertedId = $bulk->insert($this->document);

        $writeResult = $server->executeBulkWrite($this->databaseName . '.' . $this->collectionName, $bulk, $this->createOptions());

        return new InsertOneResult($writeResult, $insertedId);
    }
970773
private function processServerVariables(array $server)
    {
        foreach ($server as $key => $value) {
            $this->server[$key] = $value;

            if (strpos($key, 'HTTP_') === 0) {
                $key = str_replace('_', '-', substr($key, 5));

                $this->headers[$key] = $value;
            }
        }

        $uri     = $server['REQUEST_URI'];
        $uriPath = parse_url($uri, PHP_URL_PATH);

        $this->server['REQUEST_URI']      = $uri;
        $this->server['REQUEST_URI_PATH'] = $uriPath;

        $this->isEncrypted = !(empty($server['HTTPS']) || strcasecmp($server['HTTPS'], 'off') === 0);

        $this->server['SERVER_PROTOCOL'] = substr($server['SERVER_PROTOCOL'], -3);

        if (isset($server['CONTENT_LENGTH'])) {
            $this->headers['CONTENT-LENGTH'] = $server['CONTENT_LENGTH'];
        }

        if (isset($server['CONTENT_TYPE'])) {
            $this->headers['CONTENT-TYPE'] = $server['CONTENT_TYPE'];
        }
    }
1372581
public function onOpen(\Swoole\WebSocket\Server $server, \Swoole\Http\Request $request)
    {
        // echo "[websocket][onopen]server: handshake success with fd{$request->fd} url={$request->server['request_uri']}?{$request->server['query_string']}\n";

        // Compat fpm server
        $this->_compatFPM($request);

        // print_r(self::$websocketFrameContext);

        // filter websocket router class
        // route struct: [$controller_name, $controller_class]
        $controller_class = (\ePHP\Core\Route::init())->findWebSocketRoute();
        if ( !empty($controller_class) ) {
            // Save websocket connection Context
            self::$websocketFrameContext[$request->fd] = [
                'get'    => $_GET,
                'cookie' => $_COOKIE,
                'controller_class' => $controller_class
            ];

            // echo 'pid='. getmypid() . ', fd=' . implode(',', array_keys(self::$websocketFrameContext))
            //     . ', connections=' . count($this->server->connections) . "\n";

            call_user_func([new $controller_class(), 'onOpen'], $server, $request);
        }

    }
1008517
private static function setServerParam($server, $arr)
    {
        foreach ($arr as $key => $value) {
            if (array_key_exists($value, $server)) {
                $server[$key] = $server[$value];
            }
        }

        return $server;
    }
1038532
protected function createListenServers(Server $server): void
    {
        $this->fire(ServerEvent::PORT_CREATE, $this);

        foreach ($this->attachedListeners as $name => $cb) {
            $info = '';

            if ($cb instanceof \Closure) {
                $port = $cb($server, $this);
            } else {
                /**
                 * @var ServerInterface       $this
                 * @var PortListenerInterface $cb
                 */
                $port = $cb->attachTo($this, $server);
            }

            if ($port) {
                $type = $port->type === SWOOLE_SOCK_TCP ? 'tcp' : 'udp';
                $info = "(<cyan>$type://{$port->host}:{$port->port}</cyan>)";
            }

            $this->log("Attach the port listen server <info>$name</info>$info to the main server");
        }

        $this->attachedListeners = []; // reset.
        $this->fire(ServerEvent::PORT_CREATED, [$this]);
    }
1254841
public function onManagerStop(swoole_server $server)
    {
        $this->console(sprintf('Server <info>%s</info> Manager[<info>%s</info>] is shutdown.', $this->name, $server->manager_pid), OutputInterface::VERBOSITY_DEBUG);
    }
679635
public function setUp(Server $server, $type)
    {

        if (in_array($type, $this->hasBeenSetUp)) {
            return;
        }
        $this->hasBeenSetUp[] = $type;

        if (isset($this->alias[$type])) {
            foreach ($this->alias[$type] as $target) {
                $this->setUp($server, $target);
            }
            return;
        }
        if (!class_exists($type) && !interface_exists($type)) {
            return;
        }
        $methods = (new ReflectionClass($type))->getMethods();
        $this->functions[$type] = array_map(function (ReflectionMethod $method) {
            return $method->getName();
        }, $methods);

        if (!isset($this->functionWhitelist[$type])) {
            return;
        }

        foreach ($this->functionWhitelist[$type] as $k => $fn) {
            if (in_array($fn, $this->functions[$type]) || (isset($this->customFunctions[$type], $this->customFunctions[$type][$fn]))) {
                continue;
            }
            unset($this->functionWhitelist[$type][$k]);
        }

    }
1092779
public function onStart(swoole_server $server)
    {
        if (version_compare(SWOOLE_VERSION, '1.9.5', '<')) {
            file_put_contents($this->pidFile, $server->master_pid);
            $this->pid = $server->master_pid;
        }

        process_rename($this->name . ' master');

        $this->output->writeln(sprintf("Listen: <info>%s://%s:%s</info>", $this->getScheme(), $this->getHost(), $this->getPort()));
        foreach ($this->listens as $listen) {
            $this->output->writeln(sprintf(" <info> ></info> Listen: <info>%s://%s:%s</info>", $listen->getScheme(), $listen->getHost(), $listen->getPort()));
        }

        $this->output->writeln(sprintf('PID file: <info>%s</info>, PID: <info>%s</info>', $this->pidFile, $server->master_pid));
        $this->output->writeln(sprintf('Server Master[<info>%s</info>] is started', $server->master_pid), OutputInterface::VERBOSITY_DEBUG);
    }
1216562
public function __shutdown(\swoole\Server $server) {
        $path = $this->options['enable_pid'];
        file_exists($path) and unlink($path);

        if(method_exists($this->callback,'shutdown')) {
            $this->callback->shutdown($server);
        }
    }
1237756
function setServer($server)
    {
        $this->server = new DataEntity(
            $this->_normalizeServer($server)
        );
        
        return $this->server;
    }
1334560
public static function workerStartApp($server, $target)
    {
        if(!static::$workerStartAppComplete)
        {
            // 触发项目的workerstart事件
            Event::trigger('IMI.MAIN_SERVER.WORKER.START.APP', [
                'server'    => $server,
                'workerID'  => static::$workerID,
            ], $target, WorkerStartEventParam::class);
            static::$workerStartAppComplete = true;
        }
    }
1336534
public function onTask(SwServer $server, int $taskId, int $fromId, $data)
    {
        // {{ log
        $this->get('logger')->info('投递任务', [
            'id' => $taskId,
            'fromId' => $fromId,
            'data' => is_array($data) ? json_encode($data) : $data
        ]);
        // }}

        // {{
        return $this->get('task')->onTask($server, $taskId, $fromId, $data);
        // }}
    }
1104572
private function setServer()
    {
        if (isset($_SERVER)) {
            foreach ($_SERVER as $k => $v) {
                $this->log->debug("[ SERVER Params ]" . $k . ": " . $v, []);
                $this->server->$k = $v;
                $this->parameters["server"][$k] = $v;
            }
            unset($_SERVER);
        }
    }