vitess-gh/php/bsonrpc.php

43 строки
1.1 KiB
PHP
Исходник Обычный вид История

2015-01-20 08:03:45 +03:00
<?php
/*
NOTE: This module requires bson_encode() and bson_decode(),
which can be obtained by installing the MongoDB driver.
For example, on Debian/Ubuntu:
$ sudo apt-get install php5-mongo
*/
require_once('gorpc.php');
class BsonRpcClient extends GoRpcClient {
const LEN_PACK_FORMAT = 'V';
const LEN_PACK_SIZE = 4;
public function dial($addr) {
parent::dial("tcp://$addr", '/_bson_rpc_');
}
2015-01-20 08:03:45 +03:00
protected function send_request(GoRpcRequest $req) {
$this->write(bson_encode($req->header));
2015-01-20 11:42:01 +03:00
if ($req->body === NULL)
$this->write(bson_encode(array()));
else
$this->write(bson_encode($req->body));
2015-01-20 08:03:45 +03:00
}
protected function read_response() {
// Read the header.
2015-01-20 09:23:29 +03:00
$data = $this->read_n(self::LEN_PACK_SIZE);
2015-01-20 11:42:01 +03:00
$len = unpack(self::LEN_PACK_FORMAT, $data)[1];
2015-01-20 09:23:29 +03:00
$header = $data . $this->read_n($len - self::LEN_PACK_SIZE);
2015-01-20 08:03:45 +03:00
// Read the body.
2015-01-20 09:23:29 +03:00
$data = $this->read_n(self::LEN_PACK_SIZE);
2015-01-20 11:42:01 +03:00
$len = unpack(self::LEN_PACK_FORMAT, $data)[1];
2015-01-20 09:23:29 +03:00
$body = $data . $this->read_n($len - self::LEN_PACK_SIZE);
2015-01-20 08:03:45 +03:00
// Decode and return.
return new GoRpcResponse(bson_decode($header), bson_decode($body));
}
}