示例#1
0
 private static function mysql_recv_block(&$requestData)
 {
     global $srv_block_ini;
     $total = $srv_block_ini['breath_delay'];
     $requestData = array();
     $ret = 0;
     $cid_2_drone = array();
     $num = 0;
     foreach (self::$_result as $uniqu => $a) {
         if (!$a['lasterror']) {
             $cid_2_drone[$a['cid']] = $uniqu;
             $num++;
         }
     }
     $c_complete_array = array();
     //$num = 1;
     while ($num) {
         $c_new_gets = false;
         $db = GlobalFunc::connect_db(self::$_mysql_ini);
         if (!$db) {
             $ret = -1;
         } else {
             $query = 'select cid,status,module from ' . self::$_mysql_ini['prefix'] . 'online_task where tid=\'' . self::$_tid . '\' and dealed=0 limit ' . $num;
             $result = $db->query($query);
             $i = mysqli_num_rows($result);
             if ($i > 0) {
                 for (; $i; $i--) {
                     $tmp = $result->fetch_assoc();
                     if (isset($cid_2_drone[$tmp['cid']])) {
                         if (1 == $tmp['status'] or 0 == $tmp['status']) {
                             continue;
                         } else {
                             $ccid = $tmp['cid'];
                             $c_complete_array[$tmp['cid']] = $ccid;
                             $requestData[$tmp['cid']]['cid'] = $ccid;
                             $requestData[$tmp['cid']]['status'] = $tmp['status'];
                             $requestData[$tmp['cid']]['data'] = '';
                             $requestLen = false;
                             if (2 == $tmp['status']) {
                                 $query = 'select data from ' . self::$_mysql_ini['prefix'] . 'data_response where tid=\'' . self::$_tid . '\' and cid=' . $ccid . ' limit 1';
                                 $response_result = $db->query($query);
                                 if (1 == mysqli_num_rows($response_result)) {
                                     $tmp = $response_result->fetch_assoc();
                                     $requestData[$ccid]['data'] = $tmp['data'];
                                 }
                             }
                             $ret = 1;
                             $c_new_gets = true;
                         }
                     }
                 }
                 if (!empty($c_complete_array)) {
                     $queryHead = 'update ' . self::$_mysql_ini['prefix'] . 'online_task set dealed=1 where tid=\'' . self::$_tid . '\' and (';
                     if (self::batch_mysql_query($queryHead, 'cid', $c_complete_array, $db)) {
                         $num -= count($c_complete_array);
                         //unset ($cid_2_drone[$tmp['cid']]);
                     }
                 }
             }
             $db->close();
             if (self::$_nonBlock) {
                 break;
             }
             if (0 === $ret) {
                 if ($total >= $srv_block_ini['sleep']) {
                     $total -= $srv_block_ini['sleep'];
                     sleep($srv_block_ini['sleep']);
                     continue;
                 } else {
                     break;
                 }
             }
             if (1 === $ret and $num and true === $c_new_gets) {
                 //break;
                 $total = 0;
                 sleep($srv_block_ini['sleep']);
                 GlobalFunc::errorlog("[sleep]" . $srv_block_ini['sleep'] . " tid:" . self::$_tid . " num:" . $num);
                 continue;
             } else {
                 break;
             }
         }
     }
     return $ret;
 }
