Пример #1
0
 public function create_dbf($filename, $def)
 {
     if (!dbase_create($filename, $def)) {
         return cy_dt(CYE_ERROR, 'create DBF file failed!');
     }
     return cy_dt(0);
 }
Пример #2
0
 function run($client)
 {
     if (!is_resource($client)) {
         cy_log(CYE_ERROR, 'bad requests.');
         return cy_dt(CYE_NET_ERROR, ['code' => 500]);
     }
     $_GET = $_POST = $_COOKIE = array();
     $options = array();
     $display = 'json';
     $dt = $this->read_requests($client);
     if ($dt['errno'] === CYE_DATA_EMPTY) {
         return $dt;
     }
     if ($dt['errno'] !== 0) {
         switch ($dt['errno']) {
             case CYE_ACCESS_DENIED:
                 $dt['code'] = 403;
                 break;
             case CYE_EXPECT_FAIL:
                 $dt['code'] = 417;
                 break;
             default:
                 $dt['code'] = 503;
                 break;
         }
         goto end;
     }
     $dt = $this->dispatch($_SERVER['REQUEST_URI'], $options);
     end:
     empty($dt['code']) && ($dt['code'] = 200);
     empty($dt['data']) && ($dt['data'] = '');
     return $this->write_responses($client, $dt);
 }
Пример #3
0
 function outputs($binary)
 {
     $c_clist = [[CY_TYPE_STRING, "key", 64], [CY_TYPE_STRING, "val", 1024]];
     $c_host = [[CY_TYPE_UINT32, "host"], [CY_TYPE_STRING, "port", 4]];
     $c_host1 = [[CY_TYPE_UINT32, "port"], [CY_TYPE_STRING, "host"]];
     $config = [[CY_TYPE_OBJECT, 'global', self::$c_global], [CY_TYPE_UINT32, "bucket_count"], [CY_TYPE_UINT32, "copy_count"], [CY_TYPE_UINT32, "version"], [CY_TYPE_UINT32, "config_count"], [CY_TYPE_OBJECT, "config_list", $c_clist, "config_count"], [CY_TYPE_UINT32, "hash_size"], [CY_TYPE_STRING, "hash_data", "hash_size"], [CY_TYPE_UINT32, "server_count"], [CY_TYPE_OBJECT, "server_available", $c_host1, "server_count"]];
     $array = cy_unpack($binary, $config);
     if (empty($array['global']['flag'])) {
         return cy_dt(-1, 'error respones');
     }
     $table = gzuncompress($array['hash_data']);
     $count = strlen($table) / 8;
     $list = cy_unpack($table, [[CY_TYPE_OBJECT, "list", $c_host, $count]]);
     $srvl = [];
     foreach ($list['list'] as $srv) {
         $port = unpack("V", $srv['port']);
         $srvl[] = long2ip($srv['host']) . ':' . $port[1];
     }
     foreach ($array['server_available'] as &$srv) {
         $host = unpack("V", $srv['host']);
         $srv['host'] = long2ip($host[1]);
     }
     unset($srv);
     $data = [];
     $data['server_list'] = $srvl;
     $data['server_available'] = $array['server_available'];
     $data['config_list'] = $array['config_list'];
     return cy_dt(0, $data);
 }
Пример #4
0
 function send($data)
 {
     try {
         $msg = new AMQPMessage($data, array('delivery_mode' => 2));
         $this->channel->basic_publish($msg, '', $this->queue);
         return cy_dt(OK);
     } catch (Exception $e) {
         cy_log(CYE_ERROR, $e->getMessage());
         return cy_dt(CYE_NET_ERROR, $e->getMessage());
     }
 }
Пример #5
0
 function __call($method, $data)
 {
     $method = 'CY_Driver_Tair_' . $method;
     $req = $data[0];
     $opt = isset($data[1]) ? $data[1] : [];
     $opt['chid'] = ++$this->chid;
     $c = new $method($req, $opt);
     $req = ['body' => $c->inputs($this->net), 'server' => $this->server()];
     if (!$this->net->prepare($this->chid, $req)) {
         return cy_dt(CYE_PARAM_ERROR);
     }
     $dt = $this->net->get();
     if ($dt['errno'] !== 0) {
         return $dt;
     }
     $dc = $dt['data'][$this->chid];
     if ($dc['errno'] !== 0) {
         return $dc;
     }
     return $c->outputs($dc['data']);
 }
