MyQEE\Server\Clusters\Host::getHostByFd PHP Method

getHostByFd() public static method

根据FD获取服务器
public static getHostByFd ( $fd ) : boolean | Host
$fd
return boolean | Host
    public static function getHostByFd($fd)
    {
        $rs = self::$fdToIdTable->get($fd);
        if ($rs) {
            $rs = self::$table->get("{$rs['group']}_{$rs['id']}");
            if ($rs) {
                return self::initHostByData($rs);
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

Usage Example

Example #1
0
 public function onClose($server, $fd, $fromId)
 {
     $host = Host::getHostByFd($fd);
     if ($host) {
         # 已经有连接上的服务器
         Server::$instance->info("host#{$host->group}.{$host->id}({$host->ip}:{$host->port}) close connection.");
         $host->remove();
         # 移除服务
         foreach (Host::$table as $item) {
             if ($item['group'] === $host->group && $item['id'] === $host->id || $item['removed']) {
                 continue;
             }
             # 通知所有客户端移除
             RPC::factory($item['fd'], $item['from_id'])->trigger('server.remove', $host->group, $host->id);
         }
     }
 }