示例#2
0
 public static function KeepAlive()
 {
     global $srv_block_ini;
     global $mysql_ini;
     global $clientId;
     global $mac_num;
     $breath_delay = 0;
     $db_timer = time();
     $active_offline = false;
     // Drone 主动下线
     while (true) {
         usleep($srv_block_ini['sleep'] * 1000000);
         $db = GlobalFunc::connect_db($mysql_ini);
         if ($db) {
             $c_status = connection_status();
             if ($active_offline or $c_status != 0) {
                 //client offline
                 $query = 'delete from ' . $mysql_ini['prefix'] . "online_mac where cid={$clientId} limit {$mac_num}";
                 $db->query($query);
                 $module_num = 0;
                 $query = 'select mod_num from ' . $mysql_ini['prefix'] . "online_clients where cid={$clientId} limit 1";
                 $result = $db->query($query);
                 if (1 === mysqli_num_rows($result)) {
                     $result = $result->fetch_assoc();
                     $module_num = $result['mod_num'];
                     $module_num++;
                     // add sys remain mid Number
                     if ($module_num) {
                         $query = 'delete from ' . $mysql_ini['prefix'] . "online_module where cid={$clientId} limit {$module_num}";
                         $db->query($query);
                     }
                     $query = 'delete from ' . $mysql_ini['prefix'] . "online_clients where cid={$clientId} limit 1";
                     $db->query($query);
                 }
                 break;
             } else {
                 $query = 'select rid,tid,module from ' . $mysql_ini['prefix'] . 'online_task  where cid = ' . $clientId . ' and status = 0';
                 $result = $db->query($query);
                 $i = mysqli_num_rows($result);
                 for (; $i > 0; $i--) {
                     $request_data = '';
                     $error = false;
                     $tmp = $result->fetch_assoc();
                     if (!empty($tmp['rid'])) {
                         if (32 === strlen($tmp['rid'])) {
                             $query = 'select data from ' . $mysql_ini['prefix'] . 'data_request where rid = \'' . $tmp['rid'] . '\' limit 1';
                         } else {
                             $query = 'select data from ' . $mysql_ini['prefix'] . 'shortcuts where sid = \'' . $tmp['rid'] . '\' limit 1';
                         }
                         $res = $db->query($query);
                         if (1 == mysqli_num_rows($res)) {
                             $a = $res->fetch_assoc();
                             $request_data = $a['data'];
                         } else {
                             //出错找不到request Data,写入返回
                             $error = true;
                             $query = 'update ' . $mysql_ini['prefix'] . 'online_task set status = -101 where tid = \'' . $tmp['tid'] . '\' and cid=' . $clientId . ' limit 1';
                             $db->query($query);
                         }
                     }
                     if (false === $error) {
                         $len = strlen($request_data);
                         $out = pack("H32CA32CV", $tmp['module'], 255, $tmp['tid'], 255, $len);
                         echo "" . "{$out}" . $request_data;
                         //."\x00";
                         echo "";
                         ob_flush();
                         flush();
                         $query = 'update ' . $mysql_ini['prefix'] . 'online_task set status = 1 where tid = \'' . $tmp['tid'] . '\' and cid=' . $clientId . ' limit 1';
                         $db->query($query);
                         //reset breath delay
                         $breath_delay = $srv_block_ini['breath_delay'];
                     }
                 }
                 if ($breath_delay < 0) {
                     echo "";
                     ob_flush();
                     flush();
                     //reset breath delay
                     $breath_delay = $srv_block_ini['breath_delay'];
                 } else {
                     $breath_delay -= $srv_block_ini['sleep'];
                 }
                 //db.living
                 $c_time = time();
                 if ($c_time - $db_timer > $srv_block_ini['db_living']) {
                     $db_timer = $c_time;
                     $query = "update " . $mysql_ini['prefix'] . "online_clients set lastliving = {$db_timer} where cid={$clientId} and status = 1 limit 1";
                     $db->query($query);
                     if (1 != $db->affected_rows) {
                         GlobalFunc::errorlog('db.living update 失败,疑似Drone主动下线...', 2);
                         $active_offline = true;
                     }
                 }
             }
             $db->close();
         } else {
         }
     }
 }