Пример #6
0
 function recv($callback = NULL, $timeout = 0.1)
 {
     $active = 0;
     /* A reference to a flag to tell whether the cURLs are still running  */
     $t1 = $t2 = microtime(true);
     if ($this->count < 1) {
         return cy_dt(1, 'empty handle.');
     }
     $errno = curl_multi_exec($this->mh, $active);
     if ($errno != CURLM_OK) {
         return cy_dt($errno, curl_strerror($errno));
     }
     $select_func = function_exists('cy_curl_multi_select') ? 'cy_curl_multi_select' : 'curl_multi_select';
     while ($this->count === $active && $t2 - $t1 < $timeout) {
         //$ready = $select_func($this->mh, $timeout, 0x01|0x4);
         $ready = curl_multi_select($this->mh, $timeout);
         if ($ready < 0) {
             return cy_dt(-1, 'curl_multi_select error');
         }
         $errno = curl_multi_exec($this->mh, $active);
         if ($errno != CURLM_OK) {
             return cy_dt($errno, curl_strerror($errno));
         }
         $t2 = microtime(true);
     }
     $data = [];
     while ($r = curl_multi_info_read($this->mh, $msgs_in_queue)) {
         $ch = $r['handle'];
         list(, $c, $key) = $this->handles[(int) $ch];
         $data[$key] = $mix = $this->mix($ch, $c);
         if ($callback) {
             call_user_func($callback, $key, $mix);
         }
     }
     return cy_dt(OK, $data);
 }
Пример #7
0
 function get($id, $req, $env)
 {
     return cy_dt(0, $req);
 }
Пример #8
0
 function query($sql, $options = [])
 {
     $timeout = isset($options['timeout']) ? $options['timeout'] : $_ENV['config']['timeout']['mysql_read'];
     if (!$this->is_avaliable()) {
         return cy_dt(CYE_DB_CONNECT_ERROR, 'mysql connect is not available.');
     }
     $this->db->query($sql, MYSQLI_ASYNC);
     $finished = 0;
     $t1 = microtime(true);
     while (!$finished) {
         $links = $errors = $reject = [$this->db];
         if (!mysqli_poll($links, $errors, $reject, 0, 100000)) {
             if (microtime(true) - $t1 < $timeout) {
                 continue;
             }
             $this->db->close();
             cy_log(CYE_ERROR, 'mysql query timeout, over %f second', $timeout);
             $dt = cy_dt(CYE_NET_TIMEOUT, 'mysql query timeout');
             goto end;
         }
         $finished = 1;
     }
     if (!($result = $this->db->reap_async_query())) {
         cy_log(CYE_ERROR, 'mysql query error [%d] [%s]', $this->db->errno, $this->db->error);
         $dt = cy_dt($this->db->errno, $this->db->error);
         goto end;
     }
     /* For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object */
     if (is_object($result)) {
         $rows = $result->fetch_all(MYSQLI_ASSOC);
         mysqli_free_result($result);
         $dt = cy_dt(0, $rows);
         goto end;
     }
     /* For other successful queries mysqli_query() will return TRUE. */
     $r = ['affected_rows' => $this->db->affected_rows, 'insert_id' => $this->db->insert_id];
     $dt = ['errno' => 0, 'data' => $r, 'info' => $this->db->info];
     end:
     $t2 = microtime(true);
     cy_stat('mysql-query', ($t2 - $t1) * 1000000);
     return $dt;
 }
