/** * 设置消息回调 * @return void */ public function onMessage($connection, $data) { // 删除定时器 Timer::del($connection->timeout_timerid); $data = json_decode($data, true); $event = $data['event']; // 开始验证 switch ($event) { // 是geteway连接 case 'gateway_connect': if (empty($data['address'])) { echo "address not found\n"; return $connection->close(); } $this->_gatewayConnections[$connection->id] = $data['address']; $this->broadcastAddresses(); break; // 是worker连接 // 是worker连接 case 'worker_connect': $this->_workerConnections[$connection->id] = $connection; $this->broadcastAddresses($connection); break; case 'ping': break; default: echo "unknown event {$event}\n"; $connection->close(); } }
public static function clearTimer() { if (self::$_reconnectTimer) { Timer::del(self::$_reconnectTimer); self::$_reconnectTimer = null; } }
public function destroy() { if ($this->blinkTimeout) { Timer::del($this->blinkTimeout); $this->blinkTimeout = 0; } if ($this->despawnTimeout) { Timer::del($this->despawnTimeout); $this->despawnTimeout = 0; } if ($this->isStatic) { $this->scheduleRespawn(30000); } }
public function increaseHateFor($playerId, $points) { if ($this->hates($playerId)) { $obj = Utils::detect($this->hatelist, function ($obj) use($playerId) { return $obj->id == $playerId; }); if ($obj) { $obj->hate += $points; } } else { $this->hatelist[] = (object) array('id' => $playerId, 'hate' => $points); } if ($this->returnTimeout) { // Prevent the mob from returning to its spawning position // since it has aggroed a new player Timer::del($this->returnTimeout); $this->returnTimeout = null; } }
public static function checkWorkerStatus() { foreach (self::$_process as $process_data) { $process = $process_data[0]; $start_file = $process_data[1]; $timer_id = $process_data[2]; $status = proc_get_status($process); if (isset($status['running'])) { // 子进程退出了,重启一个子进程 if (!$status['running']) { echo "process {$start_file} terminated and try to restart\n"; Timer::del($timer_id); @proc_close($process); // 重新打开一个子进程 self::openProcess($start_file); } } else { echo "proc_get_status fail\n"; } } }
/** * 当用户断开连接时触发 * @param int $client_id * @return void */ public static function onClose($client_id) { \Workerman\Lib\Timer::del(self::$redisConnection->get($client_id)); self::$redisConnection->del($client_id); // 更新MYSQL数据库 self::$connectHC->query("UPDATE `HC` SET `clientid` = 0, `lastouttime` = CURRENT_TIMESTAMP() WHERE clientid='{$client_id}'"); }
case 'heartbeat': // 评测端信息更新 heartbeat($connection, $data); break; case 'update_state': // 评测中更新状态 update_state($connection, $data); break; case 'update': // 评测完更新结果 update($connection, $data); break; case 'login': // 评测端登录 login($connection, $data); break; default: p("Unknown Action ( cid = {$connection->cid}, IP = {$connection->IP} )"); } }; $worker_tasker->onClose = function ($connection) { global $cid, $ava_client, $client_sorted; if ($connection->deadline) { Timer::del($connection->deadline); } if (isset($cid[$connection->cid])) { unset($cid[$connection->cid]); $client_sorted = False; } p("A client closed the connection. ( cid = {$connection->cid}, IP = {$connection->IP} )"); };
public function onClose($reason = '', $description = null) { if ('closed' !== $this->readyState) { Timer::del($this->pingTimeoutTimer); Timer::del($this->checkIntervalTimer); $this->checkIntervalTimer = null; Timer::del($this->upgradeTimeoutTimer); // clean writeBuffer in next tick, so developers can still // grab the writeBuffer on 'close' event $this->writeBuffer = array(); $this->packetsFn = array(); $this->sentCallbackFn = array(); $this->clearTransport(); $this->readyState = 'closed'; $this->emit('close', $this->id, $reason, $description); $this->server = null; $this->request = null; $this->upgradeTransport = null; $this->removeAllListeners(); if (!empty($this->transport)) { $this->transport->removeAllListeners(); $this->transport = null; } } }
/** * Websocket handshake. * * @param string $buffer * @param \Workerman\Connection\TcpConnection $connection * @return int */ public static function dealHandshake($buffer, $connection) { $pos = strpos($buffer, "\r\n\r\n"); if ($pos) { // handshake complete $connection->handshakeStep = 2; $handshake_response_length = $pos + 4; // Try to emit onWebSocketConnect callback. if (isset($connection->onWebSocketConnect)) { try { call_user_func($connection->onWebSocketConnect, $connection, substr($buffer, 0, $handshake_response_length)); } catch (\Exception $e) { Worker::log($e); exit(250); } catch (\Error $e) { Worker::log($e); exit(250); } } // Headbeat. if (!empty($connection->websocketPingInterval)) { $connection->websocketPingTimer = Timer::add($connection->websocketPingInterval, function () use($connection) { if (false === $connection->send(pack('H*', '8900'), true)) { Timer::del($connection->websocketPingTimer); $connection->websocketPingTimer = null; } }); } $connection->consumeRecvBuffer($handshake_response_length); if (!empty($connection->tmpWebsocketData)) { $connection->send($connection->tmpWebsocketData, true); $connection->tmpWebsocketData = ''; } if (strlen($buffer) > $handshake_response_length) { return self::input(substr($buffer, $handshake_response_length), $connection); } } return 0; }
function login($connection, $data) { global $db, $cid, $ava_client, $client_sorted; if (!isset($data['client_id'], $data['client_hash']) || $connection->cid) { p("Bad Login Action ( IP = {$connection->IP} )"); cut($connection, 'refuse'); return False; } $sql = 'SELECT name FROM mo_judge_client WHERE id = ? AND hash = ?'; $mark = $db->prepare($sql); $db->bind($mark, 'is', $data['client_id'], $data['client_hash']); $result = $db->execute($mark); if (!$result) { p("Bad Client ID or Hash ( IP = {$connection->IP} )"); cut($connection, 'refuse'); return False; } Timer::del($connection->deadline); $connection->deadline = 0; if (isset($cid[$data['client_id']])) { cut($cid[$data['client_id']], 'another'); unset($cid[$data['client_id']]); } $connection->cid = (string) $data['client_id']; $connection->name = $result[0]['name']; $cid[$connection->cid] = $connection; sendMsg($connection, array('action' => 'admit', 'client_name' => $result[0]['name'])); $client_sorted = False; p("The client <{$connection->name}> has joined. ( cid = {$connection->cid}, IP = {$connection->IP} )"); return True; }
public function resetTimeout() { Timer::del($this->disconnectTimeout); // 15分钟 $this->disconnectTimeout = Timer::add(15 * 60, array($this, 'timeout'), false); }
/** * 废弃桌子 */ public function rmTable() { if ($this->blinkTimeOut) { Timer::del($this->blinkTimeOut); } $this->blinkTimeOut = 0; foreach ($this->uids as $uid) { $player = PlayerDao::getPlayer($uid); if ($player && $player->tableId == $this->tableId) { PlayerDao::rmPlayer($uid); } } TableDao::rmTable($this->tableId); }
/** * Reconnect. * * @param int $after * @return void */ public function reConnect($after = 0) { $this->_status = self::STATUS_INITIAL; if ($this->_reconnectTimer) { Timer::del($this->_reconnectTimer); } if ($after > 0) { $this->_reconnectTimer = Timer::add($after, array($this, 'connect'), null, false); return; } return $this->connect(); }