/** * 处理受到的数据 * @param TcpConnection $connection * @return void */ public function onConnect($connection) { $socket = $connection->getSocket(); $t_socket = new Thrift\Transport\TSocket(); $t_socket->setHandle($socket); $transport_name = '\\Thrift\\Transport\\' . $this->thriftTransport; $transport = new $transport_name($t_socket); $protocol_name = '\\Thrift\\Protocol\\' . $this->thriftProtocol; $protocol = new $protocol_name($transport); // 执行处理 try { // 先初始化一个 $protocol->fname == 'none'; // 统计开始时间 \Thrift\Statistics\StatisticClient::tick(); // 业务处理 $this->processor->process($protocol, $protocol); \Thrift\Statistics\StatisticClient::report($this->name, $protocol->fname, 1, 0, '', $this->statisticAddress); } catch (\Exception $e) { \Thrift\Statistics\StatisticClient::report($this->name, $protocol->fname, 0, $e->getCode(), $e, $this->statisticAddress); ThriftWorker::log('CODE:' . $e->getCode() . ' MESSAGE:' . $e->getMessage() . "\n" . $e->getTraceAsString() . "\nCLIENT_IP:" . $connection->getRemoteIp() . "\n"); $connection->send($e->getMessage()); } }
/** * 进程启动时的一些初始化 * @see PHPServerWorker::onServe() */ public function onServe() { // 初始化thrift生成文件存放目录 $provider_dir = PHPServerConfig::get('workers.' . $this->serviceName . '.provider'); if ($provider_dir) { if ($this->providerDir = realpath($provider_dir)) { if ($path_array = explode('/', $this->providerDir)) { $this->providerNamespace = $path_array[count($path_array) - 1]; } } else { $this->providerDir = $provider_dir; $this->notice('provider_dir ' . $provider_dir . ' not exsits'); } } // 初始化thrift生成类业务实现存放目录 $handler_dir = PHPServerConfig::get('workers.' . $this->serviceName . '.handler'); if ($handler_dir) { if ($this->handlerDir = realpath($handler_dir)) { if ($path_array = explode('/', $this->handlerDir)) { $this->handlerNamespace = $path_array[count($path_array) - 1]; } } else { $this->handlerDir = $handler_dir; $this->notice('handler_dir' . $handler_dir . ' not exsits'); } } else { $this->handlerDir = $provider_dir; } // 统一日志类初始化 require_once SERVER_BASE . 'thirdparty/MNLogger/MNLogger.php'; $logdir = PHPServerConfig::get('monitor_log_path') ? PHPServerConfig::get('monitor_log_path') : '/home/logs/monitor'; $config = array('on' => true, 'app' => 'php-rpc-server', 'logdir' => $logdir); try { self::$rpcMNLogger = @thirdparty\MNLogger\MNLogger::instance($config); } catch (Exception $e) { } // 初始化统计上报地址 $report_address = PHPServerConfig::get('workers.' . $this->serviceName . '.report_address'); if ($report_address) { StatisticClient::config(array('report_address' => $report_address)); } else { if ($config = PHPServerConfig::get('workers.StatisticWorker')) { if (!isset($config['ip'])) { $config['ip'] = '127.0.0.1'; } StatisticClient::config(array('report_address' => 'udp://' . $config['ip'] . ':' . $config['port'])); } } // 业务引导程序bootstrap初始化(没有则忽略) $bootstrap = PHPServerConfig::get('workers.' . $this->serviceName . '.bootstrap'); if (is_file($bootstrap)) { require_once $bootstrap; } // 服务名 self::$appName = $this->serviceName; }