Пример #9
0
 function __call($method, $args)
 {
     $data = [];
     $call = ['mGet' => 'find', 'save' => 'save', 'mInsert' => 'batchInsert', 'delete' => 'remove', 'update' => 'update'];
     $getid = ['mGet' => false, 'save' => 'save', 'mInsert' => true, 'delete' => false, 'update' => true];
     if (empty($call[$method])) {
         return cy_dt(-1, 'unkown method.');
     }
     $t1 = microtime(true);
     $table = array_shift($args);
     $dbname = $this->c['database'];
     try {
         if (!self::$mongo) {
             self::$mongo = $this->connect();
         }
         $db = self::$mongo->{$dbname};
         $c10n = $table == 'file' ? $db->getGridFS() : $db->{$table};
         $back = call_user_func_array([$c10n, $call[$method]], $args);
         if (is_object($back)) {
             $back = iterator_to_array($back);
         } else {
             if ($getid[$method] && $back) {
                 $list = array();
                 foreach ($args as $k => $v) {
                     $list[$k] = (string) $v['_id'];
                 }
                 $back = $list;
             }
         }
         $data = cy_dt(0, $back);
         //self::$mongo->close();
     } catch (MongoCursorException $e) {
         $error = substr($e->getMessage(), 0, 512);
         cy_log(CYE_ERROR, "mongo-{$method} " . $error);
         $data = cy_dt(CYE_SYSTEM_ERROR, $error);
     } catch (MongoCursorTimeoutException $e) {
         $error = substr($e->getMessage(), 0, 512);
         cy_log(CYE_ERROR, "mongo-{$method} " . $error);
         cy_log(CYE_ERROR, "mongo-{$method} close=%d, reconnect=%d", self::$mongo->close(), self::$mongo->connect);
         $data = cy_dt(CYE_SYSTEM_ERROR, $error);
     } catch (MongoConnectionException $e) {
         $error = substr($e->getMessage(), 0, 512);
         cy_log(CYE_ERROR, "mongo-{$method} " . $error);
         $data = cy_dt(CYE_SYSTEM_ERROR, $error);
     } catch (Exception $e) {
         $error = substr($e->getMessage(), 0, 512);
         cy_log(CYE_ERROR, "mongo-{$method} " . $error);
         $data = cy_dt(CYE_SYSTEM_ERROR, $error);
     }
     // end process.
     $cost = (microtime(true) - $t1) * 1000000;
     cy_stat('mongo-' . $method, $cost);
     return $data;
 }
Пример #10
0
 function mSet($data, $options = [])
 {
     $chunk_size = isset($options['chunk_size']) ? $options['chunk_size'] : 50;
     $chunk_data = array_chunk($data, $chunk_size);
     $update = isset($options['update']) ? $options['update'] : false;
     $table = isset($options['prefix']) ? $options['prefix'] . '_' . $this->table : $this->table;
     $i = 0;
     foreach ($chunk_data as $chunk) {
         $r = ['data' => $chunk];
         $c = new CY_Driver_DB_Default($table, $r, ['method' => 'insert', 'update' => $update]);
         $this->db->add($i++, $c);
     }
     $dt = $this->db->mGet();
     if ($dt['errno'] !== 0) {
         return $dt;
     }
     $insert_id = PHP_INT_MAX;
     $affected_rows = 0;
     foreach ($dt['data'] as $i => $dt_sub) {
         if ($dt_sub['errno'] !== 0) {
             cy_log(CYE_WARNING, 'MySQL::mSet part failed.');
             continue;
         }
         $v = $dt_sub['data'];
         if ($v['insert_id'] === 0 || $v['affected_rows'] === 0) {
             cy_log(CYE_DEBUG, 'MySQL::mSet insert but no affect, maybe exists');
             continue;
         }
         $insert_id > $v['insert_id'] && ($insert_id = $v['insert_id']);
         $affected_rows += $v['affected_rows'];
     }
     $insert_id === PHP_INT_MAX && ($insert_id = 0);
     return cy_dt(0, ['insert_id' => $insert_id, 'affected_rows' => $affected_rows]);
 }
Пример #11
0
function cy_r_m($dt)
{
    if ($dt['errno'] !== 0) {
        return $dt;
    }
    $data = array();
    foreach ($dt['data'] as $sub) {
        if ($sub['errno'] !== 0) {
            continue;
        }
        $data += $sub['data'];
    }
    return cy_dt(0, $data);
}
Пример #12
0
 function get($ack = false)
 {
     try {
         $flag = $ack ? AMQP_AUTOACK : AMQP_NOPARAM;
         $msg = $this->queue->get($flag);
         if (!$msg) {
             return cy_dt(0);
         }
         return cy_dt(0, ['body' => $msg->getBody(), 'tag' => $msg->getDeliveryTag()]);
     } catch (Exception $e) {
         cy_log(CYE_ERROR, $e->getMessage());
     }
     return cy_dt(CYE_SYSTEM_ERROR);
 }