/** * 初始化执行 * * @param bool $isRegisterServer */ public static function init($isRegisterServer = false) { if (self::$table) { return; } self::$isRegisterServer = $isRegisterServer ? true : false; if (isset(Server::$config['clusters']['count']) && ($size = Server::$config['clusters']['count'])) { # 必须是2的指数, 如1024,8192,65536等 $size = bindec(str_pad(1, strlen(decbin((int) $size - 1)), 0)) * 2; } else { $size = 1024; } $table = new Table($size * 2); $table->column('id', Table::TYPE_INT, 5); // 所在组ID $table->column('group', Table::TYPE_STRING, 64); // 分组 $table->column('ip', Table::TYPE_STRING, 64); // IP $table->column('port', Table::TYPE_INT, 5); // 端口 $table->column('worker_num', Table::TYPE_INT, 5); // 进程数 $table->column('encrypt', Table::TYPE_INT, 1); // 通讯数据是否加密 $table->column('key', Table::TYPE_STRING, 32); // 数据加密密钥 if (self::$isRegisterServer) { # 注册服务器需要多几个字段 $table->column('fd', Table::TYPE_INT, 10); // 所在 fd $table->column('from_id', Table::TYPE_INT, 10); // 所在 from_id $table->column('removed', Table::TYPE_INT, 10); // 移除时间 # 记录自动分配ID $groupIdTable = new Table(1024); $groupIdTable->column('id', Table::TYPE_INT, 5); $groupIdTable->create(); # fd 所对应的序号 $fdTable = new Table($size * 2); $fdTable->column('group', Table::TYPE_STRING, 64); $fdTable->column('id', Table::TYPE_INT, 5); $fdTable->create(); self::$fdToIdTable = $fdTable; self::$groupIdTable = $groupIdTable; } $table->create(); self::$table = $table; self::$lastChangeTime = new \Swoole\Atomic(time()); self::$taskIdAtomic = new \Swoole\Atomic(); }
/** * 设置字段类型 * * @param $name * @param $type * @param int $size */ public function column($name, $type = null, $size = null) { if ($this->_type === 'mysql' || $this->_type === 'sqlite') { # 记录下类型 $this->_column[$name] = [$type, $size]; } return parent::column($name, $type, $size); }