public function __Construct() { $this->_store = Store::instance('game'); $this->_db = Db::instance('passport'); $this->_pbMember = new PbMember(); $this->_pbResult = new PbResult(); }
/** * @return mixed */ public static function getServerList() { $store = Store::instance('game'); $db = Db::instance('passport'); $key = "GAME_ZONE_SERVER_LIST"; $serverList = $store->get($key); if ($serverList) { return $serverList; } $sql = "SELECT s_id,s_name,s_type,s_ip FROM `t_server`\n WHERE s_status=1\n ORDER BY s_id ASC"; $result = $db->query($sql); if ($result) { $store->set($key, $result); } return $result; }
/** * 想某个用户网关发送命令和消息 * @param int $client_id * @param int $cmd * @param string $message * @return boolean */ protected static function sendCmdAndMessageToClient($client_id, $cmd, $message) { $pack = new GatewayProtocol(); $pack->header['cmd'] = $cmd; // 如果是发给当前用户则直接获取上下文中的地址 if ($client_id === Context::$client_id || $client_id === null) { $pack->header['local_ip'] = Context::$local_ip; $pack->header['local_port'] = Context::$local_port; $pack->header['socket_id'] = Context::$socket_id; $pack->header['client_id'] = Context::$client_id; } else { $address = Store::instance('gateway')->get($client_id); if (!$address) { return false; } $pack->header['local_ip'] = $address['local_ip']; $pack->header['local_port'] = $address['local_port']; $pack->header['socket_id'] = $address['socket_id']; $pack->header['client_id'] = $client_id; } $pack->header['client_ip'] = Context::$client_ip; $pack->header['client_port'] = Context::$client_port; $pack->body = (string) $message; return self::sendToGateway("{$pack->header['local_ip']}:{$pack->header['local_port']}", $pack->getBuffer()); }
/** * 定时检查gateway通信端口,如果有新的gateway则去建立长连接 */ public function checkGatewayConnections() { $key = 'GLOBAL_GATEWAY_ADDRESS'; $addresses_list = Store::instance('gateway')->get($key); if (empty($addresses_list)) { return; } // 循环遍历,查找未连接的gateway ip 端口 foreach ($addresses_list as $addr) { if (!isset($this->gatewayConnections[$addr])) { // 执行连接 $conn = @stream_socket_client("tcp://{$addr}", $errno, $errstr, 1); if (!$conn) { if (!isset($this->badGatewayAddress[$addr])) { $this->badGatewayAddress[$addr] = 0; } // 删除连不上的端口 if ($this->badGatewayAddress[$addr]++ > self::MAX_RETRY_COUNT) { \Man\Core\Lib\Mutex::get(); $addresses_list = Store::instance('gateway')->get($key); unset($addresses_list[$addr]); Store::instance('gateway')->set($key, $addresses_list); $this->notice("tcp://{$addr} " . $errstr . " del {$addr} from store", false); \Man\Core\Lib\Mutex::release(); } continue; } unset($this->badGatewayAddress[$addr]); $this->gatewayConnections[$addr] = $conn; stream_set_blocking($this->gatewayConnections[$addr], 0); // 初始化一些值 $fd = (int) $this->gatewayConnections[$addr]; $this->connections[$fd] = $this->gatewayConnections[$addr]; $this->recvBuffers[$fd] = array('buf' => '', 'remain_len' => $this->prereadLength); // 添加数据可读事件 $this->event->add($this->connections[$fd], \Man\Core\Events\BaseEvent::EV_READ, array($this, 'dealInputBase'), $fd); } } }
/** * 删除客户端内部通讯地址 * @param int $global_client_id * @param string $address */ protected function deleteClientAddress($global_client_id) { StatisticClient::tick(__CLASS__, 'deleteClientAddress'); if (!Store::instance('gateway')->delete($global_client_id)) { $msg = "删除客户端通讯地址出错"; if (get_class(Store::instance('gateway')) == 'Memcached') { $msg .= " 原因:" . Store::instance('gateway')->getResultMessage(); } $this->notice($msg); return StatisticClient::report(__CLASS__, 'deleteClientAddress', 0, 106, new \Exception($msg)); } StatisticClient::report(__CLASS__, 'deleteClientAddress', 1, 0, ''); }
public function __Construct() { $this->_store = Store::instance('game'); parent::__Construct(); }
/** * 进程停止时,清除一些数据 * @see Man\Core.SocketWorker::onStop() */ public function onStop() { $this->unregisterAddress($this->lanIp . ':' . $this->lanPort); foreach ($this->connUidMap as $uid) { Store::instance('gateway')->delete($uid); } }
/** * 删除用户的网关地址 * @param int $uid */ public static function deleteUidAddress($uid) { return Store::instance('gateway')->delete($uid); }
<?php /** * workerman启动前的清除脚本 * @author walkor <*****@*****.**> */ require_once __DIR__ . '/../Lib/Store.php'; require_once __DIR__ . '/../Config/Store.php'; require_once __DIR__ . '/../Lib/StoreDriver/File.php'; use Lib\Store; // 文件存储驱动,删除文件 if (\Config\Store::$driver == \Config\Store::DRIVER_FILE) { Store::instance('gateway')->destroy(); } else { $key = 'GLOBAL_GATEWAY_ADDRESS'; Store::instance('gateway')->delete($key); }
/** * 添加到用户列表中 * @param int $uid * @param string $name */ public static function addUserToList($uid, $name) { $key = 'alluserlist'; $user_list = self::getUserList(); if (!isset($user_list[$uid])) { $user_list[$uid] = $name; return Store::instance('gateway')->set($key, $user_list); } return true; }
public function __Construct() { $this->_store = Store::instance('game'); $this->_db = Db::instance('passport'); }