示例#3
0
                ob_flush();
                flush();
                $waitSeconds = 0;
                $chunk_output++;
            }
            if ($totalSize > 0 and $chunked_size >= $totalSize) {
                $end_status = STREAM_TASK_END_COMPLISHED;
                break;
            }
            if (0 !== connection_status()) {
                $end_status = STREAM_TASK_END_BROWSERBROKE;
                break;
            }
            $db->close();
            sleep($waitSecPer);
            $db = GlobalFunc::connect_db($mysql_ini);
            if (!$db) {
                $lasterror[] = $language['fail_db'] . ' StreamLoop';
                $end_status = STREAM_TASK_END_CONNMYSQL;
            }
            $waitSeconds += $waitSecPer;
            if ($waitSeconds > $streamWaitExpire) {
                $lasterror[] = 'connect over time';
                $end_status = STREAM_TASK_END_OVERTIME;
                break;
            }
        }
    }
}
if ($lasterror and !$header_show) {
    include $template->getfile('tips.htm');
示例#4
0
 public static function KeepAlive()
 {
     global $srv_block_ini;
     global $mysql_ini;
     global $clientId;
     global $mac_num;
     global $stream_path;
     //global $client_pipe_prefix;
     $fifo_file = $stream_path . self::$_client_pipe_prefix . "{$clientId}";
     $mac_record_cleared = false;
     $active_offline = false;
     // Drone 主动下线
     if (file_exists($fifo_file)) {
         unlink($fifo_file);
     }
     $success = posix_mkfifo($fifo_file, 0700);
     if (!$success) {
         GlobalFunc::errorlog('Error: Could not create a named pipe: ' . posix_strerror(posix_errno()));
     } else {
         $pipe = @fopen($fifo_file, 'r+b');
         if ($pipe) {
             stream_set_blocking($pipe, false);
             $off_line = false;
             while (1) {
                 $read = array($pipe);
                 $write = NULL;
                 $except = NULL;
                 if (false === ($num_changed_streams = stream_select($read, $write, $except, $srv_block_ini['breath_delay']))) {
                     break;
                 } elseif ($num_changed_streams > 0) {
                     while (1) {
                         $a = fread($pipe, 1);
                         if (0 == strlen($a)) {
                             break;
                         } else {
                             echo "{$a}";
                         }
                     }
                     ob_flush();
                     flush();
                 } else {
                     if ($off_line) {
                         break;
                     } else {
                         echo "";
                         ob_flush();
                         flush();
                     }
                 }
                 if ($active_offline or !$off_line and 0 != connection_status()) {
                     $off_line = true;
                     $db = GlobalFunc::connect_db($mysql_ini);
                     if ($db) {
                         $query = 'delete from ' . $mysql_ini['prefix'] . "online_mac where cid={$clientId} limit {$mac_num}";
                         $db->query($query);
                         $mac_record_cleared = true;
                         $query = 'update ' . $mysql_ini['prefix'] . "online_clients set status=0 where cid={$clientId} limit 1";
                         $db->query($query);
                         $db->close();
                     }
                 } else {
                     //db.living
                     $c_time = time();
                     if ($c_time - $db_timer > $srv_block_ini['db_living']) {
                         $db_timer = $c_time;
                         $db = GlobalFunc::connect_db($mysql_ini);
                         if ($db) {
                             $query = "update " . $mysql_ini['prefix'] . "online_clients set lastliving = {$db_timer} where cid={$clientId} and status = 1 limit 1";
                             $db->query($query);
                             if (1 != $db->affected_rows) {
                                 GlobalFunc::errorlog('db.living update 失败 (namepipe),疑似Drone主动下线...cid:' . "{$clientId}", 2);
                                 $active_offline = true;
                             }
                             $db->close();
                         }
                     }
                 }
             }
             fclose($pipe);
         } else {
             GlobalFunc::errorlog('Error: fopen fifo_file : ' . $fifo_file);
         }
         $db = GlobalFunc::connect_db($mysql_ini);
         if ($db) {
             if (!$mac_record_cleared) {
                 $query = 'delete from ' . $mysql_ini['prefix'] . "online_mac where cid={$clientId} limit {$mac_num}";
                 $db->query($query);
             }
             $module_num = 0;
             $query = 'select mod_num from ' . $mysql_ini['prefix'] . "online_clients where cid={$clientId} limit 1";
             $result = $db->query($query);
             if (1 === mysqli_num_rows($result)) {
                 $result = $result->fetch_assoc();
                 $module_num = $result['mod_num'];
                 $module_num++;
                 // add sys remain mid Number
                 if ($module_num) {
                     $query = 'delete from ' . $mysql_ini['prefix'] . "online_module where cid={$clientId} limit {$module_num}";
                     $db->query($query);
                 }
             }
             $query = 'delete from ' . $mysql_ini['prefix'] . "online_clients where cid={$clientId} limit 1";
             $db->query($query);
             $db->close();
         }
         unlink($fifo_file);
     }
     GlobalFunc::errorlog('namepipe.keeplive 结束返回...cid:' . "{$clientId}", 2);
 }